mirror of https://gitgud.io/fatchan/jschan.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.1 KiB
55 lines
1.1 KiB
'use strict';
|
|
|
|
const { Files } = require(__dirname+'/../../db/')
|
|
, deletePostFiles = require(__dirname+'/../../helpers/files/deletepostfiles.js');
|
|
|
|
module.exports = async (posts, unlinkOnly) => {
|
|
|
|
//get filenames from all the posts
|
|
let files = [];
|
|
for (let i = 0; i < posts.length; i++) {
|
|
const post = posts[i];
|
|
if (post.files.length > 0) {
|
|
files = files.concat(post.files.map(file => {
|
|
return {
|
|
filename: file.filename,
|
|
hash: file.hash,
|
|
thumbextension: file.thumbextension
|
|
};
|
|
}));
|
|
}
|
|
}
|
|
files = [...new Set(files)];
|
|
|
|
if (files.length == 0) {
|
|
return {
|
|
message: 'No files found'
|
|
};
|
|
}
|
|
|
|
if (files.length > 0) {
|
|
await Files.decrement(files.map(x => x.filename));
|
|
}
|
|
|
|
if (unlinkOnly) {
|
|
return {
|
|
message:`Unlinked ${files.length} file(s) across ${posts.length} post(s)`,
|
|
action:'$set',
|
|
query: {
|
|
'files': []
|
|
}
|
|
};
|
|
} else {
|
|
//delete all the files
|
|
await deletePostFiles(files);
|
|
return {
|
|
message:`Deleted ${files.length} file(s) from server`,
|
|
//NOTE: only deletes from selected posts. other posts with same image will 404
|
|
action:'$set',
|
|
query: {
|
|
'files': []
|
|
}
|
|
};
|
|
}
|
|
|
|
}
|
|
|