Import Instagram archive to a Bluesky account

Add graceful handling of fetching image aspect ratio via try catch and logging so our reporter can show us what file types are not accepted.

+26 -15
+11 -6
src/image/image.ts
··· 109 109 } 110 110 111 111 export async function getImageSize(filePath: string): Promise<Ratio | null> { 112 - let image = sharp(filePath); 113 - const metadata = await image.metadata(); 114 - if( metadata.width != undefined && metadata.height != undefined) 115 - return { width: metadata.width, height: metadata.height }; 116 - else 117 - return null; 112 + let ratio: Ratio | null = null; 113 + try { 114 + let image = sharp(filePath); 115 + const metadata = await image.metadata(); 116 + if( metadata.width != undefined && metadata.height != undefined) 117 + ratio = { width: metadata.width, height: metadata.height }; 118 + } catch (error) { 119 + logger.error(`Failed to get image aspect ratio; image path: ${filePath}, error: ${error}`) 120 + } 121 + 122 + return ratio; 118 123 }
+15 -9
src/media/media.test.ts
··· 452 452 expect(result).toBeNull(); 453 453 }); 454 454 455 - test("should handle errors when file is missing", async () => { 456 - try { 457 - await getImageSize("/path/to/missing.jpg"); 458 - // If we reach here, the test should fail 459 - expect(true).toBe(false); 460 - } catch (error) { 461 - expect(error).toBeDefined(); 462 - expect((error as Error).message).toContain("Input file is missing"); 463 - } 455 + test("should log error when image processing fails", async () => { 456 + // Import the logger mock 457 + const { logger } = require("../logger/logger"); 458 + 459 + // Call the function with a path that will trigger an error 460 + const result = await getImageSize("/path/to/missing.jpg"); 461 + 462 + // Verify the logger.error was called with the expected message pattern 463 + expect(logger.error).toHaveBeenCalled(); 464 + expect(logger.error).toHaveBeenCalledWith( 465 + expect.stringMatching(/Failed to get image aspect ratio; image path: \/path\/to\/missing\.jpg, error:/) 466 + ); 467 + 468 + // Verify the function returns null when an error occurs 469 + expect(result).toBeNull(); 464 470 }); 465 471 });