From ae8c22b560a89d4b5d5f4999d4d12e776212f90a Mon Sep 17 00:00:00 2001 From: Schneider Roland Date: Sun, 2 Mar 2025 21:01:58 +0100 Subject: [PATCH] Update StrapiClient to modify image URL handling and add image route --- yoga-app/src/api/strapi/strapi-client.ts | 2 +- yoga-app/src/app/image/[...path]/route.ts | 24 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 yoga-app/src/app/image/[...path]/route.ts diff --git a/yoga-app/src/api/strapi/strapi-client.ts b/yoga-app/src/api/strapi/strapi-client.ts index d0773d1..2fb2e7e 100644 --- a/yoga-app/src/api/strapi/strapi-client.ts +++ b/yoga-app/src/api/strapi/strapi-client.ts @@ -8,7 +8,7 @@ class StrapiClient{ constructor(private strapiUrl: string = "http://localhost:1337") { } public getImageUrl(imagePath: string){ - return this.strapiUrl + imagePath; + return '/image/'+ imagePath; } public async httpGet(path: string){ return await httpClient.httpGet(this.strapiUrl + path); diff --git a/yoga-app/src/app/image/[...path]/route.ts b/yoga-app/src/app/image/[...path]/route.ts new file mode 100644 index 0000000..b0ebc70 --- /dev/null +++ b/yoga-app/src/app/image/[...path]/route.ts @@ -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}); + } +}