From 46ecdd5c3876359f93c755a7c3589266a8027559 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Fri, 8 Apr 2022 22:24:10 +0300 Subject: [PATCH] arbitrary file sharing --- server/.gitignore | 1 + server/src/client/endpoints/api/Files.js | 38 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 server/src/client/endpoints/api/Files.js diff --git a/server/.gitignore b/server/.gitignore index b91ecba..d108fc9 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -2,6 +2,7 @@ node_modules logs media thumbnails +files clipIndex.json .env \ No newline at end of file diff --git a/server/src/client/endpoints/api/Files.js b/server/src/client/endpoints/api/Files.js new file mode 100644 index 0000000..d8bc4c3 --- /dev/null +++ b/server/src/client/endpoints/api/Files.js @@ -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; \ No newline at end of file