Skip to content

Commit

Permalink
fixed up some of the options handling, and removed an unused class
Browse files Browse the repository at this point in the history
  • Loading branch information
supernomad committed Jul 3, 2015
1 parent c55c458 commit 32d1c0c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 75 deletions.
20 changes: 6 additions & 14 deletions apis/chunked-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,17 @@ var express = require('express'),
var multer = require('multer'),
bodyParser = require('body-parser'),
typeHelper = require('../libs/helpers/typeHelper'),
apiCache = require('../libs/caching/localCache'),
io = require('../libs/io'),
apiCache = null,
uploadRoutes = null,
downloadRoutes = null,
tempChunkPath = __dirname + '/tmp/chunks';

function configure(cache, options) {
if(!typeHelper.isObject(cache)) {
apiCache = require('../libs/caching/localCache');
}
else {
apiCache = cache;
}

function configure(options) {
if(!typeHelper.isObject(options)) {
options = {};
} else if(typeHelper.isString(options.tempChunkPath)) {
tempChunkPath = options.tempChunkPath;
}

uploadRoutes = require('../routes/chunked-upload-routes')(apiCache, io, options);
downloadRoutes = require('../routes/chunked-download-routes')(apiCache, io, options);

// Request parsing
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({extended: false}));
Expand All @@ -37,13 +27,15 @@ function configure(cache, options) {
}));

// Upload routes
var uploadRoutes = require('../routes/chunked-upload-routes')(apiCache, io, options);
router.get(uploadRoutes.get.uri, uploadRoutes.get.handler);
router.post(uploadRoutes.post.uri, uploadRoutes.post.handler);
router.put(uploadRoutes.put.uri, uploadRoutes.put.handler);
router.delete(uploadRoutes.delete.uri, uploadRoutes.delete.handler);
router.use(uploadRoutes.error.handler);

// Download routes
var downloadRoutes = require('../routes/chunked-download-routes')(apiCache, io, options);
router.get(downloadRoutes.get.uri, downloadRoutes.get.handler);
router.post(downloadRoutes.post.uri, downloadRoutes.post.handler);
router.delete(downloadRoutes.delete.uri, downloadRoutes.delete.handler);
Expand Down
13 changes: 0 additions & 13 deletions libs/helpers/errorHelper.js

This file was deleted.

11 changes: 7 additions & 4 deletions routes/chunked-download-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ var async = require('async'),
validators = require('../libs/validators/chunked-download-validators');

var debug = false,
routePrefix = '/chunked/download',
routePrefix = '/chunked',
defaultTtl = 3600,
chunkSize = 1024,
io = null,
dataCache = null;

var routes = {
'get': new apiModels.RouteHandler(routePrefix + '/:downloadId/:index', function (req, res, next) {
'get': new apiModels.RouteHandler(routePrefix + '/download/:downloadId/:index', function (req, res, next) {
var index = parseInt(req.params.index);
if (!guidHelper.isGuid(req.params.downloadId)){
next(errorModels.ValidationError('The supplied downloadId is not a valid v4 GUID'));
Expand Down Expand Up @@ -59,7 +59,7 @@ var routes = {
});
}
}),
'post': new apiModels.RouteHandler(routePrefix, function(req, res, next) {
'post': new apiModels.RouteHandler(routePrefix + '/download', function(req, res, next) {
var valid = validators.validateDownloadRequest(req.body);
if(valid !== validators.valid) {
next(errorModels.ValidationError(valid));
Expand Down Expand Up @@ -92,7 +92,7 @@ var routes = {
});
}
}),
'delete': new apiModels.RouteHandler(routePrefix + '/:downloadId', function (req, res, next) {
'delete': new apiModels.RouteHandler(routePrefix + '/download/:downloadId', function (req, res, next) {
if (!guidHelper.isGuid(req.params.downloadId)){
next(errorModels.ValidationError('The supplied downloadId is not a valid v4 GUID'));
} else {
Expand Down Expand Up @@ -146,6 +146,9 @@ function configure(cache, storage, options) {
if(typeHelper.isNumber(options.chunkSize)) {
chunkSize = options.chunkSize;
}
if(typeHelper.isNumber(options.defaultTtl)) {
defaultTtl = options.defaultTtl;
}
}

io = storage;
Expand Down
18 changes: 12 additions & 6 deletions routes/chunked-upload-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ var async = require('async'),
validators = require('../libs/validators/chunked-upload-validators');

var debug = false,
routePrefix = '/chunked/upload',
routePrefix = '/chunked',
defaultTtl = 3600,
maxSize = 1024 * 1024 * 1024 * 2,
maxSize = 2147483648,
io = null,
dataCache = null;

var routes = {
'get': new apiModels.RouteHandler(routePrefix + '/:uploadId', function (req, res, next) {
'get': new apiModels.RouteHandler(routePrefix + '/upload/:uploadId', function (req, res, next) {
if (!guidHelper.isGuid(req.params.uploadId)) {
next(errorModels.ValidationError('The supplied uploadId is not a valid v4 GUID'));
} else {
Expand All @@ -30,7 +30,7 @@ var routes = {
});
}
}),
'post': new apiModels.RouteHandler(routePrefix, function (req, res, next) {
'post': new apiModels.RouteHandler(routePrefix + '/upload', function (req, res, next) {
var validity = validators.validateUploadRequest(req.body, maxSize);
if(validity !== validators.valid) {
next(errorModels.ValidationError(validity));
Expand Down Expand Up @@ -65,7 +65,7 @@ var routes = {
});
}
}),
'put': new apiModels.RouteHandler(routePrefix + '/:uploadId/:index', function (req, res, next) {
'put': new apiModels.RouteHandler(routePrefix + '/upload/:uploadId/:index', function (req, res, next) {
var validity = validators.validateChunkRequest(req);
var index = parseInt(req.params.index);
if(validity !== validators.valid) {
Expand Down Expand Up @@ -155,7 +155,7 @@ var routes = {
});
}
}),
'delete': new apiModels.RouteHandler(routePrefix + '/:uploadId', function (req, res, next) {
'delete': new apiModels.RouteHandler(routePrefix + '/upload/:uploadId', function (req, res, next) {
if (!guidHelper.isGuid(req.params.uploadId)){
next(errorModels.ValidationError('The supplied uploadId is not a valid v4 GUID'));
} else {
Expand Down Expand Up @@ -205,6 +205,12 @@ function configure(cache, storage, options) {
if(typeHelper.isString(options.routePrefix)){
routePrefix = stringHelper.stripTrailingSlashes(options.routePrefix);
}
if(typeHelper.isNumber(options.maxSize)) {
maxSize = options.maxSize;
}
if(typeHelper.isNumber(options.defaultTtl)) {
defaultTtl = options.defaultTtl;
}
}

io = storage;
Expand Down
38 changes: 0 additions & 38 deletions test/libs/helpers/errorHelper-tests.js

This file was deleted.

0 comments on commit 32d1c0c

Please sign in to comment.