1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-08-02 19:55:18 +02:00

chore: Update S3 configuration and endpoint handling

This commit is contained in:
Sean Morley 2024-06-12 13:23:59 +00:00
parent 2cd169817f
commit d27971692d
2 changed files with 19 additions and 4 deletions

View file

@ -1,7 +1,6 @@
// src/routes/api/upload.js
import { ensureBucketExists, s3Client, uploadObject } from "$lib/server/s3";
import { HeadBucketCommand } from "@aws-sdk/client-s3";
import { ensureBucketExists, uploadObject } from "$lib/server/s3";
import type { RequestEvent } from "@sveltejs/kit";
import { generateId } from "lucia";
@ -10,6 +9,7 @@ export async function POST(event: RequestEvent): Promise<Response> {
const contentType = event.request.headers.get("content-type") ?? "";
const fileExtension = contentType.split("/").pop();
const fileName = `${generateId(25)}.${fileExtension}`;
const bucket = event.request.headers.get("bucket") as string;
if (!fileExtension || !fileName) {
return new Response(JSON.stringify({ error: "Invalid file type" }), {
@ -20,6 +20,18 @@ export async function POST(event: RequestEvent): Promise<Response> {
});
}
if (!bucket) {
return new Response(
JSON.stringify({ error: "Bucket name is required" }),
{
status: 400,
headers: {
"Content-Type": "application/json",
},
}
);
}
// check if the file is an image
if (!contentType.startsWith("image")) {
return new Response(JSON.stringify({ error: "Invalid file type" }), {
@ -35,10 +47,10 @@ export async function POST(event: RequestEvent): Promise<Response> {
"Content-Type": contentType,
};
await ensureBucketExists("profile-pics");
await ensureBucketExists(bucket);
const objectUrl = await uploadObject(
"profile-pics",
bucket,
fileName,
Buffer.from(fileBuffer)
);

View file

@ -81,6 +81,9 @@ export const actions: Actions = {
const response = await event.fetch("/api/upload", {
method: "POST",
body: profilePicture,
headers: {
bucket: "profile-pics",
},
});
const data = await response.json();