Add range support

This commit is contained in:
Raymond Feng 2015-11-23 17:16:48 -08:00
parent a204980c82
commit fdb4c0464e
3 changed files with 92 additions and 29 deletions

View File

@ -192,6 +192,11 @@ FileSystemProvider.prototype.download = function (options, cb) {
var fileOpts = {flags: 'r', var fileOpts = {flags: 'r',
autoClose: true }; autoClose: true };
if (options.start) {
fileOpts.start = options.start
fileOpts.end = options.end
}
try { try {
return fs.createReadStream(filePath, fileOpts); return fs.createReadStream(filePath, fileOpts);
} catch (e) { } catch (e) {

View File

@ -108,7 +108,11 @@ exports.upload = function (provider, req, res, options, cb) {
self.emit('fileBegin', part.name, file); self.emit('fileBegin', part.name, file);
var uploadParams = {container: container, remote: file.name, contentType: file.type}; var uploadParams = {
container: container,
remote: file.name,
contentType: file.type
};
if (file.acl) { if (file.acl) {
uploadParams.acl = file.acl; uploadParams.acl = file.acl;
} }
@ -162,7 +166,18 @@ exports.upload = function (provider, req, res, options, cb) {
} }
cb && cb(err, {files: files, fields: fields}); cb && cb(err, {files: files, fields: fields});
}); });
};
function handleError(res, err) {
if (err.code === 'ENOENT') {
res.type('application/json');
res.status(404).send({error: err});
return;
} }
res.type('application/json');
res.status(500).send({error: err});
}
/** /**
* Handle download from a container/file. * Handle download from a container/file.
@ -175,24 +190,66 @@ exports.upload = function (provider, req, res, options, cb) {
* @header storageService.download(provider, req, res, container, file, cb) * @header storageService.download(provider, req, res, container, file, cb)
*/ */
exports.download = function(provider, req, res, container, file, cb) { exports.download = function(provider, req, res, container, file, cb) {
var reader = provider.download({
var params = {
container: container || req && req.params.container, container: container || req && req.params.container,
remote: file || req && req.params.file remote: file || req && req.params.file
}); };
var range = null;
if (req) {
if (req.headers) {
range = req.headers.range || '';
}
if (range) {
provider.getFile(params.container, params.remote, function(err, stats) {
if (err) {
handleError(res, err);
} else {
var total = stats.size;
var parts = range.replace(/bytes=/, "").split("-")
var partialstart = parts[0]
var partialend = parts[1]
params.start = parseInt(partialstart, 10)
params.end = partialend ? parseInt(partialend, 10) : total - 1
var chunksize = (params.end - params.start) + 1
res.status(206)
res.set("Content-Range", "bytes " + params.start + "-" + params.end + "/" + total);
res.set("Accept-Ranges", "bytes");
res.set("Content-Length", chunksize);
var reader = provider.download(params);
res.type(file); res.type(file);
reader.pipe(res); reader.pipe(res);
reader.on('error', function(err) { reader.on('error', function(err) {
if (err.code === 'ENOENT') { handleError(res, err);
res.type('application/json');
res.status(404).send({ error: err });
return;
}
res.type('application/json');
res.status(500).send({ error: err });
}); });
} }
});
} else {
var reader = provider.download(params);
res.type(file);
reader.pipe(res);
reader.on('error', function(err) {
handleError(res, err);
});
}
}
};

View File

@ -246,8 +246,8 @@ StorageService.prototype.upload = function(req, res, options, cb) {
* @param {Response} res HTTP response * @param {Response} res HTTP response
* @param {Function} cb Callback function * @param {Function} cb Callback function
*/ */
StorageService.prototype.download = function (container, file, res, cb) { StorageService.prototype.download = function (container, file, req, res, cb) {
return handler.download(this.client, null, res, container, file, cb); return handler.download(this.client, req, res, container, file, cb);
}; };
StorageService.modelName = 'storage'; StorageService.modelName = 'storage';
@ -321,6 +321,7 @@ StorageService.prototype.download.shared = true;
StorageService.prototype.download.accepts = [ StorageService.prototype.download.accepts = [
{arg: 'container', type: 'string', 'http': {source: 'path'}}, {arg: 'container', type: 'string', 'http': {source: 'path'}},
{arg: 'file', type: 'string', 'http': {source: 'path'}}, {arg: 'file', type: 'string', 'http': {source: 'path'}},
{arg: 'req', type: 'object', 'http': {source: 'req'}},
{arg: 'res', type: 'object', 'http': {source: 'res'}} {arg: 'res', type: 'object', 'http': {source: 'res'}}
]; ];
StorageService.prototype.download.http = StorageService.prototype.download.http =