Spaces:
Running
Running
/** | |
* Script to add placeholder images to existing categories and products | |
* Uses Lorem Picsum for free placeholder images | |
*/ | |
import { storage } from './storage'; | |
// Generate random Lorem Picsum image URL | |
function getPlaceholderImageUrl(width: number = 400, height: number = 300): string { | |
const randomId = Math.floor(Math.random() * 1000) + 1; | |
return `https://picsum.photos/${width}/${height}?random=${randomId}`; | |
} | |
// Get category-specific image URL | |
function getCategoryImageUrl(categoryName: string): string { | |
const randomId = Math.floor(Math.random() * 1000) + 1; | |
return `https://picsum.photos/200/200?random=${randomId}`; | |
} | |
// Get product images array (3 images per product) | |
function getProductImages(): string[] { | |
const images: string[] = []; | |
for (let i = 0; i < 3; i++) { | |
const randomId = Math.floor(Math.random() * 1000) + 1; | |
images.push(`https://picsum.photos/400/400?random=${randomId}`); | |
} | |
return images; | |
} | |
export async function addPlaceholderImages() { | |
console.log('πΌοΈ Adding placeholder images to products and categories...\n'); | |
try { | |
// Update categories | |
console.log('Fetching categories...'); | |
const categories = await storage.getAllCategories(); | |
console.log(`Found ${categories.length} categories`); | |
for (const category of categories) { | |
if (!category.imageUrl) { | |
const imageUrl = getCategoryImageUrl(category.name); | |
console.log(`Updating category "${category.name}" with image: ${imageUrl}`); | |
await storage.updateCategory(category.id, { imageUrl }); | |
console.log(`β Updated category: ${category.name}`); | |
} else { | |
console.log(`Category "${category.name}" already has an image`); | |
} | |
} | |
console.log(''); | |
// Update products | |
console.log('Fetching products...'); | |
const products = await storage.getAllProducts(); | |
console.log(`Found ${products.length} products`); | |
for (const product of products) { | |
if (!product.images || product.images.length === 0) { | |
const images = getProductImages(); | |
console.log(`Updating product "${product.title}" with ${images.length} images`); | |
await storage.updateProduct(product.id, { images }); | |
console.log(`β Updated product: ${product.title}`); | |
} else { | |
console.log(`Product "${product.title}" already has ${product.images.length} image(s)`); | |
} | |
} | |
console.log('\nβ Finished adding placeholder images!'); | |
return { success: true, message: 'Placeholder images added successfully' }; | |
} catch (error) { | |
console.error('Error adding placeholder images:', error); | |
return { success: false, error: error instanceof Error ? error.message : 'Unknown error' }; | |
} | |
} | |
// Add API endpoint to trigger this function | |
export function addPlaceholderImagesRoute(app: any) { | |
app.post('/api/admin/add-placeholder-images', async (req: any, res: any) => { | |
try { | |
const result = await addPlaceholderImages(); | |
res.json(result); | |
} catch (error) { | |
res.status(500).json({ success: false, error: error instanceof Error ? error.message : 'Unknown error' }); | |
} | |
}); | |
} |