// import prisma from "@/lib/prisma"; // adjust to correct path // import { s3 } from "@/lib/s3"; // import { GetObjectCommand, PutObjectCommand } from "@aws-sdk/client-s3"; // import sharp from "sharp"; // import { Readable } from "stream"; // // const s3 = new S3Client({ region: "eu-central-1" }); // or use env config // const BUCKET = "felliesartapp"; // async function streamToBuffer(stream: Readable): Promise { // return new Promise((resolve, reject) => { // const chunks: any[] = []; // stream.on("data", (chunk) => chunks.push(chunk)); // stream.on("end", () => resolve(Buffer.concat(chunks))); // stream.on("error", reject); // }); // } // async function resizeShorterSideTo400(buffer: Buffer) { // const meta = await sharp(buffer).metadata(); // let resizeOptions; // if (meta.width && meta.height) { // resizeOptions = meta.width < meta.height ? { width: 400 } : { height: 400 }; // } else { // resizeOptions = { width: 400 }; // } // return sharp(buffer) // .resize({ ...resizeOptions, withoutEnlargement: true }) // .toFormat("webp") // .toBuffer(); // } // async function processAllImages() { // const images = await prisma.image.findMany({ // include: { // variants: true, // }, // }); // for (const image of images) { // const originalVariant = image.variants.find(v => v.type === "original"); // const resizedVariant = image.variants.find(v => v.type === "resized"); // if (!originalVariant || !originalVariant.s3Key) { // console.log(`❌ Skipping image ${image.id}: no original variant`); // continue; // } // try { // // Fetch original image // const originalData = await s3.send(new GetObjectCommand({ // Bucket: BUCKET, // Key: originalVariant.s3Key, // })); // const originalBuffer = await streamToBuffer(originalData.Body as Readable); // // Resize // const resizedBuffer = await resizeShorterSideTo400(originalBuffer); // const resizedMeta = await sharp(resizedBuffer).metadata(); // // Upload (overwrite existing resized) // const resizedKey = resizedVariant?.s3Key || `variants/${image.id}/resized.webp`; // await s3.send(new PutObjectCommand({ // Bucket: BUCKET, // Key: resizedKey, // Body: resizedBuffer, // ContentType: "image/webp", // })); // // Update or create the variant // await prisma.imageVariant.upsert({ // where: resizedVariant ? { id: resizedVariant.id } : { // imageId_type: { // imageId: image.id, // type: "resized", // } // }, // update: { // s3Key: resizedKey, // width: resizedMeta.width || 0, // height: resizedMeta.height || 0, // mimeType: "image/webp", // fileExtension: "webp", // sizeBytes: resizedBuffer.length, // }, // create: { // imageId: image.id, // type: "resized", // s3Key: resizedKey, // width: resizedMeta.width || 0, // height: resizedMeta.height || 0, // mimeType: "image/webp", // fileExtension: "webp", // sizeBytes: resizedBuffer.length, // }, // }); // console.log(`✅ Resized image ${image.id}`); // } catch (err) { // console.error(`❌ Failed to process image ${image.id}:`, err); // } // } // console.log("🎉 Done."); // } // processAllImages();