initial commit: project completion with proper gitignore
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
const cloudinary = require('cloudinary').v2;
|
||||
const postgres = require('postgres');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
require('dotenv').config({ path: '.env.local' });
|
||||
|
||||
cloudinary.config({
|
||||
cloud_name: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME,
|
||||
api_key: process.env.CLOUDINARY_API_KEY,
|
||||
api_secret: process.env.CLOUDINARY_API_SECRET
|
||||
});
|
||||
|
||||
const sql = postgres(process.env.DATABASE_URL);
|
||||
|
||||
async function uploadAndSync() {
|
||||
const partnersDir = path.join(process.cwd(), 'public', 'partnerlogo');
|
||||
const files = fs.readdirSync(partnersDir).filter(file => file.endsWith('.png'));
|
||||
|
||||
console.log(`Found ${files.length} images to upload.`);
|
||||
|
||||
// First, let's clear or check existing partners
|
||||
// To keep it simple, we'll insert new records or update by a 'name' which we'll placeholder
|
||||
|
||||
for (const file of files) {
|
||||
const filePath = path.join(partnersDir, file);
|
||||
const partnerId = file.replace('.png', ''); // e.g., "1"
|
||||
|
||||
try {
|
||||
console.log(`Uploading ${file}...`);
|
||||
const result = await cloudinary.uploader.upload(filePath, {
|
||||
folder: 'partners',
|
||||
public_id: `partner_${partnerId}`
|
||||
});
|
||||
|
||||
console.log(`Uploaded: ${result.secure_url}`);
|
||||
|
||||
// Insert or Update in DB
|
||||
// We'll use partnerId as id and display_order
|
||||
const idNum = parseInt(partnerId);
|
||||
await sql`
|
||||
INSERT INTO partners (id, name, logo, display_order)
|
||||
VALUES (${idNum}, ${`Partner ${partnerId}`}, ${result.secure_url}, ${idNum})
|
||||
ON CONFLICT (id)
|
||||
DO UPDATE SET logo = EXCLUDED.logo
|
||||
`;
|
||||
|
||||
console.log(`Synced ${file} to database (ID: ${idNum}).`);
|
||||
} catch (error) {
|
||||
console.error(`Error processing ${file}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Sync complete.');
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
uploadAndSync();
|
||||
Reference in New Issue
Block a user