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

chore: Update getBackgroundImages to use getObjectUrl for generating image URLs

This commit is contained in:
Sean Morley 2024-06-12 17:49:26 +00:00
parent 6385f9f46b
commit 5d0b450429
5 changed files with 52 additions and 42 deletions

View file

@ -1,20 +1,29 @@
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
import { db } from "./db.server"; import { db } from "./db.server";
import { imagesTable } from "./schema"; import { imagesTable } from "./schema";
import { getObjectUrl, s3Client } from "$lib/server/s3";
import { ListObjectsV2Command } from "@aws-sdk/client-s3";
export const getBackgroundImages = async (numberOfResults: number | null) => { export const getBackgroundImages = async () => {
const images = await db // const images = await db
.select({ url: imagesTable.url }) // .select({ url: imagesTable.url })
.from(imagesTable) // .from(imagesTable)
.where(eq(imagesTable.type, "background")) // .where(eq(imagesTable.type, "background"))
.execute(); // .execute();
if (numberOfResults) { // list all objects in the bucket using listObjectsV2
let randomIndex = Math.floor(Math.random() * images.length); const data = await s3Client.send(
return images.slice(randomIndex, randomIndex + numberOfResults) as { new ListObjectsV2Command({ Bucket: "images" })
url: string; );
}[]; const randomImages = data.Contents?.map((item) => item.Key) ?? [];
} const randomIndex = Math.floor(Math.random() * randomImages.length);
return images as { url: string }[]; const randomImage = randomImages[randomIndex];
console.log(randomImage);
const url = getObjectUrl("images", randomImage as string);
console.log(url);
return url as string;
}; };

View file

@ -85,32 +85,8 @@ export const uploadObject = async (
try { try {
await s3Client.send(putObjectCommand); await s3Client.send(putObjectCommand);
// Determine the provider from the endpoint
let endpoint = env.AWS_S3_ENDPOINT as string;
if (env.MINIO_CLIENT_OVERRIDE) {
endpoint = env.MINIO_CLIENT_OVERRIDE;
}
let objectUrl: string; let objectUrl: string;
objectUrl = getObjectUrl(bucketName, fileName);
if (endpoint.includes("amazonaws.com")) {
// Amazon S3
objectUrl = `https://${bucketName}.s3.${env.AWS_REGION}.amazonaws.com/${fileName}`;
} else if (endpoint.includes("storage.googleapis.com")) {
// Google Cloud Storage
objectUrl = `https://storage.googleapis.com/${bucketName}/${fileName}`;
} else if (endpoint.includes("digitaloceanspaces.com")) {
// DigitalOcean Spaces
objectUrl = `https://${bucketName}.${endpoint}/${fileName}`;
} else if (endpoint.includes("supabase.co")) {
// Supabase Storage
endpoint = endpoint.replace("s3", "object/public"); // Remove the version
console.log(endpoint);
objectUrl = `${endpoint}/${bucketName}/${fileName}`;
} else {
// Default fallback
objectUrl = `${endpoint}/${bucketName}/${fileName}`;
}
return objectUrl; return objectUrl;
} catch (error) { } catch (error) {
console.error( console.error(
@ -137,3 +113,29 @@ export const deleteObject = async (bucketName: string, fileName: string) => {
throw error; throw error;
} }
}; };
export const getObjectUrl = (bucketName: string, fileName: string): string => {
let objectUrl: string;
let endpoint = env.AWS_S3_ENDPOINT as string;
if (endpoint.includes("amazonaws.com")) {
// Amazon S3
objectUrl = `https://${bucketName}.s3.${env.AWS_REGION}.amazonaws.com/${fileName}`;
} else if (endpoint.includes("storage.googleapis.com")) {
// Google Cloud Storage
objectUrl = `https://storage.googleapis.com/${bucketName}/${fileName}`;
} else if (endpoint.includes("digitaloceanspaces.com")) {
// DigitalOcean Spaces
objectUrl = `https://${bucketName}.${endpoint}/${fileName}`;
} else if (endpoint.includes("supabase.co")) {
// Supabase Storage
endpoint = endpoint.replace("s3", "object/public"); // Remove the version
console.log(endpoint);
objectUrl = `${endpoint}/${bucketName}/${fileName}`;
} else {
// Default fallback
objectUrl = `${endpoint}/${bucketName}/${fileName}`;
}
return objectUrl;
};

View file

@ -13,7 +13,7 @@ export const load: PageServerLoad = async (event) => {
if (event.locals.user) { if (event.locals.user) {
return redirect(302, "/"); return redirect(302, "/");
} }
return { images: await getBackgroundImages(1) }; return { image: await getBackgroundImages() };
}; };
export const actions: Actions = { export const actions: Actions = {

View file

@ -6,7 +6,6 @@
import { onMount } from "svelte"; import { onMount } from "svelte";
export let data; export let data;
console.log(data); console.log(data);
let quote: string = ""; let quote: string = "";
@ -40,7 +39,7 @@
<div <div
class="min-h-screen bg-no-repeat bg-cover flex items-center justify-center" class="min-h-screen bg-no-repeat bg-cover flex items-center justify-center"
style="background-image: url('{data.images[0].url}')" style="background-image: url('{data.image}')"
> >
<div class="card card-compact w-96 bg-base-100 shadow-xl p-6"> <div class="card card-compact w-96 bg-base-100 shadow-xl p-6">
<article class="text-center text-4xl font-extrabold"> <article class="text-center text-4xl font-extrabold">

View file

@ -209,7 +209,7 @@ export const actions: Actions = {
method: "POST", method: "POST",
body: background, body: background,
headers: { headers: {
bucket: "images", bucket: "backgrounds",
type: "background", type: "background",
}, },
}); });