arbitrary file sharing

This commit is contained in:
Erik 2022-04-08 22:24:10 +03:00
parent c4630633b8
commit 46ecdd5c38
Signed by: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB
2 changed files with 39 additions and 0 deletions

1
server/.gitignore vendored
View File

@ -2,6 +2,7 @@ node_modules
logs logs
media media
thumbnails thumbnails
files
clipIndex.json clipIndex.json
.env .env

View File

@ -0,0 +1,38 @@
// Endpoint for hosting arbitrary files
const { APIEndpoint } = require('../../interfaces');
const fs = require('fs');
const path = require('path');
class FilesEndpoint extends APIEndpoint {
constructor(client, opts) {
super(client, {
name: 'files',
path: '/files/:filename',
...opts
});
this.methods = [
['get', this.get.bind(this)]
];
this.init();
}
async get(req, res) {
const { filename } = req.params;
const _path = path.join(process.cwd(), 'files');
if (!fs.existsSync(_path)) return res.status(500).send('File serving not set up');
const files = fs.readdirSync(_path);
if (!filename || !files.includes(filename)) return res.status(404).end();
res.sendFile(`${filename}`, { root: _path });
}
}
module.exports = FilesEndpoint;