Skip to content

Commit

Permalink
Refactor FilesAdapter to ES6 style.
Browse files Browse the repository at this point in the history
  • Loading branch information
nlutsenko committed Feb 9, 2016
1 parent f2c33c0 commit 5ab58fc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 57 deletions.
25 changes: 13 additions & 12 deletions src/FilesAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@
// Allows you to change the file storage mechanism.
//
// Adapter classes must implement the following functions:
// * create(config, filename, data)
// * get(config, filename)
// * location(config, req, filename)
// * createFileAsync(config, filename, data)
// * getFileDataAsync(config, filename)
// * getFileLocation(config, request, filename)
//
// Default is GridStoreAdapter, which requires mongo
// and for the API server to be using the ExportAdapter
// database adapter.

var GridStoreAdapter = require('./GridStoreAdapter');
var adapter = null;

var adapter = GridStoreAdapter;

function setAdapter(filesAdapter) {
export function setAdapter(filesAdapter) {
adapter = filesAdapter;
}

function getAdapter() {
export function getAdapter() {
return adapter;
}

module.exports = {
getAdapter: getAdapter,
setAdapter: setAdapter
};
export class FilesAdapter {
createFileAsync(config, filename, data) { }

getFileDataAsync(config, filename) { }

getFileLocation(config, request, filename) { }
}
67 changes: 30 additions & 37 deletions src/GridStoreAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,38 @@
// Requires the database adapter to be based on mongoclient

var GridStore = require('mongodb').GridStore;
var path = require('path');

// For a given config object, filename, and data, store a file
// Returns a promise
function create(config, filename, data) {
return config.database.connect().then(() => {
var gridStore = new GridStore(config.database.db, filename, 'w');
return gridStore.open();
}).then((gridStore) => {
return gridStore.write(data);
}).then((gridStore) => {
return gridStore.close();
});
}
import * as Path from 'path';
import { FilesAdapter } from './FilesAdapter';

// Search for and return a file if found by filename
// Resolves a promise that succeeds with the buffer result
// from GridStore
function get(config, filename) {
return config.database.connect().then(() => {
return GridStore.exist(config.database.db, filename);
}).then(() => {
var gridStore = new GridStore(config.database.db, filename, 'r');
return gridStore.open();
}).then((gridStore) => {
return gridStore.read();
});
}
export class GridStoreAdapter extends FilesAdapter {
// For a given config object, filename, and data, store a file
// Returns a promise
createFileAsync(config, filename, data) {
return config.database.connect().then(() => {
var gridStore = new GridStore(config.database.db, filename, 'w');
return gridStore.open();
}).then((gridStore) => {
return gridStore.write(data);
}).then((gridStore) => {
return gridStore.close();
});
}

// Generates and returns the location of a file stored in GridStore for the
// given request and filename
function location(config, req, filename) {
return (req.protocol + '://' + req.get('host') +
path.dirname(req.originalUrl) + '/' + req.config.applicationId +
getFileDataAsync(config, filename) {
return config.database.connect().then(() => {
return GridStore.exist(config.database.db, filename);
}).then(() => {
var gridStore = new GridStore(config.database.db, filename, 'r');
return gridStore.open();
}).then((gridStore) => {
return gridStore.read();
});
}

getFileLocation(config, request, filename) {
return (request.protocol + '://' + request.get('host') +
Path.dirname(request.originalUrl) + '/' + config.applicationId +
'/' + encodeURIComponent(filename));
}
}

module.exports = {
create: create,
get: get,
location: location
};
12 changes: 6 additions & 6 deletions src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
var bodyParser = require('body-parser'),
Config = require('./Config'),
express = require('express'),
FilesAdapter = require('./FilesAdapter'),
middlewares = require('./middlewares.js'),
mime = require('mime'),
Parse = require('parse/node').Parse,
rack = require('hat').rack();

import { getAdapter as getFilesAdapter } from './FilesAdapter';

var router = express.Router();

var processCreate = function(req, res, next) {
Expand Down Expand Up @@ -40,22 +41,21 @@ var processCreate = function(req, res, next) {
}

var filename = rack() + '_' + req.params.filename + extension;
FilesAdapter.getAdapter().create(req.config, filename, req.body)
.then(() => {
getFilesAdapter().createFileAsync(req.config, filename, req.body).then(() => {
res.status(201);
var location = FilesAdapter.getAdapter().location(req.config, req, filename);
var location = getFilesAdapter().getFileLocation(req.config, req, filename);
res.set('Location', location);
res.json({ url: location, name: filename });
}).catch((error) => {
console.log(error);
next(new Parse.Error(Parse.Error.FILE_SAVE_ERROR,
'Could not store file.'));
});
};

var processGet = function(req, res) {
var config = new Config(req.params.appId);
FilesAdapter.getAdapter().get(config, req.params.filename)
.then((data) => {
getFilesAdapter().getFileDataAsync(config, req.params.filename).then((data) => {
res.status(200);
var contentType = mime.lookup(req.params.filename);
res.set('Content-type', contentType);
Expand Down
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ var batch = require('./batch'),
cache = require('./cache'),
DatabaseAdapter = require('./DatabaseAdapter'),
express = require('express'),
FilesAdapter = require('./FilesAdapter'),
S3Adapter = require('./S3Adapter'),
middlewares = require('./middlewares'),
multer = require('multer'),
Parse = require('parse/node').Parse,
PromiseRouter = require('./PromiseRouter'),
httpRequest = require('./httpRequest');

import { setAdapter as setFilesAdapter } from './FilesAdapter';
import { GridStoreAdapter } from './GridStoreAdapter';

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

Expand Down Expand Up @@ -47,7 +49,9 @@ function ParseServer(args) {
DatabaseAdapter.setAdapter(args.databaseAdapter);
}
if (args.filesAdapter) {
FilesAdapter.setAdapter(args.filesAdapter);
setFilesAdapter(args.filesAdapter);
} else {
setFilesAdapter(new GridStoreAdapter());
}
if (args.databaseURI) {
DatabaseAdapter.setAppDatabaseURI(args.appId, args.databaseURI);
Expand Down

0 comments on commit 5ab58fc

Please sign in to comment.