mirror of
https://github.com/timvisee/send.git
synced 2025-10-05 10:29:26 +02:00
implemented download tokens
This commit is contained in:
parent
87d46f7ef5
commit
81e9d81dab
26 changed files with 271 additions and 126 deletions
|
@ -1,53 +1,14 @@
|
|||
const storage = require('../storage');
|
||||
const mozlog = require('../log');
|
||||
const log = mozlog('send.download');
|
||||
const { statDownloadEvent } = require('../amplitude');
|
||||
|
||||
module.exports = async function(req, res) {
|
||||
const id = req.params.id;
|
||||
try {
|
||||
const meta = req.meta;
|
||||
const contentLength = await storage.length(id);
|
||||
const fileStream = await storage.get(id);
|
||||
let cancelled = false;
|
||||
|
||||
req.on('aborted', () => {
|
||||
cancelled = true;
|
||||
fileStream.destroy();
|
||||
});
|
||||
|
||||
const { length, stream } = await storage.get(id);
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'application/octet-stream',
|
||||
'Content-Length': contentLength
|
||||
});
|
||||
fileStream.pipe(res).on('finish', async () => {
|
||||
if (cancelled) {
|
||||
return;
|
||||
}
|
||||
|
||||
const dl = meta.dl + 1;
|
||||
const dlimit = meta.dlimit;
|
||||
const ttl = await storage.ttl(id);
|
||||
statDownloadEvent({
|
||||
id,
|
||||
ip: req.ip,
|
||||
country: req.geo.country,
|
||||
state: req.geo.state,
|
||||
owner: meta.owner,
|
||||
download_count: dl,
|
||||
ttl,
|
||||
agent: req.ua.browser.name || req.ua.ua.substring(0, 6)
|
||||
});
|
||||
try {
|
||||
if (dl >= dlimit) {
|
||||
await storage.kill(id);
|
||||
} else {
|
||||
await storage.incrementField(id, 'dl');
|
||||
}
|
||||
} catch (e) {
|
||||
log.info('StorageError:', id);
|
||||
}
|
||||
'Content-Length': length
|
||||
});
|
||||
stream.pipe(res);
|
||||
} catch (e) {
|
||||
res.sendStatus(404);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue