Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
feat(users): Update users config to use UploadSize from `uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
Blckbrry-Pi committed Apr 24, 2024
1 parent f496b8f commit d24daf9
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
4 changes: 4 additions & 0 deletions modules/uploads/public.ts
Original file line number Diff line number Diff line change
@@ -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";
4 changes: 3 additions & 1 deletion modules/users/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Public } from "./_gen/mod.ts";

export interface Config {
maxProfilePictureBytes: number;
maxProfilePictureSize: Public.Uploads.UploadSize;
allowedMimes?: string[];
}

Expand Down
5 changes: 3 additions & 2 deletions modules/users/scripts/prepare_profile_picture_upload.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)` },
Expand Down
2 changes: 1 addition & 1 deletion tests/basic/backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ modules:
users:
registry: local
config:
maxProfilePictureBytes: 1048576 # 1 MiB
maxProfilePictureSize: 1mib
uploads:
registry: local
config:
Expand Down

0 comments on commit d24daf9

Please sign in to comment.