diff --git a/modules/uploads/module.yaml b/modules/uploads/module.yaml index dacb0d49..6d6430ca 100644 --- a/modules/uploads/module.yaml +++ b/modules/uploads/module.yaml @@ -1,3 +1,14 @@ +name: Uploads +description: Upload & store files in an object store. +icon: file-arrow-up +tags: + - core + - utility +authors: + - rivet-gg + - Blckbrry-Pi + - NathanFlurry +status: stable scripts: prepare: name: Prepare Upload 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..60c0305a 100644 --- a/modules/users/config.ts +++ b/modules/users/config.ts @@ -1,5 +1,7 @@ +import { Module } from "./_gen/mod.ts"; + export interface Config { - maxProfilePictureBytes: number; + maxProfilePictureSize: Module.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 8ad0af9b..8d95a150 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, Module } 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 = Module.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: