From 8c8ac3523b0ac487e6829b6a0927de5b926c4fea Mon Sep 17 00:00:00 2001 From: b1ek Date: Sat, 11 May 2024 20:33:07 +1000 Subject: [PATCH] feat: throw QuotaExceeded error on upload when applicable --- src/errors.ts | 7 +++++++ src/routes/upload.ts | 8 ++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/errors.ts b/src/errors.ts index c7e46fa..4f7e466 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -26,3 +26,10 @@ export const InvalidAuthorization = () => name: 'InvalidAuthorization', message: 'Provided authorization credentials are invalid', }) as FastifyError; + +export const QuotaExceeded = () => ({ + code: '422', + statusCode: 422, + name: 'QuotaExceeded', + message: 'Your quota has exceeded. Please delete some files to proceed.', +}); diff --git a/src/routes/upload.ts b/src/routes/upload.ts index 2247c84..b2f6317 100644 --- a/src/routes/upload.ts +++ b/src/routes/upload.ts @@ -5,10 +5,11 @@ import crypto from 'node:crypto'; import { InvalidAuthorization, InvalidPayload, + QuotaExceeded, ValidationError, } from '../errors.js'; import { config } from '../config.js'; -import { getTakenQuota, save } from '../store.js'; +import { save } from '../store.js'; type UploadPayload = { data: string; // base64-encoded @@ -54,7 +55,10 @@ export default (async function (fastify) { const data = Buffer.from(body.data, 'base64'); const name = body.name; - await save(data, name, client.name); + const status = await save(data, name, client.name); + if (status === 'Quota exceeded') { + throw QuotaExceeded(); + } return { status: 'ok',