fix: actually delete files/thumbnails when deleting user via API \*\*\*

Files stored on S3 are currently NOT deleted. See #192 for more info.
pull/190/head
tycrek 1 year ago
parent dbcb3dc777
commit 6e242ede6d
No known key found for this signature in database
GPG Key ID: FF8A54DCE404885A

@ -183,14 +183,44 @@ export const deleteUser = (unid: string): Promise<void> => new Promise((resolve,
// Remove the user from the users map // Remove the user from the users map
users.splice(users.indexOf(user), 1); users.splice(users.indexOf(user), 1);
// Save the new user to auth.json // Remove the user's files
const authPath = path('auth.json'); data().get().then((fileData: [string, FileData][]) => new Promise((resolve, reject) => {
const authData = fs.readJsonSync(authPath) as Users;
const userIndex = authData.users.findIndex((user) => user.unid === unid); // Create a queue of functions to run
authData.users.splice(userIndex, 1); const queue = fileData.map(([key, file]) => async () => {
fs.writeJson(authPath, authData, { spaces: '\t' }) if (file.uploader === unid) {
// Delete the file
const p = path(file.path);
await fs.unlink(p);
// Delete the thumbnail
const t = path(`uploads/thumbnails/${file.thumbnail}`);
await fs.unlink(t);
// Delete the info from the datafile
await data().del(key);
}
});
// Recursively run the queue (see note above in `migrate()`)
const runQueue = (index: number) => {
if (index >= queue.length) return resolve(void 0);
queue[index]().then(() => runQueue(index + 1)).catch(reject);
};
runQueue(0);
})
.then(() => {
// Save the new user to auth.json
const authPath = path('auth.json');
const authData = fs.readJsonSync(authPath) as Users;
const userIndex = authData.users.findIndex((user) => user.unid === unid);
authData.users.splice(userIndex, 1);
return fs.writeJson(authPath, authData, { spaces: '\t' })
})
.then(() => resolve()) .then(() => resolve())
.catch(reject); .catch(reject));
}); });
/** /**

Loading…
Cancel
Save