From fbf6e6a96f3d96dceef576778951de894487d803 Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Thu, 16 Aug 2018 15:18:02 +0200 Subject: [PATCH] Fixed #1: Start expiration only after first upload --- lib/api/token.js | 3 +- lib/api/upload.js | 7 +++- lib/repository/code.js | 58 ++++++++++++++++++++++++++- public/src/locale/en.js | 3 +- public/src/locale/nl.js | 3 +- public/src/route/Upload.vue | 37 ++++++++++++----- public/src/route/admin/CodeDetail.vue | 18 +++++---- 7 files changed, 103 insertions(+), 26 deletions(-) diff --git a/lib/api/token.js b/lib/api/token.js index 68d6982..09697de 100644 --- a/lib/api/token.js +++ b/lib/api/token.js @@ -20,8 +20,7 @@ module.exports = (repository) => { jwt.sign({ codeId: req.body.code, - codeUserId: code.userId, - codeExpirationTime: code.expirationDate !== null ? code.expirationDate.getTime() : null + codeUserId: code.userId }, config.jwtSecret, (err, token) => { if (err) diff --git a/lib/api/upload.js b/lib/api/upload.js index 330f7a1..5b80d14 100644 --- a/lib/api/upload.js +++ b/lib/api/upload.js @@ -57,7 +57,8 @@ module.exports = (repository, tusServer) => let info = { message: null, - expirationDate: code.expirationDate !== null ? code.expirationDate.getTime() : null + expirationDate: code.expirationDate, + expiration: code.expiration }; if (code.messageHTML) @@ -107,11 +108,13 @@ module.exports = (repository, tusServer) => return; } + let expirationDate = await repository.codes.getUploadExpirationDate(decoded.codeId); + var uploadId = await repository.uploads.insert({ userId: decoded.codeUserId, codeId: decoded.codeId, files: req.body.files, - expirationDate: decoded.codeExpirationTime !== null ? new Date(decoded.codeExpirationTime) : null + expirationDate: expirationDate }); await repository.notifications.insert({ diff --git a/lib/repository/code.js b/lib/repository/code.js index f06962e..c946cd4 100644 --- a/lib/repository/code.js +++ b/lib/repository/code.js @@ -15,6 +15,7 @@ class Code self.id = values.id || values._id || null; self.userId = values.userId || null; self.created = values.created || new Date(); + self.firstUpload = values.firstUpload || null; self.expiration = values.expiration || null; self.expirationDate = values.expirationDate || null; self.description = values.description || null; @@ -65,8 +66,9 @@ class CodeRepository _id: codeId, userId: code.userId, created: code.created || now, + firstUpload: code.firstUpload, expiration: code.expiration, - expirationDate: ExpirationUnits.apply(code.expiration, code.created), + expirationDate: code.expirationDate, description: code.description, message: code.message, messageHTML: self.getMessageHTML(code.message), @@ -95,7 +97,8 @@ class CodeRepository { self.store.update({ _id: code.id }, { $set: { expiration: code.expiration, - expirationDate: ExpirationUnits.apply(code.expiration, code.created), + firstUpload: code.firstUpload, + expirationDate: code.firstUpload != null && code.expiration != null ? ExpirationUnits.apply(code.expiration, code.firstUpload) : null, description: code.description, message: code.message, messageHTML: self.getMessageHTML(code.message) @@ -119,6 +122,57 @@ class CodeRepository } + getUploadExpirationDate(codeId) + { + var self = this; + + return new Promise((resolve, reject) => + { + self.store.findOne({ _id: codeId }, (err, doc) => + { + if (err) + { + reject(err); + return; + } + + if (doc !== null) + { + if (doc.firstUpload == null) + { + let now = new Date(); + let expirationDate = doc.expiration != null ? ExpirationUnits.apply(doc.expiration, now) : null; + + self.store.update({ _id: codeId }, { $set: { + firstUpload: now, + expirationDate: expirationDate + }}, + (err, numAffected) => + { + if (err) + { + reject(err); + return; + } + + if (numAffected == 0) + { + reject(); + } + + resolve(expirationDate); + }); + } + else + resolve(doc.expirationDate); + } + else + resolve(null); + }); + }); + } + + move(codeId, userId) { var self = this; diff --git a/public/src/locale/en.js b/public/src/locale/en.js index 542540d..51458c2 100644 --- a/public/src/locale/en.js +++ b/public/src/locale/en.js @@ -22,7 +22,8 @@ export default { uploadDisclaimer: '', expirationNotice: { never: '', - ever: 'Files uploaded for this invite code will be automatically deleted on {date} at {time}' + date: 'Files uploaded for this invite code will be automatically deleted on {date} at {time}', + timespan: 'Files uploaded for this invite code will be automatically deleted after {expiration}' }, expirationValues: { diff --git a/public/src/locale/nl.js b/public/src/locale/nl.js index 8fedc40..e797fde 100644 --- a/public/src/locale/nl.js +++ b/public/src/locale/nl.js @@ -22,7 +22,8 @@ export default { uploadDisclaimer: '', expirationNotice: { never: '', - ever: 'Bestanden die worden geupload voor deze code worden automatisch verwijderd op {date} om {time}' + date: 'Bestanden die worden geupload voor deze code worden automatisch verwijderd op {date} om {time}', + timespan: 'Bestanden die worden geupload voor deze code worden automatisch verwijderd na {expiration}' }, expirationValues: { diff --git a/public/src/route/Upload.vue b/public/src/route/Upload.vue index aaec57f..7f3633f 100644 --- a/public/src/route/Upload.vue +++ b/public/src/route/Upload.vue @@ -10,7 +10,7 @@
- {{ expirationDate === null ? $t('expirationNotice.never') : $t('expirationNotice.ever', getExpirationNoticeDateTime()) }} + {{ getExpirationNotice() }}
@@ -40,7 +40,8 @@ export default { return { uploadToken: shared.uploadToken, message: null, - expirationDate: null + expirationDate: null, + expiration: null } }, @@ -64,6 +65,7 @@ export default { { self.message = response.data.message; self.expirationDate = response.data.expirationDate !== null ? new Date(response.data.expirationDate) : null; + self.expiration = response.data.expiration; }); }, @@ -138,21 +140,34 @@ export default { { var self = this; - if (self.expirationDate === null) - return self.$i18n.t('expirationNotice.never') != ''; - else - return self.$i18n.t('expirationNotice.ever') != ''; + if (self.expirationDate != null) + return self.$i18n.t('expirationNotice.date') != ''; + + if (self.expiration != null) + return self.$i18n.t('expirationNotice.timespan') != ''; + + return self.$i18n.t('expirationNotice.never') != ''; }, - getExpirationNoticeDateTime() + getExpirationNotice() { var self = this; - return { - date: self.$options.filters.formatDate(self.expirationDate), - time: self.$options.filters.formatTime(self.expirationDate) - }; + if (self.expirationDate != null) + return self.$i18n.t('expirationNotice.date', { + date: self.$options.filters.formatDate(self.expirationDate), + time: self.$options.filters.formatTime(self.expirationDate) + }); + + if (self.expiration != null) + return self.$i18n.t('expirationNotice.timespan', { + expiration: self.$tc('expirationValues.' + self.expiration.units, + self.expiration.value, + { count: self.expiration.value }) + }); + + return self.$i18n.t('expirationNotice.never'); } } } diff --git a/public/src/route/admin/CodeDetail.vue b/public/src/route/admin/CodeDetail.vue index 4382734..b20fb6c 100644 --- a/public/src/route/admin/CodeDetail.vue +++ b/public/src/route/admin/CodeDetail.vue @@ -44,10 +44,14 @@ - + + +
+ +
- {{ $t('admin.codes.detail.expirationHint', { valueUnits: getExpirationValueUnits() }) }} + {{ $t('admin.codes.detail.expirationHint', { valueUnits: getExpirationValueUnits(maxExpiration) }) }}
@@ -210,16 +214,16 @@ export default { }, - getExpirationValueUnits() + getExpirationValueUnits(expiration) { var self = this; - if (self.maxExpiration === null) + if (expiration === null) return ''; - return self.$tc('expirationValues.' + self.maxExpiration.units, - self.maxExpiration.value, - { count: self.maxExpiration.value }); + return self.$tc('expirationValues.' + expiration.units, + expiration.value, + { count: expiration.value }); } } }