mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-08-04 12:45:17 +02:00
chore: Update S3 configuration and endpoint handling
This commit is contained in:
parent
2cd169817f
commit
d27971692d
2 changed files with 19 additions and 4 deletions
|
@ -1,7 +1,6 @@
|
||||||
// src/routes/api/upload.js
|
// src/routes/api/upload.js
|
||||||
|
|
||||||
import { ensureBucketExists, s3Client, uploadObject } from "$lib/server/s3";
|
import { ensureBucketExists, uploadObject } from "$lib/server/s3";
|
||||||
import { HeadBucketCommand } from "@aws-sdk/client-s3";
|
|
||||||
import type { RequestEvent } from "@sveltejs/kit";
|
import type { RequestEvent } from "@sveltejs/kit";
|
||||||
import { generateId } from "lucia";
|
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 contentType = event.request.headers.get("content-type") ?? "";
|
||||||
const fileExtension = contentType.split("/").pop();
|
const fileExtension = contentType.split("/").pop();
|
||||||
const fileName = `${generateId(25)}.${fileExtension}`;
|
const fileName = `${generateId(25)}.${fileExtension}`;
|
||||||
|
const bucket = event.request.headers.get("bucket") as string;
|
||||||
|
|
||||||
if (!fileExtension || !fileName) {
|
if (!fileExtension || !fileName) {
|
||||||
return new Response(JSON.stringify({ error: "Invalid file type" }), {
|
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
|
// check if the file is an image
|
||||||
if (!contentType.startsWith("image")) {
|
if (!contentType.startsWith("image")) {
|
||||||
return new Response(JSON.stringify({ error: "Invalid file type" }), {
|
return new Response(JSON.stringify({ error: "Invalid file type" }), {
|
||||||
|
@ -35,10 +47,10 @@ export async function POST(event: RequestEvent): Promise<Response> {
|
||||||
"Content-Type": contentType,
|
"Content-Type": contentType,
|
||||||
};
|
};
|
||||||
|
|
||||||
await ensureBucketExists("profile-pics");
|
await ensureBucketExists(bucket);
|
||||||
|
|
||||||
const objectUrl = await uploadObject(
|
const objectUrl = await uploadObject(
|
||||||
"profile-pics",
|
bucket,
|
||||||
fileName,
|
fileName,
|
||||||
Buffer.from(fileBuffer)
|
Buffer.from(fileBuffer)
|
||||||
);
|
);
|
||||||
|
|
|
@ -81,6 +81,9 @@ export const actions: Actions = {
|
||||||
const response = await event.fetch("/api/upload", {
|
const response = await event.fetch("/api/upload", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: profilePicture,
|
body: profilePicture,
|
||||||
|
headers: {
|
||||||
|
bucket: "profile-pics",
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue