test(download): restore download code

This commit is contained in:
OldHawk
2017-11-01 13:39:30 +08:00
parent 8b9b674734
commit c19ef15281

View File

@@ -71,35 +71,18 @@ exports.download = function (req, res) {
fs.exists(filePath, function (exists) {
if (exists) {
var stat = fs.statSync(filePath);
//
//try {
// res.set('Content-Type', 'application/octet-stream');
// res.set('Content-Disposition', 'attachment; filename=' + encodeURI(req.params.filename));
// res.set('Content-Length', stat.size);
//
// fs.createReadStream(filePath).pipe(res);
//} catch (err) {
// res.status(422).send({
// message: 'DOWNLOAD_FAILED'
// });
//}
var options = {
root: path.join(__dirname, '../../../../' + backupConfig.dir),
dotfiles: 'deny',
headers: {
'x-timestamp': Date.now(),
'x-sent': true,
'Content-Disposition': 'attachment; filename=' + encodeURI(req.params.filename)
}
};
try {
res.set('Content-Type', 'application/octet-stream');
res.set('Content-Disposition', 'attachment; filename=' + encodeURI(req.params.filename));
res.set('Content-Length', stat.size);
res.sendFile(req.params.filename, options, function (err) {
if (err) {
console.log(err);
res.status(err.status).end();
}
});
fs.createReadStream(filePath).pipe(res);
} catch (err) {
res.status(422).send({
message: 'DOWNLOAD_FAILED'
});
}
} else {
res.status(422).send({
message: 'FILE_DOES_NOT_EXISTS'