Skip to content

FileSystemAdapter for saving files to the server's file system #716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
Prev Previous commit
Next Next commit
Add options to the FileSystemAdapter constructor
  • Loading branch information
dtsolis committed Mar 12, 2016
commit ae8fe88b0d5424e86c2797bf1ce714c76d1364f8
42 changes: 36 additions & 6 deletions src/Adapters/Files/FileSystemAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@
// Requires write access to the server's file system.

import { FilesAdapter } from './FilesAdapter';
import colors from 'colors';
var fs = require('fs');
var path = require('path');

export class FileSystemAdapter extends FilesAdapter {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please allow options in the constructor to provide a path by the developer


constructor({filesSubDirectory = ''} = {}) {
super();

this._filesDir = filesSubDirectory;
this._mkdir(filesSubDirectory);
}

// For a given config object, filename, and data, store a file
// Returns a promise
createFile(config, filename, data) {
Expand Down Expand Up @@ -55,18 +65,38 @@ export class FileSystemAdapter extends FilesAdapter {
return (config.mount + '/' + this._getLocalFilePath(config, filename));
}

/*
Helpers
--------------- */

_getLocalFilePath(config, filename) {
let filesDir = 'files';
if (!fs.existsSync(filesDir)) {
fs.mkdirSync(filesDir);
}

let applicationDir = filesDir + '/' + config.applicationId;
let applicationDir = filesDir + '/' + this._filesDir;
if (!fs.existsSync(applicationDir)) {
fs.mkdirSync(applicationDir);
this._mkdir(applicationDir);
}
return (applicationDir + '/' + encodeURIComponent(filename));
}

_mkdir(path, root) {
// snippet found on -> http://stackoverflow.com/a/10600228
var dirs = path.split('/'), dir = dirs.shift(), root = (root || '') + dir + '/';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please support windows too with separator


try {
fs.mkdirSync(root);
}
catch (e) {
if ( e.code == 'EACCES' ) {
console.error("");
console.error(colors.red("ERROR: In order to use the FileSystemAdapter, write access to the server's file system is required"));
console.error("");
process.exit(1);
}
//dir wasn't made, something went wrong
if(!fs.statSync(root).isDirectory()) throw new Error(e);
}
return !dirs.length || this._mkdir(dirs.join('/'), root);
}
}

export default FileSystemAdapter;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add line break after that line