import JSZip from 'jszip' import type { ZipEntry } from '@/types/images' const IMAGE_EXTS = new Set(['heic', 'heif', 'jpg', 'jpeg', 'png']) /** * Extrait toutes les images d'un fichier ZIP. * Structure attendue : dossier-ref/image.ext (le nom du dossier = référence produit). */ export async function readImagesFromZip(file: File): Promise { const zip = await JSZip.loadAsync(file) const entries: Array<{ relativePath: string; entry: JSZip.JSZipObject }> = [] zip.forEach((relativePath, entry) => { if (!entry.dir) entries.push({ relativePath, entry }) }) const results: ZipEntry[] = [] for (const { relativePath, entry } of entries) { if (relativePath.startsWith('__MACOSX/')) continue const parts = relativePath.split('/').filter((p) => p.length > 0) const filename = parts[parts.length - 1] if (filename.startsWith('.')) continue const dotIdx = filename.lastIndexOf('.') if (dotIdx === -1) continue const ext = filename.slice(dotIdx + 1).toLowerCase() if (!IMAGE_EXTS.has(ext)) continue const folder = parts.length > 1 ? parts[0] : filename.slice(0, dotIdx) const isHeic = ext === 'heic' || ext === 'heif' const mimeType = isHeic ? 'image/heic' : ext === 'jpg' || ext === 'jpeg' ? 'image/jpeg' : 'image/png' const rawBlob = await entry.async('blob') const blob = new Blob([rawBlob], { type: mimeType }) results.push({ folder, filename: filename.slice(0, dotIdx), ext, blob, isHeic }) } return results }