Add: fileDownloadedWebhook (Closes #29)

This commit is contained in:
Christoph Wiechert
2018-12-14 17:30:43 +01:00
parent be01f8a278
commit a2e4f0c316
4 changed files with 59 additions and 1 deletions

View File

@@ -38,7 +38,10 @@ const config = {
// set to false to disable logging
"accessLog": ':date[iso] :method :url :status :response-time :remote-addr',
//use to set custom upload url
"uploadAppPath": '/'
"uploadAppPath": '/',
// download notification webhook
// invokes an HTTP POST to this url whenever a file was downloaded
"fileDownloadedWebhook": null
};

View File

@@ -16,6 +16,7 @@ const debug = require('debug')('psitransfer:main');
const archiver = require('archiver');
const zlib = require('zlib');
const _ = require('lodash');
const axios = require('axios');
const errorPage = fs.readFileSync(path.join(__dirname, '../public/html/error.html')).toString();
const store = new Store(config.uploadDir);
@@ -133,6 +134,8 @@ app.get('/files/:fid', async (req, res, next) => {
// let tusboy handle HEAD requests with Tus Header
if (req.method === 'HEAD' && req.get('Tus-Resumable')) return next();
const sid = req.params.fid.split('++')[0];
// Download all files
if (req.params.fid.match(/^[a-z0-9+]+\.(tar\.gz|zip)$/)) {
const format = req.params.fid.endsWith('.zip') ? 'zip' : 'tar.gz';
@@ -178,6 +181,16 @@ app.get('/files/:fid', async (req, res, next) => {
await db.updateLastDownload(info.metadata.sid, info.metadata.key);
}
});
// Trigger fileDownloadedWebhook
if(config.fileDownloadedWebhook) {
axios.post(config.fileDownloadedWebhook, {
fid: sid,
file: '_archive_',
date: Date.now()
}).catch(err => console.error(err));
}
});
}
catch (e) {
@@ -200,6 +213,15 @@ app.get('/files/:fid', async (req, res, next) => {
} else {
await db.updateLastDownload(info.metadata.sid, info.metadata.key);
}
// Trigger fileDownloadedWebhook
if (config.fileDownloadedWebhook) {
axios.post(config.fileDownloadedWebhook, {
bucket: sid,
file: info.metadata.name,
date: Date.now()
}).catch(err => console.error(err));
}
});
}
catch (e) {

32
package-lock.json generated
View File

@@ -88,6 +88,15 @@
"lodash": "^4.17.10"
}
},
"axios": {
"version": "0.18.0",
"resolved": "http://registry.npmjs.org/axios/-/axios-0.18.0.tgz",
"integrity": "sha1-MtU+SFHv3AoRmTts0AB4nXDAUQI=",
"requires": {
"follow-redirects": "^1.3.0",
"is-buffer": "^1.1.5"
}
},
"balanced-match": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
@@ -572,6 +581,24 @@
}
}
},
"follow-redirects": {
"version": "1.5.10",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz",
"integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==",
"requires": {
"debug": "=3.1.0"
},
"dependencies": {
"debug": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
"requires": {
"ms": "2.0.0"
}
}
}
},
"for-each": {
"version": "0.3.2",
"resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.2.tgz",
@@ -707,6 +734,11 @@
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.8.0.tgz",
"integrity": "sha1-6qM9bd16zo9/b+DJygRA5wZzix4="
},
"is-buffer": {
"version": "1.1.6",
"resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz",
"integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w=="
},
"is-callable": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.3.tgz",

View File

@@ -14,6 +14,7 @@
"main": "app.js",
"dependencies": {
"archiver": "^3.0.0",
"axios": "^0.18.0",
"compression": "^1.7.3",
"crypto-js": "^3.1.9-1",
"debug": "^4.1.0",