Skip to content

Commit ead1695

Browse files
committed
Base filesystem adapter
1 parent 6403ad1 commit ead1695

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

index.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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;

package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "parse-server-fs-adapter",
3+
"version": "1.0.0",
4+
"description": "File system adapter for parse-server",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "jasmine"
8+
},
9+
"keywords": [
10+
"parse-server",
11+
"adapter",
12+
"file",
13+
"system"
14+
],
15+
"author": "Parse",
16+
"license": "MIT",
17+
"dependencies": {
18+
"jasmine": "^2.4.1",
19+
"parse-server-conformance-tests": "^1.0.0"
20+
}
21+
}

spec/support/jasmine.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"spec_dir": "spec",
3+
"spec_files": [
4+
"test.spec.js"
5+
]
6+
}

spec/test.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
let filesAdapterTests = require('parse-server-conformance-tests').files;
3+
4+
let FileSystemAdapter = require('../index.js');
5+
6+
describe('FileSystemAdapter tests', () => {
7+
var fsAdapter = new FileSystemAdapter({
8+
filesSubDirectory: 'sub1/sub2'
9+
});
10+
11+
filesAdapterTests.testAdapter("FileSystemAdapter", fsAdapter);
12+
})

0 commit comments

Comments
 (0)