mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-06 14:05:46 +01:00
fix: #10144, automatically delete uploads from disk on post purge, ACP option to keep uploads on disk if desired
This commit is contained in:
@@ -11,6 +11,7 @@ const db = require('../database');
|
||||
const image = require('../image');
|
||||
const topics = require('../topics');
|
||||
const file = require('../file');
|
||||
const meta = require('../meta');
|
||||
|
||||
module.exports = function (Posts) {
|
||||
Posts.uploads = {};
|
||||
@@ -117,15 +118,35 @@ module.exports = function (Posts) {
|
||||
}
|
||||
|
||||
const bulkRemove = filePaths.map(path => [`upload:${md5(path)}:pids`, pid]);
|
||||
await Promise.all([
|
||||
const promises = [
|
||||
db.sortedSetRemove(`post:${pid}:uploads`, filePaths),
|
||||
db.sortedSetRemoveBulk(bulkRemove),
|
||||
]);
|
||||
];
|
||||
|
||||
if (!meta.config.preserveOrphanedUploads) {
|
||||
const deletePaths = (await Promise.all(
|
||||
filePaths.map(async filePath => (await Posts.uploads.isOrphan(filePath) ? filePath : false))
|
||||
)).filter(Boolean);
|
||||
promises.push(Posts.uploads.deleteFromDisk(deletePaths));
|
||||
}
|
||||
|
||||
await Promise.all(promises);
|
||||
};
|
||||
|
||||
Posts.uploads.dissociateAll = async (pid) => {
|
||||
const current = await Posts.uploads.list(pid);
|
||||
await Promise.all(current.map(async path => await Posts.uploads.dissociate(pid, path)));
|
||||
await Posts.uploads.dissociate(pid, current);
|
||||
};
|
||||
|
||||
Posts.uploads.deleteFromDisk = async (filePaths) => {
|
||||
if (typeof filePaths === 'string') {
|
||||
filePaths = [filePaths];
|
||||
} else if (!Array.isArray(filePaths)) {
|
||||
throw new Error(`[[error:wrong-parameter-type, filePaths, ${typeof filePaths}, array]]`);
|
||||
}
|
||||
|
||||
filePaths = (await _filterValidPaths(filePaths)).map(_getFullPath);
|
||||
await Promise.all(filePaths.map(file.delete));
|
||||
};
|
||||
|
||||
Posts.uploads.saveSize = async (filePaths) => {
|
||||
|
||||
Reference in New Issue
Block a user