one lazy ai
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 4m26s

This commit is contained in:
2025-12-02 21:24:05 -08:00
parent 67d1b9b077
commit d592200b71
2 changed files with 24 additions and 12 deletions

View File

@@ -29,7 +29,7 @@ export async function getFlyers(): Promise<Flyer[]> {
) as store
FROM public.flyers f
LEFT JOIN public.stores s ON f.store_id = s.store_id
ORDER BY f.valid_to DESC, s.name ASC;
ORDER BY f.created_at DESC, s.name ASC;
`;
const res = await getPool().query<Flyer>(query);
return res.rows;

View File

@@ -107,22 +107,34 @@ export const flyerWorker = new Worker<FlyerJobData>(
const viewport = page.getViewport({ scale: 1.5 }); // ~150 DPI
// Create a fake canvas and context to render the PDF into raw pixel data.
// This is a common pattern for using pdf.js on the server without a real canvas.
const canvas = {
width: viewport.width,
height: viewport.height,
getContext: () => context,
// This is a common pattern for using pdf.js on the server. We must provide
// stubs for the methods the rendering engine expects to find.
const canvasAndContext = {
canvas: {
width: viewport.width,
height: viewport.height,
getContext: () => canvasAndContext.context,
},
context: {
canvas: null as any, // This will be set below
getImageData: () => ({ data: new Uint8ClampedArray(viewport.width * viewport.height * 4) }),
// Add stub methods that pdf.js calls during rendering.
fillRect: () => {},
transform: () => {},
createPattern: () => ({}),
},
};
const context = {
canvas: canvas, // The context needs a back-reference to its canvas.
getImageData: () => ({ data: new Uint8ClampedArray(viewport.width * viewport.height * 4) }),
canvasAndContext.context.canvas = canvasAndContext.canvas;
const renderContext = {
canvasContext: canvasAndContext.context as any,
viewport,
canvas: canvasAndContext.canvas as any, // Add the canvas object to the render context
};
const renderContext = { canvasContext: context as any, viewport, canvas: canvas as any };
const renderTask = page.render(renderContext);
await renderTask.promise;
const rawPixelData = context.getImageData().data;
const rawPixelData = canvasAndContext.context.getImageData().data;
const imageFileName = `${path.basename(filePath, '.pdf')}_page_${i}.jpeg`;
const imageOutputPath = path.join(outputDir, imageFileName);
await sharp(rawPixelData, { raw: { width: viewport.width, height: viewport.height, channels: 4 } }).jpeg().toFile(imageOutputPath);