Convert report photos for PDF embedding
Deploy / deploy-dev (push) Has been skipped
Deploy / deploy-prod (push) Successful in 2m37s

This commit is contained in:
Corey Blais
2026-06-03 11:20:52 -04:00
parent 603b4eee4d
commit c02bb4d6d8
3 changed files with 546 additions and 10 deletions
+25 -9
View File
@@ -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 ({
},
});
};