Add download-all as tar.gz and style improvements

This commit is contained in:
Christoph Wiechert
2017-04-25 21:52:20 +02:00
parent c7b354386d
commit 377d363770
2 changed files with 31 additions and 18 deletions

View File

@@ -14,6 +14,7 @@ const AES = require("crypto-js/aes");
const MD5 = require("crypto-js/md5");
const debug = require('debug')('psitransfer:main');
const archiver = require('archiver');
const zlib = require('zlib');
const errorPage = fs.readFileSync(path.join(__dirname, '../public/html/error.html')).toString();
const store = new Store(config.uploadDir);
@@ -70,25 +71,28 @@ app.get('/:sid', (req, res, next) => {
});
// Download single file
// Download files
app.get('/files/:fid', async(req, res, next) => {
// let tusboy handle HEAD with Tus Header
// let tusboy handle HEAD requests with Tus Header
if(req.method === 'HEAD' && req.get('Tus-Resumable')) return next();
// Download all files
if(req.params.fid.endsWith('.zip')) {
if(req.params.fid.match(/^[a-z0-9+]+\.(tar\.gz|zip)$/)) {
const sid = req.params.fid.split('++')[0];
const format = req.params.fid.endsWith('.zip') ? 'zip' : 'tar.gz';
const bucket = db.get(sid);
if(req.params.fid !== sid + '++' + MD5(bucket.map(f => f.key).join()).toString() + '.zip') {
if(req.params.fid !== sid + '++' + MD5(bucket.map(f => f.key).join()).toString() + '.' + format) {
res.status(404).send(errorPage.replace('%%ERROR%%', 'Invalid link'));
return;
}
debug(`Download Bucket ${sid}`);
res.header('ContentType', 'application/zip');
res.header('Content-Disposition', 'attachment; filename="' + sid + '.zip"');
if(format === 'zip') res.header('ContentType', 'application/zip');
if(format === 'tar.gz') res.header('ContentType', 'application/x-gtar');
res.header('Content-Disposition', `attachment; filename="${sid}.${format}"`);
const archive = archiver('zip');
const archive = archiver(format === 'zip' ? 'zip' : 'tar');
archive.on('error', function(err) {
console.error(err);
});
@@ -100,7 +104,11 @@ app.get('/files/:fid', async(req, res, next) => {
);
});
archive.pipe(res);
if(format === 'tar.gz') {
archive.pipe(zlib.createGzip()).pipe(res);
} else {
archive.pipe(res);
}
archive.finalize();
try {