Starting infrastructure for server side image loading
This commit is contained in:
1 parent
2ec6873ee0
commit
89c5e065de
8 files changed
+396
-7
No files matched your search
@@ -1,6 +1,7 @@
|
||||
---
|
||||
import { SITE, SOCIALS } from "@consts"
|
||||
import Container from "@components/Container.astro"
|
||||
import { storage } from "@lib/appwrite"
|
||||
---
|
||||
|
||||
<footer class="relative bg-white dark:bg-black">
|
||||
@@ -26,7 +27,7 @@ import Container from "@components/Container.astro"
|
||||
<div class="grid grid-cols-1 sm:grid-cols-2 gap-3">
|
||||
<div class="flex flex-col items-center sm:items-start">
|
||||
<a href="/" class="flex gap-1 w-fit font-semibold text-current hover:text-black dark:hover:text-white transition-colors duration-300 ease-in-out">
|
||||
<img src="https://appdev.spaghet.io/v1/storage/buckets/689cda7900102e12abf2/files/689d265e0036955a618d/view?project=689783c90022d294b15c" alt="Logo" class="size-6" />
|
||||
<img src="/favicon.svg" alt="Logo" class="size-6" />
|
||||
{SITE.TITLE}
|
||||
</a>
|
||||
</div>
|
||||
|
||||
@@ -11,7 +11,7 @@ import Container from "@components/Container.astro"
|
||||
<div class="relative h-full w-full">
|
||||
<div class="absolute left-0 top-1/2 -translate-y-1/2 flex gap-1 font-semibold">
|
||||
<a href="/" class="flex gap-1 text-current hover:text-spaghetti-brown dark:hover:text-spaghetti-brown transition-colors duration-300 ease-in-out">
|
||||
<img src="${import.meta.env.APPWRITE_ENDPOINT}/v1/storage/buckets/${import.meta.env.APPWRITE_BUCKET_ID}/files/689d265e0036955a618d/view?project=${import.meta.env.APPWRITE_PROJECT_ID}" alt="Logo" class="size-6" />
|
||||
<img src="/api/image-proxy/689cda7900102e12abf2/689d265e0036955a618d" alt="Logo" class="size-6" />
|
||||
<div>
|
||||
{SITE.TITLE}
|
||||
</div>
|
||||
|
||||
+12
-3
@@ -2,8 +2,17 @@ import { Client, Storage } from "appwrite";
|
||||
|
||||
const client = new Client();
|
||||
|
||||
client
|
||||
.setEndpoint(import.meta.env.APPWRITE_ENDPOINT) // Your Appwrite endpoint
|
||||
.setProject(import.meta.env.APPWRITE_PROJECT_ID); // Your Project ID
|
||||
// Check if we're in a server environment (SSR)
|
||||
if (typeof window === 'undefined') {
|
||||
// Server-side: use environment variables directly
|
||||
client
|
||||
.setEndpoint(process.env.APPWRITE_ENDPOINT!) // Your Appwrite endpoint
|
||||
.setProject(process.env.APPWRITE_PROJECT_ID!); // Your Project ID
|
||||
} else {
|
||||
// Client-side: use import.meta.env
|
||||
client
|
||||
.setEndpoint(import.meta.env.APPWRITE_ENDPOINT) // Your Appwrite endpoint
|
||||
.setProject(import.meta.env.APPWRITE_PROJECT_ID); // Your Project ID
|
||||
}
|
||||
|
||||
export const storage = new Storage(client);
|
||||
@@ -0,0 +1,60 @@
|
||||
import type { APIRoute } from "astro";
|
||||
|
||||
export const GET: APIRoute = async ({ params, request }) => {
|
||||
// Get the bucket ID and file ID from the URL parameters
|
||||
const { bucketId, fileId } = params;
|
||||
|
||||
// Validate that we have required parameters
|
||||
if (!bucketId || !fileId) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: "Missing bucketId or fileId" }),
|
||||
{ status: 400, headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
// Get the Appwrite endpoint and project ID from environment variables
|
||||
const endpoint = process.env.APPWRITE_ENDPOINT;
|
||||
const projectId = process.env.APPWRITE_PROJECT_ID;
|
||||
|
||||
if (!endpoint || !projectId) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: "Appwrite configuration missing" }),
|
||||
{ status: 500, headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
|
||||
// Construct the Appwrite storage URL
|
||||
const appwriteUrl = `${endpoint}/v1/storage/buckets/${bucketId}/files/${fileId}/view?project=${projectId}`;
|
||||
|
||||
// Fetch the image from Appwrite storage
|
||||
const response = await fetch(appwriteUrl, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Accept": "image/*"
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch image: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
// Get the image content type
|
||||
const contentType = response.headers.get("content-type") || "image/jpeg";
|
||||
|
||||
// Return the image with proper headers
|
||||
return new Response(response.body, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": contentType,
|
||||
"Cache-Control": "public, max-age=3600", // Cache for 1 hour
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error proxying image from Appwrite:", error);
|
||||
return new Response(
|
||||
JSON.stringify({ error: "Failed to fetch image" }),
|
||||
{ status: 500, headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
};
|
||||
@@ -79,7 +79,7 @@ const stack = [
|
||||
<div class="animate absolute h-full w-full flex items-center justify-center">
|
||||
<div class='relative w-full h-full flex items-center justify-center'>
|
||||
<div class='p-5 text-center'>
|
||||
<img src="${import.meta.env.APPWRITE_ENDPOINT}/v1/storage/buckets/${import.meta.env.APPWRITE_BUCKET_ID}/files/689d27a400194d326f69/view?project=${import.meta.env.APPWRITE_PROJECT_ID}" alt="Quinn Marvin" class="rounded-full w-32 h-32 mx-auto mb-4 border-4 border-spaghetti-red" />
|
||||
<img src="/api/image-proxy/689cda7900102e12abf2/689d27a400194d326f69" alt="Quinn Marvin" class="rounded-full w-32 h-32 mx-auto mb-4 border-4 border-spaghetti-red" />
|
||||
<p class='animated text-lg md:text-xl lg:text-2xl font-semibold opacity-75 text-spaghetti-brown'>
|
||||
Hello, I am Quinn Marvin
|
||||
</p>
|
||||
|
||||
Reference in new issue
Block a user