Convert report photos for PDF embedding
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import path from 'path';
|
||||
import sharp from 'sharp';
|
||||
|
||||
import {
|
||||
getBirdById,
|
||||
@@ -13,9 +14,31 @@ import { renderAdoptionReportPdf } from './adoptionReport.js';
|
||||
|
||||
const adoptionReportWeightHistoryDays = 425;
|
||||
|
||||
const parseDataImage = (value: string | null) => {
|
||||
if (!value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const match = value.match(/^data:image\/(?:png|jpeg|jpg|webp|gif);base64,(.+)$/);
|
||||
return match ? Buffer.from(match[1], 'base64') : null;
|
||||
};
|
||||
|
||||
const normalizeReportPhotoBuffer = async (imageBuffer: Buffer | null) => {
|
||||
if (!imageBuffer) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return await sharp(imageBuffer).rotate().png().toBuffer();
|
||||
} catch (error) {
|
||||
console.warn('Unable to normalize bird photo for adoption report:', error);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const loadBirdReportPhotoBuffer = async (bird: BirdRow) => {
|
||||
if (!bird.photo_object_key) {
|
||||
return null;
|
||||
return normalizeReportPhotoBuffer(parseDataImage(bird.photo_data_url));
|
||||
}
|
||||
|
||||
const s3Config = getS3ImageStorageConfig();
|
||||
@@ -35,13 +58,7 @@ const loadBirdReportPhotoBuffer = async (bird: BirdRow) => {
|
||||
return null;
|
||||
}
|
||||
|
||||
const contentType = imageResponse.headers.get('content-type') || bird.photo_content_type || '';
|
||||
|
||||
if (!/^image\/(?:png|jpe?g)$/i.test(contentType)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Buffer.from(await imageResponse.arrayBuffer());
|
||||
return normalizeReportPhotoBuffer(Buffer.from(await imageResponse.arrayBuffer()));
|
||||
};
|
||||
|
||||
export const renderAdoptionReportForBird = async ({
|
||||
@@ -84,4 +101,3 @@ export const renderAdoptionReportForBird = async ({
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user