Add range support
This commit is contained in:
parent
a204980c82
commit
fdb4c0464e
|
@ -192,6 +192,11 @@ FileSystemProvider.prototype.download = function (options, cb) {
|
|||
var fileOpts = {flags: 'r',
|
||||
autoClose: true };
|
||||
|
||||
if (options.start) {
|
||||
fileOpts.start = options.start
|
||||
fileOpts.end = options.end
|
||||
}
|
||||
|
||||
try {
|
||||
return fs.createReadStream(filePath, fileOpts);
|
||||
} catch (e) {
|
||||
|
|
|
@ -108,7 +108,11 @@ exports.upload = function (provider, req, res, options, cb) {
|
|||
|
||||
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) {
|
||||
uploadParams.acl = file.acl;
|
||||
}
|
||||
|
@ -162,7 +166,18 @@ exports.upload = function (provider, req, res, options, cb) {
|
|||
}
|
||||
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.
|
||||
|
@ -175,24 +190,66 @@ exports.upload = function (provider, req, res, options, cb) {
|
|||
* @header storageService.download(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,
|
||||
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);
|
||||
|
||||
reader.pipe(res);
|
||||
reader.on('error', function(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 });
|
||||
handleError(res, err);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
} else {
|
||||
|
||||
var reader = provider.download(params);
|
||||
|
||||
res.type(file);
|
||||
|
||||
reader.pipe(res);
|
||||
reader.on('error', function(err) {
|
||||
handleError(res, err);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -246,8 +246,8 @@ StorageService.prototype.upload = function(req, res, options, cb) {
|
|||
* @param {Response} res HTTP response
|
||||
* @param {Function} cb Callback function
|
||||
*/
|
||||
StorageService.prototype.download = function (container, file, res, cb) {
|
||||
return handler.download(this.client, null, res, container, file, cb);
|
||||
StorageService.prototype.download = function (container, file, req, res, cb) {
|
||||
return handler.download(this.client, req, res, container, file, cb);
|
||||
};
|
||||
|
||||
StorageService.modelName = 'storage';
|
||||
|
@ -321,6 +321,7 @@ StorageService.prototype.download.shared = true;
|
|||
StorageService.prototype.download.accepts = [
|
||||
{arg: 'container', 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'}}
|
||||
];
|
||||
StorageService.prototype.download.http =
|
||||
|
|
Loading…
Reference in New Issue