aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPitu <[email protected]>2019-02-28 23:51:59 +0900
committerPitu <[email protected]>2019-02-28 23:51:59 +0900
commit9f5a3d15f55fea03052627f3bd4d97a4284cdf7c (patch)
tree7062ca416d14d0d6863ed0acdc76ce65ece768bb
parentSome stuff (diff)
downloadhost.fuwn.me-9f5a3d15f55fea03052627f3bd4d97a4284cdf7c.tar.xz
host.fuwn.me-9f5a3d15f55fea03052627f3bd4d97a4284cdf7c.zip
Purge user's files
-rw-r--r--src/api/routes/admin/userPurge.js26
-rw-r--r--src/api/utils/Util.js16
-rw-r--r--src/site/pages/dashboard/users.vue22
3 files changed, 64 insertions, 0 deletions
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 @@
<b-switch v-model="props.row.isAdmin"
@input="changeIsAdmin(props.row)" />
</b-table-column>
+
+ <b-table-column field="purge"
+ centered>
+ <button class="button is-primary"
+ @click="promptPurgeFiles(props.row)">Purge files</button>
+ </b-table-column>
</template>
<template slot="empty">
<div class="has-text-centered">
@@ -251,6 +257,22 @@ export default {
} catch (error) {
this.$onPromiseError(error);
}
+ },
+ promptPurgeFiles(row) {
+ this.$dialog.confirm({
+ message: 'Are you sure you want to delete this user\'s files?',
+ onConfirm: () => this.purgeFiles(row)
+ });
+ },
+ async purgeFiles(row) {
+ try {
+ const response = await this.axios.post(`${this.config.baseURL}/admin/users/purge`, {
+ id: row.id
+ });
+ this.$toast.open(response.data.message);
+ } catch (error) {
+ this.$onPromiseError(error);
+ }
}
}
};