Spaces:
Running
Running
/** | |
* Script to add placeholder images to existing products and categories | |
* Uses Lorem Picsum for free placeholder images | |
*/ | |
const API_BASE = 'http://localhost:7860'; | |
// Generate random Lorem Picsum image URL | |
function getPlaceholderImageUrl(width = 400, height = 300, category = 'all') { | |
const randomId = Math.floor(Math.random() * 1000) + 1; | |
return `https://picsum.photos/${width}/${height}?random=${randomId}`; | |
} | |
// Get category-specific image URL | |
function getCategoryImageUrl(categoryName) { | |
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() { | |
const images = []; | |
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; | |
} | |
async function updateCategories() { | |
try { | |
console.log('Fetching categories...'); | |
const response = await fetch(`${API_BASE}/api/categories`); | |
const categories = await response.json(); | |
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}`); | |
const updateResponse = await fetch(`${API_BASE}/api/categories/${category.id}`, { | |
method: 'PUT', | |
headers: { | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify({ | |
imageUrl: imageUrl | |
}) | |
}); | |
if (updateResponse.ok) { | |
console.log(`β Updated category: ${category.name}`); | |
} else { | |
console.error(`β Failed to update category: ${category.name}`); | |
} | |
} | |
} | |
} catch (error) { | |
console.error('Error updating categories:', error); | |
} | |
} | |
async function updateProducts() { | |
try { | |
console.log('Fetching products...'); | |
const response = await fetch(`${API_BASE}/api/products`); | |
const products = await response.json(); | |
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`); | |
// Since the API expects file uploads, we'll need to use the storage interface directly | |
// For now, we'll log what we would update | |
console.log(`Would update product "${product.title}" with images:`, images); | |
} | |
} | |
} catch (error) { | |
console.error('Error updating products:', error); | |
} | |
} | |
async function main() { | |
console.log('πΌοΈ Adding placeholder images to products and categories...\n'); | |
await updateCategories(); | |
console.log('\n'); | |
await updateProducts(); | |
console.log('\nβ Finished adding placeholder images!'); | |
} | |
// Only run if called directly | |
if (typeof window === 'undefined' && typeof require !== 'undefined') { | |
main().catch(console.error); | |
} | |
// Export for use in browser/client | |
if (typeof module !== 'undefined') { | |
module.exports = { | |
getPlaceholderImageUrl, | |
getCategoryImageUrl, | |
getProductImages | |
}; | |
} |