-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Closed
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1dfc7e4
Add FileSystemAdapter
dtsolis ae8fe88
Add options to the FileSystemAdapter constructor
dtsolis 5866e61
Remove file system permissions validation from CLI
dtsolis b8c08a3
Remove config parameter from _getLocalFilePath()
dtsolis 13bc540
Add FileSystemAdapter
dtsolis 273ee52
Remove file system permissions validation from CLI
dtsolis ac42754
Add helper methods to the FileSystemAdapter
dtsolis 89ae73a
Throw on FileSystemAdapter constructor instead of 'process.exit(1)'
dtsolis a9b2f46
Add test for the FileSystemAdapter
dtsolis 8b55128
Fix importing FilesController two times
dtsolis 31f56ee
Fix conflict problems
dtsolis 2f08a57
Add path support for windows
dtsolis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add options to the FileSystemAdapter constructor
- Loading branch information
commit ae8fe88b0d5424e86c2797bf1ce714c76d1364f8
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
||
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) { | ||
|
@@ -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 + '/'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add line break after that line |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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