diff --git a/modules/uploads/public.ts b/modules/uploads/public.ts new file mode 100644 index 00000000..aeb6c4a9 --- /dev/null +++ b/modules/uploads/public.ts @@ -0,0 +1,4 @@ +export const __CANONICAL_MODULE_NAME = "uploads"; + +export type { UploadSize } from "./utils/data_size.ts"; +export { getBytes } from "./utils/data_size.ts"; diff --git a/modules/users/config.ts b/modules/users/config.ts index 4a007119..de8205ff 100644 --- a/modules/users/config.ts +++ b/modules/users/config.ts @@ -1,5 +1,7 @@ +import { Public } from "./_gen/mod.ts"; + export interface Config { - maxProfilePictureBytes: number; + maxProfilePictureSize: Public.Uploads.UploadSize; allowedMimes?: string[]; } diff --git a/modules/users/scripts/prepare_profile_picture_upload.ts b/modules/users/scripts/prepare_profile_picture_upload.ts index 2729db79..e3604444 100644 --- a/modules/users/scripts/prepare_profile_picture_upload.ts +++ b/modules/users/scripts/prepare_profile_picture_upload.ts @@ -1,4 +1,4 @@ -import { ScriptContext, RuntimeError } from "../_gen/scripts/prepare_profile_picture_upload.ts"; +import { ScriptContext, RuntimeError, Public } from "../_gen/scripts/prepare_profile_picture_upload.ts"; import { DEFAULT_MIME_TYPES } from "../config.ts"; export interface Request { @@ -30,7 +30,8 @@ export async function run( } // Ensure the file is within the maximum configured size for a PFP - if (BigInt(req.contentLength) > ctx.userConfig.maxProfilePictureBytes) { + const maxBytes = Public.Uploads.getBytes(ctx.userConfig.maxProfilePictureSize); + if (BigInt(req.contentLength) > maxBytes) { throw new RuntimeError( "file_too_large", { cause: `File is too large (${req.contentLength} bytes)` }, diff --git a/tests/basic/backend.yaml b/tests/basic/backend.yaml index da96a7b3..a2a2ef6c 100644 --- a/tests/basic/backend.yaml +++ b/tests/basic/backend.yaml @@ -14,7 +14,7 @@ modules: users: registry: local config: - maxProfilePictureBytes: 1048576 # 1 MiB + maxProfilePictureSize: 1mib uploads: registry: local config: