Purge user's files

This commit is contained in:
Pitu 2019-02-28 23:51:59 +09:00
parent c169ab6dc1
commit 9f5a3d15f5
3 changed files with 64 additions and 0 deletions

View File

@ -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;

View File

@ -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];

View File

@ -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);
}
}
}
};