-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Supernomad/chunked-v2
Chunked v2
- Loading branch information
Showing
29 changed files
with
2,253 additions
and
127 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* global __dirname */ | ||
var express = require('express'), | ||
router = express.Router(); | ||
|
||
var multer = require('multer'), | ||
bodyParser = require('body-parser'), | ||
typeHelper = require.main.require('libs/helpers/typeHelper'), | ||
io = require.main.require('libs/io'), | ||
apiCache = null, | ||
uploadRoutes = null, | ||
downloadRoutes = null, | ||
tempChunkPath = __dirname + '/tmp/chunks'; | ||
|
||
function configure(cache, options) { | ||
if(!typeHelper.isObject(cache)) { | ||
apiCache = require.main.require('libs/caching/localCache'); | ||
} | ||
else { | ||
apiCache = cache; | ||
} | ||
|
||
if(!typeHelper.isObject(options)) { | ||
options = {}; | ||
} | ||
|
||
uploadRoutes = require.main.require('routes/chunked-upload-routes')(apiCache, io, options); | ||
downloadRoutes = require.main.require('routes/chunked-download-routes')(apiCache, io, options); | ||
|
||
// Request parsing | ||
router.use(bodyParser.json()); | ||
router.use(bodyParser.urlencoded({extended: false})); | ||
router.use(multer({ | ||
dest: tempChunkPath, | ||
rename: function (fieldname, filename) { | ||
return filename.replace(/\W+/g, '-').toLowerCase() + '-' + fieldname.replace(/\W+/g, '-').toLowerCase(); | ||
} | ||
})); | ||
|
||
// Upload routes | ||
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 | ||
router.get(downloadRoutes.get.uri, downloadRoutes.get.handler); | ||
router.post(downloadRoutes.post.uri, downloadRoutes.post.handler); | ||
router.delete(downloadRoutes.delete.uri, downloadRoutes.delete.handler); | ||
router.use(downloadRoutes.error.handler); | ||
return router; | ||
} | ||
|
||
module.export = configure; |
This file contains 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
var NodeCache = require('node-cache'), | ||
cache = new NodeCache({ stdTTL: 0, checkperiod: 300, useClones: false }), | ||
typeHelper = require.main.require('libs/helpers/typeHelper'); | ||
|
||
function create(key, val, ttl, callback) { | ||
if(typeHelper.isFunction(callback)) { | ||
cache.set(key, val, ttl, function (error, success) { | ||
callback(error, success); | ||
}); | ||
} else { | ||
return cache.set(key, val, ttl); | ||
} | ||
} | ||
|
||
function restore(key, callback) { | ||
if(typeHelper.isFunction(callback)) { | ||
cache.get(key, function (error, value) { | ||
callback(error, {key: key, value: value}); | ||
}); | ||
} else { | ||
var value = cache.get(key); | ||
return {key: key, value: value}; | ||
} | ||
} | ||
|
||
function update(key, val, ttl, callback) { | ||
return create(key, val, ttl, callback); | ||
} | ||
|
||
function del(key, callback) { | ||
if(typeHelper.isFunction(callback)) { | ||
cache.del(key, function (error, count) { | ||
callback(error, count); | ||
}); | ||
} else { | ||
return cache.del(key); | ||
} | ||
} | ||
|
||
module.exports = { | ||
'create': create, | ||
'restore': restore, | ||
'update': update, | ||
'delete': del | ||
}; |
This file contains 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
function genericErrorHandler(error, debug) { | ||
if(error) { | ||
if(debug) { | ||
console.error(error); | ||
} | ||
throw new Error(JSON.stringify(error)); | ||
} | ||
} | ||
|
||
|
||
module.exports = { | ||
genericErrorHandler: genericErrorHandler | ||
}; |
This file contains 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* global process */ | ||
var typeHelper = require.main.require('libs/helpers/typeHelper'); | ||
|
||
var nanoToSeconds = function(nano) { | ||
return parseFloat(nano) / 1000000000; | ||
}; | ||
|
||
var getFloatingTime = function() { | ||
var time = process.hrtime(); | ||
var seconds = time[0]; | ||
var nanoseconds = time[1]; | ||
return parseFloat(seconds) + nanoToSeconds(nanoseconds); | ||
}; | ||
|
||
function newGuid() { | ||
var d = getFloatingTime(); | ||
var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { | ||
var r = (d + Math.random()*16)%16 | 0; | ||
d = Math.floor(d/16); | ||
return (c==='x' ? r : (r&0x3|0x8)).toString(16); | ||
}); | ||
return guid; | ||
} | ||
|
||
function isGuid(guid) { | ||
return typeHelper.isString(guid) | ||
&& /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[8,9,a,b][0-9a-f]{3}-[0-9a-f]{12}$/ig.test(guid); | ||
} | ||
|
||
module.exports = { | ||
newGuid: newGuid, | ||
isGuid: isGuid | ||
}; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
function GenericError(code, error, message) { | ||
var self = this; | ||
self.Code = code; | ||
self.Error = error; | ||
self.Message = message; | ||
} | ||
|
||
function uploadMissing() { | ||
return new GenericError(404, "Missing Upload Error", "The upload specified could not be found, please check the supplied id and try again."); | ||
} | ||
|
||
function downloadMissing() { | ||
return new GenericError(404, "Missing Download Error", "The download specified could not be found, please check the supplied id and try again."); | ||
} | ||
|
||
function serverError() { | ||
return new GenericError(500, "Internal Server Error", "There has been an internal server error, so things are basically blowing up in our datacenter. We will get back to you in a minute but go ahead and try again."); | ||
} | ||
|
||
function validationError(msg) { | ||
return new GenericError(400, "Validation Error", msg); | ||
} | ||
|
||
module.exports = { | ||
GenericError: GenericError, | ||
UploadMissing: uploadMissing, | ||
DownloadMissing: downloadMissing, | ||
ServerError: serverError, | ||
ValidationError: validationError | ||
}; |
This file contains 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var typeHelper = require.main.require('libs/helpers/typeHelper'); | ||
|
||
var valid = 'valid'; | ||
|
||
function validateDownloadRequest(downloadRequest) { | ||
if(!typeHelper.isObject(downloadRequest)) { | ||
return 'Download request object was not recieved, or is not an object.'; | ||
} else if (!typeHelper.isString(downloadRequest.path)) { | ||
return 'Download path missing or it is not a string.'; | ||
} else { | ||
return valid; | ||
} | ||
} | ||
|
||
module.exports = { | ||
valid: valid, | ||
validateDownloadRequest: validateDownloadRequest | ||
}; |
Oops, something went wrong.