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

chore: update uploads to read from correct env #130

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/uploads/scripts/complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function run(
ctx: ScriptContext,
req: Request,
): Promise<Response> {
const config = getConfig(ctx.config);
const config = getConfig(ctx);

const newUpload = await ctx.db.$transaction(async (db) => {
// Find the upload by ID
Expand Down
2 changes: 1 addition & 1 deletion modules/uploads/scripts/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function run(
ctx: ScriptContext,
req: Request,
): Promise<Response> {
const config = getConfig(ctx.config);
const config = getConfig(ctx);

const bytesDeleted = await ctx.db.$transaction(async (db) => {
const upload = await db.upload.findFirst({
Expand Down
2 changes: 1 addition & 1 deletion modules/uploads/scripts/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function run(
ctx: ScriptContext,
req: Request,
): Promise<Response> {
getConfig(ctx.config);
getConfig(ctx);

// Find uploads that match the IDs in the request
const dbUploads = await ctx.db.upload.findMany({
Expand Down
2 changes: 1 addition & 1 deletion modules/uploads/scripts/get_public_file_urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function run(
ctx: ScriptContext,
req: Request,
): Promise<Response> {
const config = getConfig(ctx.config);
const config = getConfig(ctx);

const dbFiles = await ctx.db.files.findMany({
where: {
Expand Down
2 changes: 1 addition & 1 deletion modules/uploads/scripts/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function run(
ctx: ScriptContext,
req: Request,
): Promise<Response> {
const config = getConfig(ctx.config);
const config = getConfig(ctx);

// Ensure there are files in the upload
if (req.files.length === 0) {
Expand Down
2 changes: 1 addition & 1 deletion modules/uploads/tests/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { faker } from "https://deno.land/x/[email protected]/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;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/uploads/tests/multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
14 changes: 7 additions & 7 deletions modules/uploads/utils/config_defaults.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -13,17 +13,17 @@ interface Config {
s3: S3Config;
}

export function getConfig(config: UserConfig): Required<Config> {
const s3 = getS3EnvConfig();
export function getConfig(ctx: ScriptContext): Required<Config> {
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,
};
Expand Down
14 changes: 8 additions & 6 deletions modules/uploads/utils/env.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ModuleContext, ScriptContext } from "../module.gen.ts";

export interface S3EnvConfig {
S3_ENDPOINT: string;
S3_REGION: string;
Expand All @@ -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
Expand Down
Loading