feat: throw QuotaExceeded error on upload when applicable

This commit is contained in:
b1ek 2024-05-11 20:33:07 +10:00
parent 89a6efb4a3
commit 8c8ac3523b
Signed by: blek
GPG Key ID: 14546221E3595D0C
2 changed files with 13 additions and 2 deletions

View File

@ -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.',
});

View File

@ -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',