Skip to content

Cleanup and modernize S3Adapter to ES6 syntax. #333

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

Merged
merged 1 commit into from
Feb 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/FilesAdapter.js → src/Adapters/Files/FilesAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
// Allows you to change the file storage mechanism.
//
// Adapter classes must implement the following functions:
// * createFileAsync(config, filename, data)
// * getFileDataAsync(config, filename)
// * createFile(config, filename, data)
// * getFileData(config, filename)
// * getFileLocation(config, request, filename)
//
// Default is GridStoreAdapter, which requires mongo
// and for the API server to be using the ExportAdapter
// database adapter.

export class FilesAdapter {
createFileAsync(config, filename, data) { }
createFile(config, filename, data) { }

getFileDataAsync(config, filename) { }
getFileData(config, filename) { }

getFileLocation(config, filename) { }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import { GridStore } from 'mongodb';
import { FilesAdapter } from './FilesAdapter';

class GridStoreAdapter extends FilesAdapter {
export class GridStoreAdapter extends FilesAdapter {
// For a given config object, filename, and data, store a file
// Returns a promise
createFileAsync(config, filename, data) {
createFile(config, filename, data) {
return config.database.connect().then(() => {
let gridStore = new GridStore(config.database.db, filename, 'w');
return gridStore.open();
Expand All @@ -20,7 +20,7 @@ class GridStoreAdapter extends FilesAdapter {
});
}

getFileDataAsync(config, filename) {
getFileData(config, filename) {
return config.database.connect().then(() => {
return GridStore.exist(config.database.db, filename);
}).then(() => {
Expand Down
83 changes: 83 additions & 0 deletions src/Adapters/Files/S3Adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// S3Adapter
//
// Stores Parse files in AWS S3.

import * as AWS from 'aws-sdk';
import { FilesAdapter } from './FilesAdapter';

const DEFAULT_S3_REGION = "us-east-1";
const DEFAULT_S3_BUCKET = "parse-files";

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,
secretKey,
{ region = DEFAULT_S3_REGION,
bucket = DEFAULT_S3_BUCKET,
bucketPrefix = '',
directAccess = 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);
}

// 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) {
let params = {
Key: this._bucketPrefix + filename,
Body: data
};
if (this._directAccess) {
params.ACL = "public-read"
}
return new Promise((resolve, reject) => {
this._s3Client.upload(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 new Promise((resolve, reject) => {
this._s3Client.getObject(params, (err, data) => {
if (err !== null) {
return reject(err);
}
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 + '._s3Client.amazonaws.com' + '/' + this._bucketPrefix + filename);
}
return (config.mount + '/files/' + config.applicationId + '/' + encodeURIComponent(filename));
}
}

export default S3Adapter;
4 changes: 2 additions & 2 deletions src/Controllers/FilesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class FilesController {
return (req, res) => {
let config = new Config(req.params.appId);
let filename = req.params.filename;
this._filesAdapter.getFileDataAsync(config, filename).then((data) => {
this._filesAdapter.getFileData(config, filename).then((data) => {
res.status(200);
var contentType = mime.lookup(filename);
res.set('Content-type', contentType);
Expand Down Expand Up @@ -62,7 +62,7 @@ export class FilesController {
}

let filename = rack() + '_' + req.params.filename + extension;
this._filesAdapter.createFileAsync(req.config, filename, req.body).then(() => {
this._filesAdapter.createFile(req.config, filename, req.body).then(() => {
res.status(201);
var location = this._filesAdapter.getFileLocation(req.config, filename);
res.set('Location', location);
Expand Down
77 changes: 0 additions & 77 deletions src/S3Adapter.js

This file was deleted.

7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ var batch = require('./batch'),
cache = require('./cache'),
DatabaseAdapter = require('./DatabaseAdapter'),
express = require('express'),
S3Adapter = require('./S3Adapter'),
middlewares = require('./middlewares'),
multer = require('multer'),
Parse = require('parse/node').Parse,
PromiseRouter = require('./PromiseRouter'),
httpRequest = require('./httpRequest');

import { default as GridStoreAdapter } from './GridStoreAdapter';
import { default as FilesController } from './Controllers/FilesController';
import { GridStoreAdapter } from './Adapters/Files/GridStoreAdapter';
import { S3Adapter } from './Adapters/Files/S3Adapter';

import { FilesController } from './Controllers/FilesController';

// Mutate the Parse object to add the Cloud Code handlers
addParseCloud();
Expand Down