Update StrapiClient to modify image URL handling and add image route

This commit is contained in:
Schneider Roland 2025-03-02 21:01:58 +01:00
parent a735471eb1
commit ae8c22b560
2 changed files with 25 additions and 1 deletions

View File

@ -8,7 +8,7 @@ class StrapiClient{
constructor(private strapiUrl: string = "http://localhost:1337") { constructor(private strapiUrl: string = "http://localhost:1337") {
} }
public getImageUrl(imagePath: string){ public getImageUrl(imagePath: string){
return this.strapiUrl + imagePath; return '/image/'+ imagePath;
} }
public async httpGet(path: string){ public async httpGet(path: string){
return await httpClient.httpGet(this.strapiUrl + path); return await httpClient.httpGet(this.strapiUrl + path);

View File

@ -0,0 +1,24 @@
import strapiClient from "@/api/strapi/strapi-client";
export async function GET(_request: Request, {params}: { params: Promise<{ path: string[] }> }) {
try {
const pathParams = await params;
const strapiImageUrl = ("/" + pathParams.path.join("/"))
const imageResponse = await strapiClient.httpGet(strapiImageUrl);
if (imageResponse.status != 200) {
console.info("failed to download image,status,statusText", imageResponse.status, imageResponse.statusText)
}
const image = await imageResponse.blob()
return new Response(image, {
status: 200,
headers: {
'Content-Type': imageResponse.headers.get('content-type') as string
}
});
} catch (error) {
console.info('failed to download image: ', error);
return Response.json({error}, {status: 404});
}
}