Add maxAge to expire obsolete one-time downloads

This commit is contained in:
Christoph Wiechert
2017-05-09 01:16:14 +02:00
parent 6e569924d7
commit 5f751d927b
2 changed files with 11 additions and 4 deletions

View File

@@ -10,7 +10,7 @@ const config = {
"port": 3000,
"iface": '0.0.0.0',
// retention options in seconds:label
retentions: {
"retentions": {
"one-time": "one time download",
"3600": "1 Hour",
"21600": "6 Hours",
@@ -22,6 +22,8 @@ const config = {
"4838400": "8 Weeks"
},
"defaultRetention": 604800,
// expire every file after maxAge (eg never downloaded one-time files)
"maxAge": 3600*24*75, // 75 days
// maximum file-size for previews in byte
"maxPreviewSize": Math.pow(2,20) * 2, // 2MB
"mailTemplate": 'mailto:?subject=File Transfer&body=You can download the files here: %%URL%%',

View File

@@ -2,6 +2,7 @@
const fsp = require('fs-promise');
const path = require('path');
const debug = require('debug')('psitransfer:db');
const config = require('../config');
module.exports = class DB {
@@ -18,10 +19,14 @@ module.exports = class DB {
let sid,f,expires;
for (sid of Object.keys(this.db)) {
for (f of this.db[sid]) {
// no removal of one-time downloads
if(!Number.isInteger(+f.metadata.retention)) return;
// expire on maxAge
expires = (+f.metadata.createdAt) + (config.maxAge * 1000) - Date.now();
// respect one-time downloads
if(expires > 0 && Number.isInteger(+f.metadata.retention)) {
expires = (+f.metadata.createdAt) + (+f.metadata.retention * 1000) - Date.now();
}
expires = (+f.metadata.createdAt) + (+f.metadata.retention * 1000) - Date.now();
if(expires <= 0) {
debug(`Expired ${sid}++${f.key}`);
this.remove(sid, f.key).catch(e => console.error(e));