mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
refactor: .deleteUpload() to accept array of paths
This commit is contained in:
@@ -11,11 +11,17 @@ const batch = require('../batch');
|
|||||||
|
|
||||||
const md5 = filename => crypto.createHash('md5').update(filename).digest('hex');
|
const md5 = filename => crypto.createHash('md5').update(filename).digest('hex');
|
||||||
const _getFullPath = relativePath => path.resolve(nconf.get('upload_path'), relativePath);
|
const _getFullPath = relativePath => path.resolve(nconf.get('upload_path'), relativePath);
|
||||||
const _validatePath = async (relativePath) => {
|
const _validatePath = async (relativePaths) => {
|
||||||
const fullPath = _getFullPath(relativePath);
|
if (typeof relativePaths === 'string') {
|
||||||
const exists = await file.exists(fullPath);
|
relativePaths = [relativePaths];
|
||||||
|
} else if (!Array.isArray(relativePaths)) {
|
||||||
|
throw new Error(`[[error:wrong-parameter-type, relativePaths, ${typeof relativePaths}, array]]`);
|
||||||
|
}
|
||||||
|
|
||||||
if (!fullPath.startsWith(nconf.get('upload_path')) || !exists) {
|
const fullPaths = relativePaths.map(path => _getFullPath(path));
|
||||||
|
const exists = await Promise.all(fullPaths.map(async fullPath => file.exists(fullPath)));
|
||||||
|
|
||||||
|
if (!fullPaths.every(fullPath => fullPath.startsWith(nconf.get('upload_path'))) || !exists.every(Boolean)) {
|
||||||
throw new Error('[[error:invalid-path]]');
|
throw new Error('[[error:invalid-path]]');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -29,23 +35,35 @@ module.exports = function (User) {
|
|||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
|
|
||||||
User.deleteUpload = async function (callerUid, uid, uploadName) {
|
User.deleteUpload = async function (callerUid, uid, uploadNames) {
|
||||||
|
if (typeof uploadNames === 'string') {
|
||||||
|
uploadNames = [uploadNames];
|
||||||
|
} else if (!Array.isArray(uploadNames)) {
|
||||||
|
throw new Error(`[[error:wrong-parameter-type, uploadNames, ${typeof uploadNames}, array]]`);
|
||||||
|
}
|
||||||
|
|
||||||
const [isUsersUpload, isAdminOrGlobalMod] = await Promise.all([
|
const [isUsersUpload, isAdminOrGlobalMod] = await Promise.all([
|
||||||
db.isSortedSetMember(`uid:${callerUid}:uploads`, uploadName),
|
db.isSortedSetMembers(`uid:${callerUid}:uploads`, uploadNames),
|
||||||
User.isAdminOrGlobalMod(callerUid),
|
User.isAdminOrGlobalMod(callerUid),
|
||||||
]);
|
]);
|
||||||
if (!isAdminOrGlobalMod && !isUsersUpload) {
|
if (!isAdminOrGlobalMod && !isUsersUpload.every(Boolean)) {
|
||||||
throw new Error('[[error:no-privileges]]');
|
throw new Error('[[error:no-privileges]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
await _validatePath(uploadName);
|
await _validatePath(uploadNames);
|
||||||
const fullPath = _getFullPath(uploadName);
|
|
||||||
winston.verbose(`[user/deleteUpload] Deleting ${uploadName}`);
|
await batch.processArray(uploadNames, async (uploadNames) => {
|
||||||
await Promise.all([
|
const fullPaths = uploadNames.map(path => _getFullPath(path));
|
||||||
file.delete(fullPath),
|
|
||||||
file.delete(file.appendToFileName(fullPath, '-resized')),
|
await Promise.all(fullPaths.map(async (fullPath, idx) => {
|
||||||
]);
|
winston.verbose(`[user/deleteUpload] Deleting ${uploadNames[idx]}`);
|
||||||
await db.sortedSetRemove(`uid:${uid}:uploads`, uploadName);
|
await Promise.all([
|
||||||
|
file.delete(fullPath),
|
||||||
|
file.delete(file.appendToFileName(fullPath, '-resized')),
|
||||||
|
]);
|
||||||
|
await db.sortedSetRemove(`uid:${uid}:uploads`, uploadNames[idx]);
|
||||||
|
}));
|
||||||
|
}, { batch: 50 });
|
||||||
};
|
};
|
||||||
|
|
||||||
User.collateUploads = async function (uid, archive) {
|
User.collateUploads = async function (uid, archive) {
|
||||||
|
|||||||
Reference in New Issue
Block a user