apps: make downloadFile async

This commit is contained in:
Girish Ramakrishnan
2021-10-21 15:25:15 -07:00
parent 2e3070a5c6
commit 010024dfd7
2 changed files with 72 additions and 68 deletions

View File

@@ -798,26 +798,26 @@ async function uploadFile(req, res, next) {
next(new HttpSuccess(202, {}));
}
function downloadFile(req, res, next) {
async function downloadFile(req, res, next) {
assert.strictEqual(typeof req.app, 'object');
if (typeof req.query.file !== 'string' || !req.query.file) return next(new HttpError(400, 'file query argument must be provided'));
req.clearTimeout();
apps.downloadFile(req.app, req.query.file, function (error, stream, info) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(apps.downloadFile(req.app, req.query.file));
if (error) return next(BoxError.toHttpError(error));
var headers = {
'Content-Type': 'application/octet-stream',
'Content-Disposition': `attachment; filename*=utf-8''${encodeURIComponent(info.filename)}` // RFC 2184 section 4
};
if (info.size) headers['Content-Length'] = info.size;
const { stream, filename, size } = result;
const headers = {
'Content-Type': 'application/octet-stream',
'Content-Disposition': `attachment; filename*=utf-8''${encodeURIComponent(filename)}` // RFC 2184 section 4
};
if (size) headers['Content-Length'] = size;
res.writeHead(200, headers);
res.writeHead(200, headers);
stream.pipe(res);
});
stream.pipe(res);
}
async function setMounts(req, res, next) {