feat: /get/:name endpoint

This commit is contained in:
b1ek 2024-05-11 21:21:50 +10:00
parent 5e1c610614
commit 89e7e3ded4
Signed by: blek
GPG Key ID: 14546221E3595D0C
4 changed files with 51 additions and 8 deletions

View File

@ -27,9 +27,20 @@ export const InvalidAuthorization = () =>
message: 'Provided authorization credentials are invalid',
}) as FastifyError;
export const QuotaExceeded = () => ({
export const QuotaExceeded = () =>
({
code: '422',
statusCode: 422,
name: 'QuotaExceeded',
message: 'Your quota has exceeded. Please delete some files to proceed.',
});
message:
'Your quota has exceeded. Please delete some files to proceed.',
}) as FastifyError;
export const NotFoundError = () =>
({
code: '404',
statusCode: 404,
name: 'NotFound',
message:
'The requested resource has never existed, deleted or has a different name or path.',
}) as FastifyError;

30
src/routes/get.ts Normal file
View File

@ -0,0 +1,30 @@
import { FastifyPluginAsync } from 'fastify';
import fsp from 'node:fs/promises';
import { Config, config } from '../config.js';
import { InvalidAuthorization, NotFoundError } from '../errors.js';
import { getFilesFor, getPathFor } from '../store.js';
export default (async function (fastify) {
fastify.get('/get/:name', async (req, rep) => {
const key = req.headers.authorization?.replace('Bearer ', '') ?? 'none';
const client = Config.getClientByKey(config, key);
if (!client) {
throw InvalidAuthorization();
}
const files = (await getFilesFor(client.name)).map((x) =>
x.replace(client.name + '-', ''),
);
const params = req.params as { name: string };
if (!files.find((x) => x === params.name)) {
throw NotFoundError();
}
rep.type('application/octet-stream');
return await fsp.readFile(getPathFor(params.name, client.name));
});
} as FastifyPluginAsync);

View File

@ -1,8 +1,10 @@
import { FastifyPluginAsync } from 'fastify';
import upload from './upload.js';
import list from './list.js';
import get from './get.js';
export default (async function (fastify) {
await fastify.register(upload);
await fastify.register(list);
await fastify.register(get);
} as FastifyPluginAsync);

View File

@ -1,10 +1,10 @@
import { FastifyPluginAsync } from 'fastify';
import { type FastifyPluginAsync } from 'fastify';
import fsp from 'node:fs/promises';
import path from 'node:path';
import { Stats } from 'node:fs';
import { Config, config } from '../config.js';
import { InvalidAuthorization } from '../errors.js';
import { Stats } from 'node:fs';
type ListFilesResponse = {
name: string;