- Refactor multiple API routes to utilize a centralized configuration function for base URL and token management, improving code consistency and maintainability. - Replace direct environment variable access with a unified config function in the `lib/api.ts` file. - Remove redundant error handling and streamline response handling in various API endpoints. - Delete unused job-related API routes and settings, simplifying the overall API structure.
47 lines
1.6 KiB
TypeScript
47 lines
1.6 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { config } from "@/lib/api";
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ bookId: string; pageNum: string }> }
|
|
) {
|
|
const { bookId, pageNum } = await params;
|
|
try {
|
|
const { baseUrl, token } = config();
|
|
const { searchParams } = new URL(request.url);
|
|
const format = searchParams.get("format") || "webp";
|
|
const width = searchParams.get("width") || "";
|
|
const quality = searchParams.get("quality") || "";
|
|
|
|
const apiUrl = new URL(`${baseUrl}/books/${bookId}/pages/${pageNum}`);
|
|
apiUrl.searchParams.set("format", format);
|
|
if (width) apiUrl.searchParams.set("width", width);
|
|
if (quality) apiUrl.searchParams.set("quality", quality);
|
|
|
|
const response = await fetch(apiUrl.toString(), {
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
});
|
|
|
|
if (!response.ok) {
|
|
return new NextResponse(`Failed to fetch image: ${response.status}`, {
|
|
status: response.status
|
|
});
|
|
}
|
|
|
|
// Récupérer le content-type et les données
|
|
const contentType = response.headers.get("content-type") || "image/webp";
|
|
const imageBuffer = await response.arrayBuffer();
|
|
|
|
// Retourner l'image avec le bon content-type
|
|
return new NextResponse(imageBuffer, {
|
|
headers: {
|
|
"Content-Type": contentType,
|
|
"Cache-Control": "public, max-age=300",
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error("Error fetching image:", error);
|
|
return new NextResponse("Failed to fetch image", { status: 500 });
|
|
}
|
|
}
|