feat: DELETE /:name endpoint

This commit is contained in:
b1ek 2024-05-11 21:34:44 +10:00
parent 89e7e3ded4
commit 71d635fcb1
Signed by: blek
GPG Key ID: 14546221E3595D0C
4 changed files with 31 additions and 1 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
node_modules
dist
store

28
src/routes/delete.ts Normal file
View File

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

View File

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

View File

@ -1 +0,0 @@
uwu