|
| 1 | +'use strict'; |
| 2 | +// FileSystemAdapter |
| 3 | +// |
| 4 | +// Stores files in local file system |
| 5 | +// Requires write access to the server's file system. |
| 6 | + |
| 7 | +var fs = require('fs'); |
| 8 | +var path = require('path'); |
| 9 | +var pathSep = require('path').sep; |
| 10 | + |
| 11 | +function FileSystemAdapter(options) { |
| 12 | + options = options || {}; |
| 13 | + let filesSubDirectory = options.filesSubDirectory || ''; |
| 14 | + this._filesDir = filesSubDirectory; |
| 15 | + this._mkdir(this._getApplicationDir()); |
| 16 | + if (!this._applicationDirExist()) { |
| 17 | + throw "Files directory doesn't exist."; |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +FileSystemAdapter.prototype.createFile = function(filename, data) { |
| 22 | + return new Promise((resolve, reject) => { |
| 23 | + let filepath = this._getLocalFilePath(filename); |
| 24 | + fs.writeFile(filepath, data, (err) => { |
| 25 | + if(err !== null) { |
| 26 | + return reject(err); |
| 27 | + } |
| 28 | + resolve(data); |
| 29 | + }); |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +FileSystemAdapter.prototype.deleteFile = function(filename) { |
| 34 | + return new Promise((resolve, reject) => { |
| 35 | + let filepath = this._getLocalFilePath(filename); |
| 36 | + fs.readFile( filepath , function (err, data) { |
| 37 | + if(err !== null) { |
| 38 | + return reject(err); |
| 39 | + } |
| 40 | + fs.unlink(filepath, (unlinkErr) => { |
| 41 | + if(err !== null) { |
| 42 | + return reject(unlinkErr); |
| 43 | + } |
| 44 | + resolve(data); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +FileSystemAdapter.prototype.getFileData = function(filename) { |
| 52 | + return new Promise((resolve, reject) => { |
| 53 | + let filepath = this._getLocalFilePath(filename); |
| 54 | + fs.readFile( filepath , function (err, data) { |
| 55 | + if(err !== null) { |
| 56 | + return reject(err); |
| 57 | + } |
| 58 | + resolve(data); |
| 59 | + }); |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +FileSystemAdapter.prototype.getFileLocation = function(config, filename) { |
| 64 | + return (config.mount + '/' + this._getLocalFilePath(filename)); |
| 65 | +} |
| 66 | + |
| 67 | +/* |
| 68 | + Helpers |
| 69 | + --------------- */ |
| 70 | + FileSystemAdapter.prototype._getApplicationDir = function() { |
| 71 | + if (this._filesDir) { |
| 72 | + return path.join('files', this._filesDir); |
| 73 | + } else { |
| 74 | + return 'files'; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | +FileSystemAdapter.prototype._applicationDirExist = function() { |
| 79 | + return fs.existsSync(this._getApplicationDir()); |
| 80 | +} |
| 81 | + |
| 82 | +FileSystemAdapter.prototype._getLocalFilePath = function(filename) { |
| 83 | + let applicationDir = this._getApplicationDir(); |
| 84 | + if (!fs.existsSync(applicationDir)) { |
| 85 | + this._mkdir(applicationDir); |
| 86 | + } |
| 87 | + return path.join(applicationDir, encodeURIComponent(filename)); |
| 88 | +} |
| 89 | + |
| 90 | +FileSystemAdapter.prototype._mkdir = function(dirPath) { |
| 91 | + // snippet found on -> https://gist.github.com/danherbert-epam/3960169 |
| 92 | + let dirs = dirPath.split(pathSep); |
| 93 | + var root = ""; |
| 94 | + |
| 95 | + while (dirs.length > 0) { |
| 96 | + var dir = dirs.shift(); |
| 97 | + if (dir === "") { // If directory starts with a /, the first path will be an empty string. |
| 98 | + root = pathSep; |
| 99 | + } |
| 100 | + if (!fs.existsSync(path.join(root, dir))) { |
| 101 | + try { |
| 102 | + fs.mkdirSync(path.join(root, dir)); |
| 103 | + } |
| 104 | + catch (e) { |
| 105 | + if ( e.code == 'EACCES' ) { |
| 106 | + throw new Error("PERMISSION ERROR: In order to use the FileSystemAdapter, write access to the server's file system is required."); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + root = path.join(root, dir, pathSep); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +module.exports = FileSystemAdapter; |
| 115 | +module.exports.default = FileSystemAdapter; |
0 commit comments