fix: some stuff

This commit is contained in:
Max Richter
2025-10-24 15:53:52 +02:00
parent bfe968c78f
commit 84d1c5b6fd
3 changed files with 10 additions and 5 deletions

View File

@@ -9,6 +9,7 @@
const iso = (d: string | Date) => { const iso = (d: string | Date) => {
if(!d) return "" if(!d) return ""
const v = toDate(d); const v = toDate(d);
if(!v) return ""
return isNaN(v.getTime()) ? "" : v.toISOString(); return isNaN(v.getTime()) ? "" : v.toISOString();
}; };

Binary file not shown.

View File

@@ -1,6 +1,7 @@
import { rgbaToThumbHash } from "thumbhash"; import { rgbaToThumbHash } from "thumbhash";
import ExifReader from "exifreader"; import ExifReader from "exifreader";
import type { ImageMetadata } from "astro"; import type { ImageMetadata } from "astro";
import { readFile } from "node:fs/promises";
import sharp from "sharp"; import sharp from "sharp";
export async function generateThumbHash( export async function generateThumbHash(
@@ -72,23 +73,26 @@ const allowedExif = [
]; ];
export async function getExifData(image: ImageMetadata) { export async function getExifData(image: ImageMetadata) {
if (image.format === "svg") return undefined; // SVGs don't have EXIF data") if (image.format === "svg") return undefined; // SVGs don't have EXIF data
const imagePath = (image as ImageMetadata & { fsPath: string }).fsPath ?? const imagePath = (image as ImageMetadata & { fsPath: string }).fsPath ??
image.src; image.src;
if (!imagePath) return undefined; if (!imagePath) return undefined;
try { try {
let buffer: ArrayBuffer; let buffer: ArrayBufferLike;
if (imagePath.startsWith("https://") || imagePath.startsWith("http://")) { if (imagePath.startsWith("https://") || imagePath.startsWith("http://")) {
const res = await fetch(imagePath); const res = await fetch(imagePath);
buffer = await res.arrayBuffer(); buffer = await res.arrayBuffer();
} else { } else {
buffer = await sharp(imagePath).toBuffer() as unknown as ArrayBuffer; const b = await readFile(imagePath);
buffer = b.buffer;
} }
const tags = await ExifReader.load(buffer, { async: true }); const tags = await ExifReader.load(buffer, { async: true });
if (!buffer) return undefined;
const out: Record<string, any> = {}; const out: Record<string, any> = {};
let hasExif = false; let hasExif = false;