UNPKG

astro-image-exif-loader

Version:

Astro content collection loader for extracting EXIF data from images

64 lines (62 loc) 2.33 kB
import { relative, resolve } from "path"; //#region src/importer.ts async function imageImporter(entriesOrEntry) { if (entriesOrEntry === void 0) return null; const files = import.meta.glob("/src/**/*", { eager: false }); const isArray = Array.isArray(entriesOrEntry); const entries = isArray ? entriesOrEntry : [entriesOrEntry]; const outsideSrc = entries.filter((entry) => { if (!entry.filePath) return true; const rootPath = process.cwd(); const srcRoot = resolve(rootPath, "src"); const fullPath = resolve(rootPath, entry.filePath); const insideSrc = fullPath.startsWith(srcRoot + "/") || fullPath === srcRoot; return !insideSrc; }); if (outsideSrc.length === entries.length) throw new Error("astro-image-exif-loader: The importer only works for images under /src."); else if (outsideSrc.length > 0) console.warn(`astro-image-exif-loader: ${outsideSrc.length} entries are outside /src and cannot be imported`); const results = await Promise.all(entries.map(async (entry) => { const filePath = entry.filePath; if (!filePath) { console.warn("astro-image-exif-loader: entry missing filePath: ", entry.id); return { id: entry.id, collection: entry.collection, data: entry.data, defaultImport: null }; } const rootPath = process.cwd(); const srcRoot = resolve(rootPath, "src"); const fullPath = resolve(rootPath, filePath); const insideSrc = fullPath.startsWith(srcRoot + "/") || fullPath === srcRoot; if (!insideSrc) return { id: entry.id, collection: entry.collection, data: entry.data, defaultImport: null }; const srcRelativePath = relative(srcRoot, fullPath).replace(/\\/g, "/"); const imagePath = `/src/${srcRelativePath}`; const filesMap = files; const importer = filesMap[imagePath]; let defaultImport = null; if (importer) try { const mod = await importer(); defaultImport = mod?.default ?? null; } catch (err) { console.warn("astro-image-exif-loader: failed to import image module: ", imagePath, err); } else console.warn("astro-image-exif-loader: no importer found for: ", imagePath); return { id: entry.id, collection: entry.collection, data: entry.data, defaultImport }; })); return isArray ? results : results[0]; } var importer_default = imageImporter; //#endregion export { importer_default };