mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-19 21:09:37 +02:00
CDN Route
This commit is contained in:
parent
7894e8c192
commit
272ea60057
3 changed files with 15 additions and 44 deletions
|
@ -1,7 +1,6 @@
|
|||
<script lang="ts">
|
||||
import { enhance } from "$app/forms";
|
||||
import { goto } from "$app/navigation";
|
||||
import { getObjectUrl } from "$lib";
|
||||
export let user: any;
|
||||
|
||||
async function navToSettings() {
|
||||
|
@ -16,7 +15,7 @@
|
|||
<div class="avatar placeholder">
|
||||
<div class="bg-neutral text-neutral-content rounded-full w-10 ml-4">
|
||||
{#if user.icon}
|
||||
<img src={getObjectUrl("profile-pics", user.icon)} alt="" />
|
||||
<img src={`/cdn/profile-pics/${user.icon}`} alt="" />
|
||||
{:else}
|
||||
<span class="text-2xl -mt-1">{user.first_name[0]}</span>
|
||||
{/if}
|
||||
|
|
|
@ -124,45 +124,3 @@ export async function getImage(adventureTitle: string) {
|
|||
return `Error fetching Wikipedia data for "${adventureTitle}".`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the URL of an object in the specified bucket.
|
||||
* @param bucketName - The name of the bucket.
|
||||
* @param fileName - The name of the file.
|
||||
* @returns The URL of the object.
|
||||
*/
|
||||
export const getObjectUrl = (bucketName: string, fileName: string): string => {
|
||||
let objectUrl: string;
|
||||
let endpoint: string = "";
|
||||
if (import.meta.env.VITE_MINIO_CLIENT_OVERRIDE) {
|
||||
endpoint = import.meta.env.VITE_MINIO_CLIENT_OVERRIDE as string;
|
||||
} else {
|
||||
endpoint = import.meta.env.VITE_AWS_S3_ENDPOINT as string;
|
||||
}
|
||||
|
||||
// This code is not as clean as it could be, but it works for whats needed. Help is welcome to clean it up!
|
||||
// Currently supports Amazon S3, Google Cloud Storage, DigitalOcean Spaces, and Supabase Storage as well as self-hosted MinIO.
|
||||
|
||||
if (endpoint.includes("amazonaws.com")) {
|
||||
// Amazon S3
|
||||
objectUrl = `https://${bucketName}.s3.${
|
||||
import.meta.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 as string;
|
||||
};
|
||||
|
|
14
src/routes/cdn/[bucket]/[key]/+page.server.ts
Normal file
14
src/routes/cdn/[bucket]/[key]/+page.server.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
import { redirect } from "@sveltejs/kit";
|
||||
import type { PageServerLoad, RouteParams } from "../../../$types";
|
||||
import { getObjectUrl } from "$lib/server/s3";
|
||||
|
||||
export const load = (async (event) => {
|
||||
const key = event.params.key as string;
|
||||
const bucket = event.params.bucket as string;
|
||||
|
||||
const url = getObjectUrl(bucket, key);
|
||||
|
||||
console.log(`Redirecting to ${url}`);
|
||||
|
||||
return redirect(302, url);
|
||||
}) satisfies PageServerLoad;
|
Loading…
Add table
Add a link
Reference in a new issue