From 71d635fcb109c69762d1cf5958f5127d6b8a9ffc Mon Sep 17 00:00:00 2001 From: b1ek Date: Sat, 11 May 2024 21:34:44 +1000 Subject: [PATCH] feat: DELETE /:name endpoint --- .gitignore | 1 + src/routes/delete.ts | 28 ++++++++++++++++++++++++++++ src/routes/index.ts | 2 ++ store/izz-testy | 1 - 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/routes/delete.ts delete mode 100644 store/izz-testy diff --git a/.gitignore b/.gitignore index f06235c..1af2303 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules dist +store diff --git a/src/routes/delete.ts b/src/routes/delete.ts new file mode 100644 index 0000000..8fd3f11 --- /dev/null +++ b/src/routes/delete.ts @@ -0,0 +1,28 @@ +import { type FastifyPluginAsync } from 'fastify'; +import fsp from 'node:fs/promises'; + +import { Config, config } from '../config.js'; +import { InvalidAuthorization } from '../errors.js'; +import { getFilesFor, getPathFor } from '../store.js'; + +export default (async function (fastify) { + fastify.delete('/:name', async (req) => { + const key = req.headers.authorization?.replace('Bearer ', '') ?? 'none'; + + const client = Config.getClientByKey(config, key); + if (!client) { + throw InvalidAuthorization(); + } + + const params = req.params as { name: string }; + const files = await getFilesFor(client.name); + + if (!files.find((x) => x === client.name + '-' + params.name)) { + return { status: 'ok' }; + } + + await fsp.rm(getPathFor(params.name, client.name)); + + return { status: 'ok' }; + }); +} as FastifyPluginAsync); diff --git a/src/routes/index.ts b/src/routes/index.ts index c7602e2..c68c704 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -1,9 +1,11 @@ import { FastifyPluginAsync } from 'fastify'; +import deleter from './delete.js'; import upload from './upload.js'; import list from './list.js'; import get from './get.js'; export default (async function (fastify) { + await fastify.register(deleter); await fastify.register(upload); await fastify.register(list); await fastify.register(get); diff --git a/store/izz-testy b/store/izz-testy deleted file mode 100644 index ea844c1..0000000 --- a/store/izz-testy +++ /dev/null @@ -1 +0,0 @@ -uwu \ No newline at end of file