fix: some stuff

This commit is contained in:
Max Richter
2025-10-25 13:55:48 +02:00
parent 2f08c7f4c2
commit f1e0654609
5 changed files with 46 additions and 11 deletions

View File

@@ -3,6 +3,9 @@ import ExifReader from "exifreader";
import type { ImageMetadata } from "astro";
import { readFile } from "node:fs/promises";
import sharp from "sharp";
import { createHash } from "node:crypto";
import { promises as fs } from "node:fs";
import path from "node:path";
export async function generateThumbHash(
buffer: ArrayBuffer,
@@ -81,3 +84,36 @@ export async function getExifData(buffer: ArrayBuffer) {
return hasExif ? out : undefined;
}
const CACHE_DIR = path.join(process.cwd(), 'node_modules', '.astro', 'image-cache');
export async function getProcessedImage(image: ImageMetadata) {
const buffer = await getImageBuffer(image);
if (!buffer) {
return { thumbhash: undefined, exif: undefined };
}
const hash = createHash('sha256').update(new Uint8Array(buffer)).digest('hex');
const cacheFile = path.join(CACHE_DIR, `${hash}.json`);
try {
const cachedData = await fs.readFile(cacheFile, 'utf-8');
return JSON.parse(cachedData);
} catch (e) {
if (e.code !== 'ENOENT') {
console.error("Failed to read from image cache:", e);
}
}
const thumbhash = await generateThumbHash(buffer);
const exif = await getExifData(buffer);
try {
await fs.mkdir(CACHE_DIR, { recursive: true });
await fs.writeFile(cacheFile, JSON.stringify({ thumbhash, exif }), 'utf-8');
} catch (writeError) {
console.error("Failed to write to image cache:", writeError);
}
return { thumbhash, exif };
}

View File

@@ -34,7 +34,7 @@ export async function listResource(
if (json.type == "dir") {
return {
...json,
content: json.content.filter((res) =>
content: json.content.filter((res: MemoriumEntry) =>
res.mime === "application/markdown"
),
};