diff --git a/src/components/images/ImageCard.tsx b/src/components/images/ImageCard.tsx index 1899fa3..e9fd750 100644 --- a/src/components/images/ImageCard.tsx +++ b/src/components/images/ImageCard.tsx @@ -20,6 +20,7 @@ const STATUS_LABELS: Record = { ready: { label: '✓ Prêt', color: 'text-green-400' }, uploading: { label: '⬆️ Upload…', color: 'text-blue-400' }, uploaded: { label: '✅ Uploadé', color: 'text-green-500' }, + skipped: { label: '⏭️ Déjà sur Shopify', color: 'text-slate-400' }, not_found: { label: '❌ Réf. introuvable', color: 'text-red-400' }, error: { label: '❌ Erreur', color: 'text-red-400' }, } diff --git a/src/hooks/useImages.ts b/src/hooks/useImages.ts index 4aea7db..c22f685 100644 --- a/src/hooks/useImages.ts +++ b/src/hooks/useImages.ts @@ -241,7 +241,7 @@ export function useImages() { }) const data = await res.json() if (!res.ok) throw new Error(data.error || `HTTP ${res.status}`) - update(id, { status: 'uploaded', uploadedUrl: data.url }) + update(id, { status: data.skipped ? 'skipped' : 'uploaded', uploadedUrl: data.url }) } catch (err) { update(id, { status: 'error', error: `Upload : ${(err as Error).message}` }) } diff --git a/src/lib/shopify.ts b/src/lib/shopify.ts index acd6412..10dad03 100644 --- a/src/lib/shopify.ts +++ b/src/lib/shopify.ts @@ -85,13 +85,39 @@ export async function searchProductByRef(ref: string): Promise { + const { baseUrl, headers } = getConfig() + const res = await fetch(`${baseUrl}/products/${shopifyProductId}/images.json?fields=id,src`, { headers }) + if (!res.ok) return null + const data = await res.json() + const images = (data.images ?? []) as Array<{ id: number; src: string }> + // Compare le nom de base sans extension (ex: "30827" dans "30827.jpg") + const baseName = filename.replace(/\.[^.]+$/, '').toLowerCase() + const existing = images.find(img => { + const imgFile = img.src.split('/').pop()?.split('?')[0] ?? '' + return imgFile.replace(/\.[^.]+$/, '').toLowerCase() === baseName + }) + return existing ? existing.src : null +} + export async function uploadProductImage( shopifyProductId: string, imageBase64: string, filename: string, -): Promise<{ url: string }> { +): Promise<{ url: string; skipped?: boolean }> { const { baseUrl, headers } = getConfig() + // Vérifie si l'image existe déjà + const existingUrl = await productImageExists(shopifyProductId, filename) + if (existingUrl) return { url: existingUrl, skipped: true } + const res = await fetch(`${baseUrl}/products/${shopifyProductId}/images.json`, { method: 'POST', headers, diff --git a/src/types/images.ts b/src/types/images.ts index f91f5c9..9ea7b35 100644 --- a/src/types/images.ts +++ b/src/types/images.ts @@ -6,6 +6,7 @@ export type ImageStatus = | 'ready' // prêt à uploader | 'uploading' // upload Shopify en cours | 'uploaded' // uploadé avec succès + | 'skipped' // image déjà présente sur le produit Shopify | 'not_found' // référence produit introuvable dans Shopify | 'error' // erreur à n'importe quelle étape