// Supabase Edge Function: imagekit-files // Lists the image files in ImageKit's /viejewels/products folder so the admin // can sync product images. The ImageKit PRIVATE key stays here, on the server. // // Deploy: supabase functions deploy imagekit-files // Secret: supabase secrets set IMAGEKIT_PRIVATE_KEY=private_xxxxxxxx= // (or add the secret in the Supabase dashboard -> Edge Functions -> Secrets) const cors = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "authorization, x-client-info, apikey, content-type", "Access-Control-Allow-Methods": "POST, OPTIONS", }; Deno.serve(async (req) => { if (req.method === "OPTIONS") return new Response("ok", { headers: cors }); const key = Deno.env.get("IMAGEKIT_PRIVATE_KEY"); if (!key) { return new Response(JSON.stringify({ error: "IMAGEKIT_PRIVATE_KEY secret is not set" }), { status: 500, headers: { ...cors, "Content-Type": "application/json" }, }); } let folder = "/viejewels/products"; try { const b = await req.json(); if (b && typeof b.path === "string" && b.path.startsWith("/viejewels/")) folder = b.path; } catch (_) {} const auth = "Basic " + btoa(key + ":"); const files: unknown[] = []; let skip = 0; for (;;) { const r = await fetch( `https://api.imagekit.io/v1/files?path=${encodeURIComponent(folder)}&type=file&limit=1000&skip=${skip}`, { headers: { Authorization: auth } }, ); if (!r.ok) { return new Response(await r.text(), { status: r.status, headers: { ...cors, "Content-Type": "application/json" } }); } const page = await r.json(); for (const f of page) { files.push({ name: f.name, filePath: f.filePath, fileType: f.fileType, type: f.type, updatedAt: f.updatedAt, createdAt: f.createdAt }); } if (page.length < 1000) break; skip += 1000; } return new Response(JSON.stringify({ files }), { headers: { ...cors, "Content-Type": "application/json" } }); });