From 96e8be3543fd9a3cccfdb8fbed9405432dbd32de Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Fri, 30 May 2025 11:42:44 +0100 Subject: [PATCH] fix: allow larger file uploads --- src/routes/+page.svelte | 21 ++++----------------- src/routes/api/study_spaces/+server.ts | 15 +++++++-------- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index c0591a0..74bc469 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -15,27 +15,14 @@ async function uploadStudySpace() { const imageFile = fileInput?.files?.[0]; - const imageB64 = imageFile - ? await new Promise((resolve) => { - const reader = new FileReader(); - reader.onload = () => resolve(reader.result as string); - reader.readAsDataURL(imageFile); - }) - : null; - if (!imageB64 || !imageFile) { + if (!imageFile) { alert("Please select an image file."); return; } - const res = await fetch("/api/study_spaces", { + const params = new URLSearchParams({ title, imgTitle: imageFile.name }); + const res = await fetch(`/api/study_spaces?${params.toString()}`, { method: "POST", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ - title, - img: imageB64, - imgTitle: imageFile.name - }) + body: imageFile }); if (res.ok) { alert("Study space uploaded successfully!"); diff --git a/src/routes/api/study_spaces/+server.ts b/src/routes/api/study_spaces/+server.ts index da4b54c..162e23a 100644 --- a/src/routes/api/study_spaces/+server.ts +++ b/src/routes/api/study_spaces/+server.ts @@ -5,17 +5,16 @@ import { error, type RequestHandler } from "@sveltejs/kit"; export const POST: RequestHandler = async ({ request }) => { const supabase = createClient(PUBLIC_SUPABASE_URL, PUBLIC_SUPABASE_ANON_KEY); - const body = await request.json(); - const title = body.title; - const imgB64 = body.img; - const imgTitle = body.imgTitle; - if (!title || !imgB64 || !imgTitle) error(400, "Missing required fields: title, img, imgTitle"); + const body = await request.bytes(); + const url = new URL(request.url); + const title = url.searchParams.get("title"); + const imgTitle = url.searchParams.get("imgTitle"); + if (!title || !imgTitle) error(400, "Missing required fields: title, imgTitle"); - const img = await fetch(imgB64).then((res) => res.blob()); const { data: imageData, error: imageError } = await supabase.storage .from("files_bucket") - .upload(`public/${imgTitle}`, img, { - contentType: img.type, + .upload(`public/${imgTitle}`, body, { + contentType: request.headers.get("content-type") || "image/png", upsert: false }); if (imageError) error(500, `Failed to upload image: ${imageError.message}`);