diff --git a/package.json b/package.json index 3e78b49..896450d 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ }, "dependencies": { "drizzle-orm": "^0.44.5", + "mime": "^4.1.0", "zod": "^4.1.5" }, "peerDependencies": { diff --git a/src/r2.ts b/src/r2.ts index fbccd9f..586aa19 100644 --- a/src/r2.ts +++ b/src/r2.ts @@ -1,6 +1,7 @@ import type { AuthContext } from "better-auth"; import { createAuthEndpoint, getSessionFromCtx, sessionMiddleware } from "better-auth/api"; import type { FieldAttribute } from "better-auth/db"; +import mime from "mime/lite"; import { z, type ZodType } from "zod"; import type { FileMetadata, R2Config } from "./types"; @@ -512,11 +513,18 @@ export const createR2Endpoints = ( getR2Storage: () => ReturnType | null, r2Config?: R2Config ) => { + const allowedMediaTypes = r2Config?.allowedTypes + ?.map(type => mime.getType(type)) + .filter((mimeType): mimeType is string => Boolean(mimeType)); + return { upload: createAuthEndpoint( "/files/upload-raw", { method: "POST", + metadata: { + allowedMediaTypes, + }, }, async ctx => { // Manually get session instead of using middleware