chore: optimize LCP images, fix Turbopack warning, update mock images

This commit is contained in:
2026-09-05 19:26:57 +03:00
parent 7f8a3a34d0
commit 30fea06e5b
9 changed files with 205 additions and 6 deletions
+74
View File
@@ -0,0 +1,74 @@
import { PrismaClient } from '@prisma/client';
import { Pool } from 'pg';
import { PrismaPg } from '@prisma/adapter-pg';
import { uploadToOpeninary } from '../lib/openinary';
import fs from 'fs';
import path from 'path';
import 'dotenv/config';
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
const adapter = new PrismaPg(pool);
const prisma = new PrismaClient({ adapter });
async function main() {
console.log("Starting migration...");
const products = await prisma.products.findMany({
where: {
image_url: { not: null }
}
});
console.log(`Found ${products.length} products with images.`);
for (const product of products) {
if (!product.image_url) continue;
if (product.image_url.startsWith('kiteqr/')) {
console.log(`[SKIP] Already migrated ${product.id}`);
continue;
}
// Extract filename from image_url (e.g. admin/uploads/products/xxx.jpg or similar)
const filename = path.basename(product.image_url);
if (!filename) continue;
const localPath = path.join(process.cwd(), 'products', filename);
if (!fs.existsSync(localPath)) {
console.log(`[SKIP] Local file not found for product ${product.id} (${product.name}): ${localPath}`);
continue;
}
try {
console.log(`[UPLOAD] Uploading ${filename} for product ${product.id}...`);
const buffer = fs.readFileSync(localPath);
const ext = path.extname(filename).toLowerCase();
let mime = 'image/jpeg';
if (ext === '.png') mime = 'image/png';
if (ext === '.webp') mime = 'image/webp';
if (ext === '.gif') mime = 'image/gif';
const file = new File([buffer], filename, { type: mime });
const result = await uploadToOpeninary(file, "kiteqr");
console.log(`[SUCCESS] Uploaded to: ${result.path}`);
// Update database
await prisma.products.update({
where: { id: product.id },
data: { image_url: result.path } // Using path as requested/required by loader
});
} catch (error) {
console.error(`[ERROR] Failed to upload/update product ${product.id}:`, error);
}
}
console.log("Migration complete.");
}
main()
.catch(console.error)
.finally(() => prisma.$disconnect());
+50
View File
@@ -0,0 +1,50 @@
import { PrismaClient } from '@prisma/client';
import { Pool } from 'pg';
import { PrismaPg } from '@prisma/adapter-pg';
import { uploadToOpeninary } from '../lib/openinary';
import fs from 'fs';
import path from 'path';
import 'dotenv/config';
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
const adapter = new PrismaPg(pool);
const prisma = new PrismaClient({ adapter });
async function main() {
console.log("Uploading default product image to Openinary...");
const localPath = path.join(process.cwd(), 'public', 'default-product.png');
const buffer = fs.readFileSync(localPath);
const file = new File([buffer], 'default-product.png', { type: 'image/png' });
let defaultPath = "";
try {
const result = await uploadToOpeninary(file, "kiteqr");
defaultPath = result.path;
console.log(`[SUCCESS] Default image uploaded to: ${defaultPath}`);
} catch (error) {
console.error("Failed to upload default image:", error);
return;
}
console.log("Updating products with missing or old images...");
const products = await prisma.products.findMany();
let updatedCount = 0;
for (const product of products) {
// If image_url is empty, null, or doesn't start with kiteqr/ (meaning it wasn't migrated successfully)
if (!product.image_url || !product.image_url.startsWith('kiteqr/')) {
await prisma.products.update({
where: { id: product.id },
data: { image_url: defaultPath }
});
updatedCount++;
console.log(`Updated product ${product.id} (${product.name}) with mock image.`);
}
}
console.log(`Done. ${updatedCount} products updated with the mock image.`);
}
main()
.catch(console.error)
.finally(() => prisma.$disconnect());