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 authored and NathanFlurry committed May 18, 2024
1 parent 256e40f commit 38edfa3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
11 changes: 11 additions & 0 deletions modules/uploads/module.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
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 { Module } from "./_gen/mod.ts";

export interface Config {
maxProfilePictureBytes: number;
maxProfilePictureSize: Module.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, Module } 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 = 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)` },
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 38edfa3

Please sign in to comment.