From bdaaf852a73456c765aa53402ffe1d86d8f1480c Mon Sep 17 00:00:00 2001 From: tycrek Date: Mon, 5 Apr 2021 17:54:56 -0600 Subject: [PATCH] added support for file deletion --- README.md | 2 +- ass.js | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3db1453..81116fa 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,6 @@ A Not Shitty ShareX Upload Server That Actually Works As Intended (Pronounced "a - [ ] Upload videos - [ ] Upload files - [ ] Thumbnail support -- [ ] Delete support +- [x] Delete support - [ ] Multiple database types (JSON, Mongo, MySQL, PostgreSQL, etc.) - [ ] Multiple access types (original, mixed-case alphanumeric, [ZWS](https://zws.im)) \ No newline at end of file diff --git a/ass.js b/ass.js index 9b30461..a4961ec 100644 --- a/ass.js +++ b/ass.js @@ -57,7 +57,7 @@ function startup() { data[resourceId] = req.file; saveData(data); - res.type('text').send(`http://lh:${process.env.PORT}/${resourceId}`); + res.type('json').send({ resource: `http://lh:${process.env.PORT}/${resourceId}`, delete: `http://lh:${process.env.PORT}/delete/${req.file.filename}` }); }); // View file @@ -67,5 +67,22 @@ function startup() { else res.sendStatus(404); }); - app.listen(process.env.PORT, () => console.log(`Server started on port ${process.env.PORT}`)); + // Delete file + app.get('/delete/:filename', (req, res) => { + let filename = req.params.filename; + let resourceId = Object.keys(data)[Object.values(data).indexOf(Object.values(data).find((d) => d.filename == filename))]; + + if (!resourceId || !data[resourceId]) return res.sendStatus(400); + + log(`Deleted: ${data[resourceId].originalname} (${data[resourceId].mimetype})`); + + // Save the file information + fs.rmSync(path(data[resourceId].path)); + delete data[resourceId]; + saveData(data); + + res.type('text').send('File has been deleted!'); + }) + + app.listen(process.env.PORT, () => log(`Server started on port ${process.env.PORT}\nAuthorized tokens: ${tokens.length}\nAvailable files: ${Object.keys(data).length}`)); } \ No newline at end of file