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

@@ -20,10 +20,15 @@
| decrypt | decrypt
.panel.panel-primary(v-if='!needsPassword') .panel.panel-primary(v-if='!needsPassword')
.panel-heading .panel-heading
a.pull-right(style="color:#fff", @click="downloadAll", v-if="downloadsAvailable") strong Files
i.fa.fa-fw.fa-download div.pull-right(style="margin-top:-5px;")
| Download ZIP span.btn-group
| Files a.btn.btn-sm.btn-default(@click="downloadAll('zip')", title="Archive download is not resumeable!")
i.fa.fa-fw.fa-fw.fa-download
| zip
a.btn.btn-sm.btn-default(@click="downloadAll('tar.gz')", title="Archive download is not resumeable!")
i.fa.fa-fw.fa-fw.fa-download
| tar.gz
.panel-body .panel-body
table.table.table-hover.table-striped(style='margin-bottom: 0') table.table.table-hover.table-striped(style='margin-bottom: 0')
tbody tbody
@@ -70,7 +75,7 @@
computed: { computed: {
downloadsAvailable: function() { downloadsAvailable: function() {
return this.files.some(f => !f.downloaded || f.metadata.retention !== 'one-time') return this.files.length > 1 && this.files.some(f => !f.downloaded || f.metadata.retention !== 'one-time')
} }
}, },
@@ -84,14 +89,14 @@
file.downloaded = true; file.downloaded = true;
}, },
downloadAll() { downloadAll(format) {
document.location.href = document.location.protocol + '//' + document.location.host document.location.href = document.location.protocol + '//' + document.location.host
+ '/files/' + this.sid + '++' + '/files/' + this.sid + '++'
+ MD5( + MD5(
this.files this.files
.filter(f => !f.downloaded || f.metadata.retention !== 'one-time') .filter(f => !f.downloaded || f.metadata.retention !== 'one-time')
.map(f => f.key).join() .map(f => f.key).join()
).toString() + '.zip'; ).toString() + '.' + format;
this.files.forEach(f => { this.files.forEach(f => {
f.downloaded = true; f.downloaded = true;

View File

@@ -14,6 +14,7 @@ const AES = require("crypto-js/aes");
const MD5 = require("crypto-js/md5"); const MD5 = require("crypto-js/md5");
const debug = require('debug')('psitransfer:main'); const debug = require('debug')('psitransfer:main');
const archiver = require('archiver'); const archiver = require('archiver');
const zlib = require('zlib');
const errorPage = fs.readFileSync(path.join(__dirname, '../public/html/error.html')).toString(); const errorPage = fs.readFileSync(path.join(__dirname, '../public/html/error.html')).toString();
const store = new Store(config.uploadDir); 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) => { 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(); if(req.method === 'HEAD' && req.get('Tus-Resumable')) return next();
// Download all files // 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 sid = req.params.fid.split('++')[0];
const format = req.params.fid.endsWith('.zip') ? 'zip' : 'tar.gz';
const bucket = db.get(sid); 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')); res.status(404).send(errorPage.replace('%%ERROR%%', 'Invalid link'));
return; return;
} }
debug(`Download Bucket ${sid}`); debug(`Download Bucket ${sid}`);
res.header('ContentType', 'application/zip'); if(format === 'zip') res.header('ContentType', 'application/zip');
res.header('Content-Disposition', 'attachment; filename="' + sid + '.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) { archive.on('error', function(err) {
console.error(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(); archive.finalize();
try { try {