v3: code quality pass, various bug fixes
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { Context } from "@oak/oak";
|
||||
import { APIErrorCode, APIException } from "../model/interfaces.ts";
|
||||
|
||||
export const UPLOADS_DIR = "api/uploads";
|
||||
export const DUMPS_DIR = `${UPLOADS_DIR}/dumps`;
|
||||
@@ -35,6 +36,26 @@ export function detectImageMime(data: Uint8Array): string | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Validates image upload data: checks size and MIME. Returns the detected MIME type or throws APIException. */
|
||||
export function validateImageUpload(data: Uint8Array): string {
|
||||
if (data.length > MAX_IMAGE_SIZE) {
|
||||
throw new APIException(
|
||||
APIErrorCode.BAD_REQUEST,
|
||||
400,
|
||||
"File too large (max 5 MB)",
|
||||
);
|
||||
}
|
||||
const mime = detectImageMime(data);
|
||||
if (!mime) {
|
||||
throw new APIException(
|
||||
APIErrorCode.BAD_REQUEST,
|
||||
400,
|
||||
"File content is not a recognised image (JPEG, PNG, GIF, WebP)",
|
||||
);
|
||||
}
|
||||
return mime;
|
||||
}
|
||||
|
||||
export async function serveUploadedFile(
|
||||
ctx: Context,
|
||||
filePath: string,
|
||||
|
||||
Reference in New Issue
Block a user