feat: /get/:name endpoint
This commit is contained in:
parent
5e1c610614
commit
89e7e3ded4
|
@ -27,9 +27,20 @@ export const InvalidAuthorization = () =>
|
||||||
message: 'Provided authorization credentials are invalid',
|
message: 'Provided authorization credentials are invalid',
|
||||||
}) as FastifyError;
|
}) as FastifyError;
|
||||||
|
|
||||||
export const QuotaExceeded = () => ({
|
export const QuotaExceeded = () =>
|
||||||
|
({
|
||||||
code: '422',
|
code: '422',
|
||||||
statusCode: 422,
|
statusCode: 422,
|
||||||
name: 'QuotaExceeded',
|
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;
|
||||||
|
|
|
@ -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);
|
|
@ -1,8 +1,10 @@
|
||||||
import { FastifyPluginAsync } from 'fastify';
|
import { FastifyPluginAsync } from 'fastify';
|
||||||
import upload from './upload.js';
|
import upload from './upload.js';
|
||||||
import list from './list.js';
|
import list from './list.js';
|
||||||
|
import get from './get.js';
|
||||||
|
|
||||||
export default (async function (fastify) {
|
export default (async function (fastify) {
|
||||||
await fastify.register(upload);
|
await fastify.register(upload);
|
||||||
await fastify.register(list);
|
await fastify.register(list);
|
||||||
|
await fastify.register(get);
|
||||||
} as FastifyPluginAsync);
|
} as FastifyPluginAsync);
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
import { FastifyPluginAsync } from 'fastify';
|
import { type FastifyPluginAsync } from 'fastify';
|
||||||
import fsp from 'node:fs/promises';
|
import fsp from 'node:fs/promises';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
|
import { Stats } from 'node:fs';
|
||||||
|
|
||||||
import { Config, config } from '../config.js';
|
import { Config, config } from '../config.js';
|
||||||
import { InvalidAuthorization } from '../errors.js';
|
import { InvalidAuthorization } from '../errors.js';
|
||||||
import { Stats } from 'node:fs';
|
|
||||||
|
|
||||||
type ListFilesResponse = {
|
type ListFilesResponse = {
|
||||||
name: string;
|
name: string;
|
||||||
|
|
Loading…
Reference in New Issue