Moves Files adapters to external packages
This commit is contained in:
@@ -1,120 +0,0 @@
|
||||
// FileSystemAdapter
|
||||
//
|
||||
// Stores files in local file system
|
||||
// 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');
|
||||
var pathSep = require('path').sep;
|
||||
|
||||
export class FileSystemAdapter extends FilesAdapter {
|
||||
|
||||
constructor({filesSubDirectory = ''} = {}) {
|
||||
super();
|
||||
|
||||
this._filesDir = filesSubDirectory;
|
||||
this._mkdir(this._getApplicationDir());
|
||||
if (!this._applicationDirExist()) {
|
||||
throw "Files directory doesn't exist.";
|
||||
}
|
||||
}
|
||||
|
||||
// For a given config object, filename, and data, store a file
|
||||
// Returns a promise
|
||||
createFile(config, 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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
deleteFile(config, 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);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
getFileData(config, 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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
getFileLocation(config, filename) {
|
||||
return (config.mount + '/' + this._getLocalFilePath(filename));
|
||||
}
|
||||
|
||||
/*
|
||||
Helpers
|
||||
--------------- */
|
||||
_getApplicationDir() {
|
||||
if (this._filesDir) {
|
||||
return path.join('files', this._filesDir);
|
||||
} else {
|
||||
return 'files';
|
||||
}
|
||||
}
|
||||
|
||||
_applicationDirExist() {
|
||||
return fs.existsSync(this._getApplicationDir());
|
||||
}
|
||||
|
||||
_getLocalFilePath(filename) {
|
||||
let applicationDir = this._getApplicationDir();
|
||||
if (!fs.existsSync(applicationDir)) {
|
||||
this._mkdir(applicationDir);
|
||||
}
|
||||
return path.join(applicationDir, encodeURIComponent(filename));
|
||||
}
|
||||
|
||||
_mkdir(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default FileSystemAdapter;
|
||||
@@ -13,22 +13,22 @@
|
||||
|
||||
export class FilesAdapter {
|
||||
/* this method is responsible to store the file in order to be retrived later by it's file name
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param config the current config
|
||||
* @param filename the filename to save
|
||||
* @param data the buffer of data from the file
|
||||
* @param contentType the supposed contentType
|
||||
* @discussion the contentType can be undefined if the controller was not able to determine it
|
||||
*
|
||||
* @discussion the contentType can be undefined if the controller was not able to determine it
|
||||
*
|
||||
* @return a promise that should fail if the storage didn't succeed
|
||||
*
|
||||
*
|
||||
*/
|
||||
createFile(config, filename: string, data, contentType: string) { }
|
||||
createFile(filename: string, data, contentType: string) { }
|
||||
|
||||
deleteFile(config, filename) { }
|
||||
deleteFile(filename) { }
|
||||
|
||||
getFileData(config, filename) { }
|
||||
getFileData(filename) { }
|
||||
|
||||
getFileLocation(config, filename) { }
|
||||
}
|
||||
|
||||
@@ -1,125 +0,0 @@
|
||||
// GCSAdapter
|
||||
// Store Parse Files in Google Cloud Storage: https://cloud.google.com/storage
|
||||
import { storage } from 'gcloud';
|
||||
import { FilesAdapter } from './FilesAdapter';
|
||||
import requiredParameter from '../../requiredParameter';
|
||||
|
||||
function requiredOrFromEnvironment(env, name) {
|
||||
let environmentVariable = process.env[env];
|
||||
if (!environmentVariable) {
|
||||
requiredParameter(`GCSAdapter requires an ${name}`);
|
||||
}
|
||||
return environmentVariable;
|
||||
}
|
||||
|
||||
function fromEnvironmentOrDefault(env, defaultValue) {
|
||||
let environmentVariable = process.env[env];
|
||||
if (environmentVariable) {
|
||||
return environmentVariable;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
export class GCSAdapter extends FilesAdapter {
|
||||
// GCS Project ID and the name of a corresponding Keyfile are required.
|
||||
// Unlike the S3 adapter, you must create a new Cloud Storage bucket, as this is not created automatically.
|
||||
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/master/guides/authentication
|
||||
// for more details.
|
||||
constructor(
|
||||
projectId = requiredOrFromEnvironment('GCP_PROJECT_ID', 'projectId'),
|
||||
keyFilename = requiredOrFromEnvironment('GCP_KEYFILE_PATH', 'keyfile path'),
|
||||
bucket = requiredOrFromEnvironment('GCS_BUCKET', 'bucket name'),
|
||||
{ bucketPrefix = fromEnvironmentOrDefault('GCS_BUCKET_PREFIX', ''),
|
||||
directAccess = fromEnvironmentOrDefault('GCS_DIRECT_ACCESS', false) } = {}) {
|
||||
super();
|
||||
|
||||
this._bucket = bucket;
|
||||
this._bucketPrefix = bucketPrefix;
|
||||
this._directAccess = directAccess;
|
||||
|
||||
let options = {
|
||||
projectId: projectId,
|
||||
keyFilename: keyFilename
|
||||
};
|
||||
|
||||
this._gcsClient = new storage(options);
|
||||
}
|
||||
|
||||
// For a given config object, filename, and data, store a file in GCS.
|
||||
// Resolves the promise or fails with an error.
|
||||
createFile(config, filename, data, contentType) {
|
||||
let params = {
|
||||
contentType: contentType || 'application/octet-stream'
|
||||
};
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
let file = this._gcsClient.bucket(this._bucket).file(this._bucketPrefix + filename);
|
||||
// gcloud supports upload(file) not upload(bytes), so we need to stream.
|
||||
var uploadStream = file.createWriteStream(params);
|
||||
uploadStream.on('error', (err) => {
|
||||
return reject(err);
|
||||
}).on('finish', () => {
|
||||
// Second call to set public read ACL after object is uploaded.
|
||||
if (this._directAccess) {
|
||||
file.makePublic((err, res) => {
|
||||
if (err !== null) {
|
||||
return reject(err);
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
uploadStream.write(data);
|
||||
uploadStream.end();
|
||||
});
|
||||
}
|
||||
|
||||
// Deletes a file with the given file name.
|
||||
// Returns a promise that succeeds with the delete response, or fails with an error.
|
||||
deleteFile(config, filename) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let file = this._gcsClient.bucket(this._bucket).file(this._bucketPrefix + filename);
|
||||
file.delete((err, res) => {
|
||||
if(err !== null) {
|
||||
return reject(err);
|
||||
}
|
||||
resolve(res);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Search for and return a file if found by filename.
|
||||
// Returns a promise that succeeds with the buffer result from GCS, or fails with an error.
|
||||
getFileData(config, filename) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let file = this._gcsClient.bucket(this._bucket).file(this._bucketPrefix + filename);
|
||||
// Check for existence, since gcloud-node seemed to be caching the result
|
||||
file.exists((err, exists) => {
|
||||
if (exists) {
|
||||
file.download((err, data) => {
|
||||
if (err !== null) {
|
||||
return reject(err);
|
||||
}
|
||||
return resolve(data);
|
||||
});
|
||||
} else {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Generates and returns the location of a file stored in GCS for the given request and filename.
|
||||
// The location is the direct GCS link if the option is set,
|
||||
// otherwise we serve the file through parse-server.
|
||||
getFileLocation(config, filename) {
|
||||
if (this._directAccess) {
|
||||
return `https://${this._bucket}.storage.googleapis.com/${this._bucketPrefix + filename}`;
|
||||
}
|
||||
return (config.mount + '/files/' + config.applicationId + '/' + encodeURIComponent(filename));
|
||||
}
|
||||
}
|
||||
|
||||
export default GCSAdapter;
|
||||
@@ -28,7 +28,7 @@ export class GridStoreAdapter extends FilesAdapter {
|
||||
|
||||
// For a given config object, filename, and data, store a file
|
||||
// Returns a promise
|
||||
createFile(config, filename: string, data, contentType) {
|
||||
createFile(filename: string, data, contentType) {
|
||||
return this._connect().then(database => {
|
||||
let gridStore = new GridStore(database, filename, 'w');
|
||||
return gridStore.open();
|
||||
@@ -39,7 +39,7 @@ export class GridStoreAdapter extends FilesAdapter {
|
||||
});
|
||||
}
|
||||
|
||||
deleteFile(config, filename: string) {
|
||||
deleteFile(filename: string) {
|
||||
return this._connect().then(database => {
|
||||
let gridStore = new GridStore(database, filename, 'w');
|
||||
return gridStore.open();
|
||||
@@ -50,7 +50,7 @@ export class GridStoreAdapter extends FilesAdapter {
|
||||
});
|
||||
}
|
||||
|
||||
getFileData(config, filename: string) {
|
||||
getFileData(filename: string) {
|
||||
return this._connect().then(database => {
|
||||
return GridStore.exist(database, filename)
|
||||
.then(() => {
|
||||
|
||||
@@ -1,141 +0,0 @@
|
||||
// S3Adapter
|
||||
//
|
||||
// Stores Parse files in AWS S3.
|
||||
|
||||
import * as AWS from 'aws-sdk';
|
||||
import { FilesAdapter } from './FilesAdapter';
|
||||
import requiredParameter from '../../requiredParameter';
|
||||
|
||||
const DEFAULT_S3_REGION = "us-east-1";
|
||||
|
||||
function requiredOrFromEnvironment(env, name) {
|
||||
let environmentVariable = process.env[env];
|
||||
if (!environmentVariable) {
|
||||
requiredParameter(`S3Adapter requires an ${name}`);
|
||||
}
|
||||
return environmentVariable;
|
||||
}
|
||||
|
||||
function fromEnvironmentOrDefault(env, defaultValue) {
|
||||
let environmentVariable = process.env[env];
|
||||
if (environmentVariable) {
|
||||
return environmentVariable;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
export class S3Adapter extends FilesAdapter {
|
||||
// Creates an S3 session.
|
||||
// Providing AWS access and secret keys is mandatory
|
||||
// Region and bucket will use sane defaults if omitted
|
||||
constructor(
|
||||
accessKey = requiredOrFromEnvironment('S3_ACCESS_KEY', 'accessKey'),
|
||||
secretKey = requiredOrFromEnvironment('S3_SECRET_KEY', 'secretKey'),
|
||||
bucket = fromEnvironmentOrDefault('S3_BUCKET', undefined),
|
||||
{ region = fromEnvironmentOrDefault('S3_REGION', DEFAULT_S3_REGION),
|
||||
bucketPrefix = fromEnvironmentOrDefault('S3_BUCKET_PREFIX', ''),
|
||||
directAccess = fromEnvironmentOrDefault('S3_DIRECT_ACCESS', false) } = {}) {
|
||||
super();
|
||||
|
||||
this._region = region;
|
||||
this._bucket = bucket;
|
||||
this._bucketPrefix = bucketPrefix;
|
||||
this._directAccess = directAccess;
|
||||
|
||||
let s3Options = {
|
||||
accessKeyId: accessKey,
|
||||
secretAccessKey: secretKey,
|
||||
params: { Bucket: this._bucket }
|
||||
};
|
||||
AWS.config._region = this._region;
|
||||
this._s3Client = new AWS.S3(s3Options);
|
||||
this._hasBucket = false;
|
||||
}
|
||||
|
||||
createBucket() {
|
||||
var promise;
|
||||
if (this._hasBucket) {
|
||||
promise = Promise.resolve();
|
||||
} else {
|
||||
promise = new Promise((resolve, reject) => {
|
||||
this._s3Client.createBucket(() => {
|
||||
this._hasBucket = true;
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
return promise;
|
||||
}
|
||||
|
||||
// For a given config object, filename, and data, store a file in S3
|
||||
// Returns a promise containing the S3 object creation response
|
||||
createFile(config, filename, data, contentType) {
|
||||
let params = {
|
||||
Key: this._bucketPrefix + filename,
|
||||
Body: data
|
||||
};
|
||||
if (this._directAccess) {
|
||||
params.ACL = "public-read"
|
||||
}
|
||||
if (contentType) {
|
||||
params.ContentType = contentType;
|
||||
}
|
||||
return this.createBucket().then(() => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._s3Client.upload(params, (err, data) => {
|
||||
if (err !== null) {
|
||||
return reject(err);
|
||||
}
|
||||
resolve(data);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
deleteFile(config, filename) {
|
||||
return this.createBucket().then(() => {
|
||||
return new Promise((resolve, reject) => {
|
||||
let params = {
|
||||
Key: this._bucketPrefix + filename
|
||||
};
|
||||
this._s3Client.deleteObject(params, (err, data) =>{
|
||||
if(err !== null) {
|
||||
return reject(err);
|
||||
}
|
||||
resolve(data);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Search for and return a file if found by filename
|
||||
// Returns a promise that succeeds with the buffer result from S3
|
||||
getFileData(config, filename) {
|
||||
let params = {Key: this._bucketPrefix + filename};
|
||||
return this.createBucket().then(() => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._s3Client.getObject(params, (err, data) => {
|
||||
if (err !== null) {
|
||||
return reject(err);
|
||||
}
|
||||
// Something happend here...
|
||||
if (data && !data.Body) {
|
||||
return reject(data);
|
||||
}
|
||||
resolve(data.Body);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Generates and returns the location of a file stored in S3 for the given request and filename
|
||||
// The location is the direct S3 link if the option is set, otherwise we serve the file through parse-server
|
||||
getFileLocation(config, filename) {
|
||||
if (this._directAccess) {
|
||||
return `https://${this._bucket}.s3.amazonaws.com/${this._bucketPrefix + filename}`;
|
||||
}
|
||||
return (config.mount + '/files/' + config.applicationId + '/' + encodeURIComponent(filename));
|
||||
}
|
||||
}
|
||||
|
||||
export default S3Adapter;
|
||||
Reference in New Issue
Block a user