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