Update StrapiClient to modify image URL handling and add image route
This commit is contained in:
parent
a735471eb1
commit
ae8c22b560
@ -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);
|
||||
|
||||
24
yoga-app/src/app/image/[...path]/route.ts
Normal file
24
yoga-app/src/app/image/[...path]/route.ts
Normal 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});
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user