|
|
|
@ -1,50 +1,48 @@ |
|
|
|
|
'use strict'; |
|
|
|
|
|
|
|
|
|
const { remove } = require('fs-extra') |
|
|
|
|
, { Files } = require(__dirname+'/../../db/') |
|
|
|
|
, uploadDirectory = require(__dirname+'/../../helpers/files/uploadDirectory.js') |
|
|
|
|
const { Files } = require(__dirname+'/../../db/') |
|
|
|
|
, deletePostFiles = require(__dirname+'/../../helpers/files/deletepostfiles.js'); |
|
|
|
|
|
|
|
|
|
module.exports = async (posts, unlinkOnly) => { |
|
|
|
|
|
|
|
|
|
//get filenames from all the posts
|
|
|
|
|
let fileNames = []; |
|
|
|
|
let files = []; |
|
|
|
|
for (let i = 0; i < posts.length; i++) { |
|
|
|
|
const post = posts[i]; |
|
|
|
|
if (post.files.length > 0) { |
|
|
|
|
fileNames = fileNames.concat(post.files.map(x => x.filename)); |
|
|
|
|
files = files.concat(post.files.map(file => { |
|
|
|
|
return { |
|
|
|
|
filename: file.filename, |
|
|
|
|
hash: file.hash |
|
|
|
|
}; |
|
|
|
|
})); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
fileNames = [...new Set(fileNames)]; |
|
|
|
|
files = [...new Set(files)]; |
|
|
|
|
|
|
|
|
|
if (fileNames.length == 0) { |
|
|
|
|
if (files.length == 0) { |
|
|
|
|
return { |
|
|
|
|
message: 'No files found' |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (fileNames.length > 0) { |
|
|
|
|
await Files.decrement(fileNames); |
|
|
|
|
if (files.length > 0) { |
|
|
|
|
await Files.decrement(files.map(x => x.filename)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (unlinkOnly) { |
|
|
|
|
return { |
|
|
|
|
message:`Unlinked ${fileNames.length} file(s) across ${posts.length} post(s)`, |
|
|
|
|
message:`Unlinked ${files.length} file(s) across ${posts.length} post(s)`, |
|
|
|
|
action:'$set', |
|
|
|
|
query: { |
|
|
|
|
'files': [] |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} else { |
|
|
|
|
//delete all the files using the filenames
|
|
|
|
|
await Promise.all(fileNames.map(async filename => { |
|
|
|
|
//dont question it.
|
|
|
|
|
return Promise.all([ |
|
|
|
|
remove(`${uploadDirectory}img/${filename}`), |
|
|
|
|
remove(`${uploadDirectory}img/thumb-${filename.split('.')[0]}.jpg`) |
|
|
|
|
]) |
|
|
|
|
})); |
|
|
|
|
//delete all the files
|
|
|
|
|
await deletePostFiles(files); |
|
|
|
|
return { |
|
|
|
|
message:`Deleted ${fileNames.length} file(s) from server`, |
|
|
|
|
message:`Deleted ${files.length} file(s) from server`, |
|
|
|
|
//NOTE: only deletes from selected posts. other posts with same image will 404
|
|
|
|
|
action:'$set', |
|
|
|
|
query: { |
|
|
|
|