refactor: streamline image handling by implementing direct streaming in BookService and ImageService, and update .gitignore to include temp directory
This commit is contained in:
@@ -3,28 +3,34 @@ import { ERROR_CODES } from "../../constants/errorCodes";
|
||||
import { AppError } from "../../utils/errors";
|
||||
import logger from "@/lib/logger";
|
||||
|
||||
export interface ImageResponse {
|
||||
buffer: Buffer;
|
||||
contentType: string | null;
|
||||
}
|
||||
// Cache HTTP navigateur : 30 jours (immutable car les thumbnails ne changent pas)
|
||||
const IMAGE_CACHE_MAX_AGE = 2592000;
|
||||
|
||||
export class ImageService extends BaseApiService {
|
||||
static async getImage(path: string): Promise<ImageResponse> {
|
||||
/**
|
||||
* Stream an image directly from Komga without buffering in memory
|
||||
* Returns a Response that can be directly returned to the client
|
||||
*/
|
||||
static async streamImage(
|
||||
path: string,
|
||||
cacheMaxAge: number = IMAGE_CACHE_MAX_AGE
|
||||
): Promise<Response> {
|
||||
try {
|
||||
const headers = { Accept: "image/jpeg, image/png, image/gif, image/webp, */*" };
|
||||
|
||||
// NE PAS mettre en cache - les images sont trop grosses et les Buffers ne sérialisent pas bien
|
||||
const response = await this.fetchFromApi<Response>({ path }, headers, { isImage: true });
|
||||
const contentType = response.headers.get("content-type");
|
||||
const arrayBuffer = await response.arrayBuffer();
|
||||
const buffer = Buffer.from(arrayBuffer);
|
||||
|
||||
return {
|
||||
buffer,
|
||||
contentType,
|
||||
};
|
||||
// Stream the response body directly without buffering
|
||||
return new Response(response.body, {
|
||||
status: response.status,
|
||||
headers: {
|
||||
"Content-Type": response.headers.get("content-type") || "image/jpeg",
|
||||
"Content-Length": response.headers.get("content-length") || "",
|
||||
"Cache-Control": `public, max-age=${cacheMaxAge}, immutable`,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error({ err: error }, "Erreur lors de la récupération de l'image");
|
||||
logger.error({ err: error }, "Erreur lors du streaming de l'image");
|
||||
throw new AppError(ERROR_CODES.IMAGE.FETCH_ERROR, {}, error);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user