diff --git a/modules/uploads/scripts/complete.ts b/modules/uploads/scripts/complete.ts index a660b731..98b0f5c5 100644 --- a/modules/uploads/scripts/complete.ts +++ b/modules/uploads/scripts/complete.ts @@ -19,7 +19,7 @@ export async function run( ctx: ScriptContext, req: Request, ): Promise { - const config = getConfig(ctx.config); + const config = getConfig(ctx); const newUpload = await ctx.db.$transaction(async (db) => { // Find the upload by ID diff --git a/modules/uploads/scripts/delete.ts b/modules/uploads/scripts/delete.ts index e04b23a2..bd2ea026 100644 --- a/modules/uploads/scripts/delete.ts +++ b/modules/uploads/scripts/delete.ts @@ -15,7 +15,7 @@ export async function run( ctx: ScriptContext, req: Request, ): Promise { - const config = getConfig(ctx.config); + const config = getConfig(ctx); const bytesDeleted = await ctx.db.$transaction(async (db) => { const upload = await db.upload.findFirst({ diff --git a/modules/uploads/scripts/get.ts b/modules/uploads/scripts/get.ts index e685d322..9b82fe22 100644 --- a/modules/uploads/scripts/get.ts +++ b/modules/uploads/scripts/get.ts @@ -19,7 +19,7 @@ export async function run( ctx: ScriptContext, req: Request, ): Promise { - getConfig(ctx.config); + getConfig(ctx); // Find uploads that match the IDs in the request const dbUploads = await ctx.db.upload.findMany({ diff --git a/modules/uploads/scripts/get_public_file_urls.ts b/modules/uploads/scripts/get_public_file_urls.ts index affcb802..910aa33a 100644 --- a/modules/uploads/scripts/get_public_file_urls.ts +++ b/modules/uploads/scripts/get_public_file_urls.ts @@ -17,7 +17,7 @@ export async function run( ctx: ScriptContext, req: Request, ): Promise { - const config = getConfig(ctx.config); + const config = getConfig(ctx); const dbFiles = await ctx.db.files.findMany({ where: { diff --git a/modules/uploads/scripts/prepare.ts b/modules/uploads/scripts/prepare.ts index 294287a8..6a127fa6 100644 --- a/modules/uploads/scripts/prepare.ts +++ b/modules/uploads/scripts/prepare.ts @@ -24,7 +24,7 @@ export async function run( ctx: ScriptContext, req: Request, ): Promise { - const config = getConfig(ctx.config); + const config = getConfig(ctx); // Ensure there are files in the upload if (req.files.length === 0) { diff --git a/modules/uploads/tests/e2e.ts b/modules/uploads/tests/e2e.ts index 158d28b7..1b0e1da7 100644 --- a/modules/uploads/tests/e2e.ts +++ b/modules/uploads/tests/e2e.ts @@ -7,7 +7,7 @@ import { faker } from "https://deno.land/x/deno_faker@v1.0.3/mod.ts"; import { getS3EnvConfig } from "../utils/env.ts"; test("e2e", async (ctx: TestContext) => { - if (!getS3EnvConfig()) { + if (!getS3EnvConfig(ctx)) { ctx.log.warn("s3 not configured"); return; } diff --git a/modules/uploads/tests/multipart.ts b/modules/uploads/tests/multipart.ts index 5518a3ab..6afe3ffd 100644 --- a/modules/uploads/tests/multipart.ts +++ b/modules/uploads/tests/multipart.ts @@ -19,7 +19,7 @@ function randomBuffer(size: number): Uint8Array { } test("multipart uploads", async (ctx: TestContext) => { - if (!getS3EnvConfig()) { + if (!getS3EnvConfig(ctx)) { ctx.log.warn("s3 not configured"); return; } diff --git a/modules/uploads/utils/config_defaults.ts b/modules/uploads/utils/config_defaults.ts index 2b0d3c0d..18b71e37 100644 --- a/modules/uploads/utils/config_defaults.ts +++ b/modules/uploads/utils/config_defaults.ts @@ -1,4 +1,4 @@ -import { RuntimeError } from "../module.gen.ts"; +import { RuntimeError, ScriptContext } from "../module.gen.ts"; import { Config as UserConfig } from "../config.ts"; import { getS3EnvConfig, S3Config } from "./env.ts"; @@ -13,17 +13,17 @@ interface Config { s3: S3Config; } -export function getConfig(config: UserConfig): Required { - const s3 = getS3EnvConfig(); +export function getConfig(ctx: ScriptContext): Required { + const s3 = getS3EnvConfig(ctx); if (!s3) throw new RuntimeError("s3_not_configured"); const nonOptionalConfig = { - maxUploadSize: config.maxUploadSize ?? defaults.DEFAULT_MAX_UPLOAD_SIZE, - maxMultipartUploadSize: config.maxMultipartUploadSize ?? + maxUploadSize: ctx.config.maxUploadSize ?? defaults.DEFAULT_MAX_UPLOAD_SIZE, + maxMultipartUploadSize: ctx.config.maxMultipartUploadSize ?? defaults.DEFAULT_MAX_MULTIPART_UPLOAD_SIZE, - maxFilesPerUpload: config.maxFilesPerUpload ?? + maxFilesPerUpload: ctx.config.maxFilesPerUpload ?? defaults.DEFAULT_MAX_FILES_PER_UPLOAD, - defaultMultipartChunkSize: config.defaultMultipartChunkSize ?? + defaultMultipartChunkSize: ctx.config.defaultMultipartChunkSize ?? defaults.DEFAULT_MULTIPART_CHUNK_SIZE, s3, }; diff --git a/modules/uploads/utils/env.ts b/modules/uploads/utils/env.ts index e3693e95..befe3536 100644 --- a/modules/uploads/utils/env.ts +++ b/modules/uploads/utils/env.ts @@ -1,3 +1,5 @@ +import { ModuleContext, ScriptContext } from "../module.gen.ts"; + export interface S3EnvConfig { S3_ENDPOINT: string; S3_REGION: string; @@ -14,12 +16,12 @@ export interface S3Config { secretAccessKey: string; } -export function getS3EnvConfig(): S3Config | null { - const endpoint = Deno.env.get("S3_ENDPOINT"); - const region = Deno.env.get("S3_REGION"); - const bucket = Deno.env.get("S3_BUCKET"); - const accessKeyId = Deno.env.get("S3_ACCESS_KEY_ID"); - const secretAccessKey = Deno.env.get("S3_SECRET_ACCESS_KEY"); +export function getS3EnvConfig(ctx: ScriptContext): S3Config | null { + const endpoint = ctx.environment.get("S3_ENDPOINT"); + const region = ctx.environment.get("S3_REGION"); + const bucket = ctx.environment.get("S3_BUCKET"); + const accessKeyId = ctx.environment.get("S3_ACCESS_KEY_ID"); + const secretAccessKey = ctx.environment.get("S3_SECRET_ACCESS_KEY"); if ( !endpoint || !region || !bucket || !accessKeyId || !secretAccessKey