Skip to content

Commit

Permalink
Fix: Try again
Browse files Browse the repository at this point in the history
  • Loading branch information
AntGa committed Sep 30, 2024
1 parent d8e8cf2 commit 1e47047
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 33 deletions.
20 changes: 18 additions & 2 deletions src/components/PersonCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useEffect, useState } from "react";
import "../styles/global.css";
import "../styles/members.css";

Expand Down Expand Up @@ -27,11 +27,27 @@ const getClassName = (team: string) => {

const PersonCard: React.FC<PersonCardProps> = ({ team, desc, name, cover }) => {
const className = getClassName(team);
const [imageSrc, setImageSrc] = useState<string | null>(null);

useEffect(() => {
const fetchImage = async () => {
try {
const response = await fetch(`/api/images/${cover}`);
if (!response.ok) throw new Error('Image not found');
const blob = await response.blob();
setImageSrc(URL.createObjectURL(blob));
} catch (error) {
console.error(error);
}
};

fetchImage();
}, [cover]);

return (
<div className="person-card">
<div className="img-wrapper">
<img src={cover} alt={name} />
{imageSrc ? <img src={imageSrc} alt={name} /> : <p>Loading...</p>} {/* Show loading state */}
</div>
<p className={className}>
<b>{name}</b><br />
Expand Down
25 changes: 25 additions & 0 deletions src/pages/api/images/[...image].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import fs from 'fs';
import path from 'path';
import type { APIRoute } from 'astro';

export const get: APIRoute = ({ params }) => {
// Check if params.image is defined
const imageParam = params.image as string; // Cast to string
if (!imageParam) {
return new Response('Image not specified', { status: 400 });
}

const imagePath = path.join('data', 'members', imageParam); // Use the imageParam directly

// Check if the image file exists
if (fs.existsSync(imagePath)) {
const imageBuffer = fs.readFileSync(imagePath);
return new Response(imageBuffer, {
headers: {
'Content-Type': 'image/webp', // Adjust the content type as necessary
},
});
} else {
return new Response('Image not found', { status: 404 });
}
};
29 changes: 0 additions & 29 deletions src/pages/api/members/[imageName].ts

This file was deleted.

6 changes: 4 additions & 2 deletions src/scripts/getMembers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ export async function getMembers(): Promise<Member[]> {
console.log(`Image ${imageName} downloaded and saved.`);
}

// Update the member's cover to point to the local file path
member.cover = `/api/members/${imageName}`;

member.cover = `${sanitizedFileName}.webp`; // Only the file name, no path


} catch (err) {
console.error(`Failed to download or process image for ${member.name}:`, err);
}
Expand Down

0 comments on commit 1e47047

Please sign in to comment.