fix: allow larger file uploads
This commit is contained in:
@@ -15,27 +15,14 @@
|
|||||||
|
|
||||||
async function uploadStudySpace() {
|
async function uploadStudySpace() {
|
||||||
const imageFile = fileInput?.files?.[0];
|
const imageFile = fileInput?.files?.[0];
|
||||||
const imageB64 = imageFile
|
if (!imageFile) {
|
||||||
? await new Promise<string>((resolve) => {
|
|
||||||
const reader = new FileReader();
|
|
||||||
reader.onload = () => resolve(reader.result as string);
|
|
||||||
reader.readAsDataURL(imageFile);
|
|
||||||
})
|
|
||||||
: null;
|
|
||||||
if (!imageB64 || !imageFile) {
|
|
||||||
alert("Please select an image file.");
|
alert("Please select an image file.");
|
||||||
return;
|
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",
|
method: "POST",
|
||||||
headers: {
|
body: imageFile
|
||||||
"Content-Type": "application/json"
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
title,
|
|
||||||
img: imageB64,
|
|
||||||
imgTitle: imageFile.name
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
alert("Study space uploaded successfully!");
|
alert("Study space uploaded successfully!");
|
||||||
|
|||||||
@@ -5,17 +5,16 @@ import { error, type RequestHandler } from "@sveltejs/kit";
|
|||||||
|
|
||||||
export const POST: RequestHandler = async ({ request }) => {
|
export const POST: RequestHandler = async ({ request }) => {
|
||||||
const supabase = createClient<Database>(PUBLIC_SUPABASE_URL, PUBLIC_SUPABASE_ANON_KEY);
|
const supabase = createClient<Database>(PUBLIC_SUPABASE_URL, PUBLIC_SUPABASE_ANON_KEY);
|
||||||
const body = await request.json();
|
const body = await request.bytes();
|
||||||
const title = body.title;
|
const url = new URL(request.url);
|
||||||
const imgB64 = body.img;
|
const title = url.searchParams.get("title");
|
||||||
const imgTitle = body.imgTitle;
|
const imgTitle = url.searchParams.get("imgTitle");
|
||||||
if (!title || !imgB64 || !imgTitle) error(400, "Missing required fields: title, img, 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
|
const { data: imageData, error: imageError } = await supabase.storage
|
||||||
.from("files_bucket")
|
.from("files_bucket")
|
||||||
.upload(`public/${imgTitle}`, img, {
|
.upload(`public/${imgTitle}`, body, {
|
||||||
contentType: img.type,
|
contentType: request.headers.get("content-type") || "image/png",
|
||||||
upsert: false
|
upsert: false
|
||||||
});
|
});
|
||||||
if (imageError) error(500, `Failed to upload image: ${imageError.message}`);
|
if (imageError) error(500, `Failed to upload image: ${imageError.message}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user