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
@@ -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