2018-04-09 20:03:33 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
const path = require('path');
|
|
|
|
|
const nconf = require('nconf');
|
|
|
|
|
const winston = require('winston');
|
2018-04-09 20:03:33 -04:00
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
const db = require('../database');
|
|
|
|
|
const file = require('../file');
|
|
|
|
|
const batch = require('../batch');
|
2018-04-09 20:03:33 -04:00
|
|
|
|
|
|
|
|
module.exports = function (User) {
|
2019-07-16 20:44:00 -04:00
|
|
|
User.deleteUpload = async function (callerUid, uid, uploadName) {
|
|
|
|
|
const [isUsersUpload, isAdminOrGlobalMod] = await Promise.all([
|
|
|
|
|
db.isSortedSetMember('uid:' + callerUid + ':uploads', uploadName),
|
|
|
|
|
User.isAdminOrGlobalMod(callerUid),
|
|
|
|
|
]);
|
|
|
|
|
if (!isAdminOrGlobalMod && !isUsersUpload) {
|
|
|
|
|
throw new Error('[[error:no-privileges]]');
|
|
|
|
|
}
|
2018-04-09 20:03:33 -04:00
|
|
|
|
2020-01-19 11:56:13 -05:00
|
|
|
if (uploadName.startsWith('.')) {
|
|
|
|
|
throw new Error('[[error:invalid-path]]');
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-16 20:44:00 -04:00
|
|
|
winston.verbose('[user/deleteUpload] Deleting ' + uploadName);
|
|
|
|
|
await Promise.all([
|
|
|
|
|
file.delete(path.join(nconf.get('upload_path'), uploadName)),
|
|
|
|
|
file.delete(path.join(nconf.get('upload_path'), path.dirname(uploadName), path.basename(uploadName, path.extname(uploadName)) + '-resized' + path.extname(uploadName))),
|
|
|
|
|
]);
|
|
|
|
|
await db.sortedSetRemove('uid:' + uid + ':uploads', uploadName);
|
2018-04-09 20:03:33 -04:00
|
|
|
};
|
2018-04-13 11:58:31 -04:00
|
|
|
|
2019-07-16 20:44:00 -04:00
|
|
|
User.collateUploads = async function (uid, archive) {
|
|
|
|
|
await batch.processSortedSet('uid:' + uid + ':uploads', function (files, next) {
|
2018-04-13 11:58:31 -04:00
|
|
|
files.forEach(function (file) {
|
|
|
|
|
archive.file(path.join(nconf.get('upload_path'), file), {
|
|
|
|
|
name: path.basename(file),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setImmediate(next);
|
2019-07-16 21:01:07 -04:00
|
|
|
}, { batch: 100 });
|
2018-04-13 11:58:31 -04:00
|
|
|
};
|
2018-04-09 20:03:33 -04:00
|
|
|
};
|