From 9f5a3d15f55fea03052627f3bd4d97a4284cdf7c Mon Sep 17 00:00:00 2001 From: Pitu Date: Thu, 28 Feb 2019 23:51:59 +0900 Subject: [PATCH] Purge user's files --- src/api/routes/admin/userPurge.js | 26 ++++++++++++++++++++++++++ src/api/utils/Util.js | 16 ++++++++++++++++ src/site/pages/dashboard/users.vue | 22 ++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 src/api/routes/admin/userPurge.js diff --git a/src/api/routes/admin/userPurge.js b/src/api/routes/admin/userPurge.js new file mode 100644 index 0000000..90f6ec9 --- /dev/null +++ b/src/api/routes/admin/userPurge.js @@ -0,0 +1,26 @@ +const Route = require('../../structures/Route'); +const Util = require('../../utils/Util'); + +class userDemote extends Route { + constructor() { + super('/admin/users/purge', 'post', { adminOnly: true }); + } + + async run(req, res) { + if (!req.body) return res.status(400).json({ message: 'No body provided' }); + const { id } = req.body; + if (!id) return res.status(400).json({ message: 'No id provided' }); + + try { + await Util.deleteAllFilesFromUser(id); + } catch (error) { + return super.error(res, error); + } + + return res.json({ + message: 'Successfully deleted the user\'s files' + }); + } +} + +module.exports = userDemote; diff --git a/src/api/utils/Util.js b/src/api/utils/Util.js index 26edf4b..9e9753c 100644 --- a/src/api/utils/Util.js +++ b/src/api/utils/Util.js @@ -90,6 +90,10 @@ class Util { } static constructFilePublicLink(file) { + /* + TODO: This wont work without a reverse proxy serving both + the site and the API under the same domain. Pls fix. + */ file.url = `${process.env.DOMAIN}/${file.name}`; const thumb = this.getFileThumbnail(file.name); if (thumb) { @@ -175,6 +179,18 @@ class Util { } } + static async deleteAllFilesFromUser(id) { + try { + const files = await db.table('files').where({ userId: id }); + for (const file of files) { + await jetpack.removeAsync(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, file)); + } + await db.table('files').where({ userId: id }).delete(); + } catch (error) { + log.error(error); + } + } + static isAuthorized(req) { if (!req.headers.authorization) return false; const token = req.headers.authorization.split(' ')[1]; diff --git a/src/site/pages/dashboard/users.vue b/src/site/pages/dashboard/users.vue index c930876..59a75d3 100644 --- a/src/site/pages/dashboard/users.vue +++ b/src/site/pages/dashboard/users.vue @@ -171,6 +171,12 @@ + + + +