recipe-manager/fix-image-paths.js

44 lines
1.1 KiB
JavaScript

#!/usr/bin/env node
import initSqlJs from 'sql.js';
import fs from 'fs';
import path from 'path';
const DB_PATH = 'data/recipes.db';
async function fixImagePaths() {
const SQL = await initSqlJs();
const dbBuffer = fs.readFileSync(DB_PATH);
const db = new SQL.Database(dbBuffer);
// Get all recipes with image_url starting with 'images/'
const recipes = db.exec(`
SELECT id, title, image_url
FROM recipes
WHERE image_url LIKE 'images/%'
`);
if (recipes.length === 0 || recipes[0].values.length === 0) {
console.log('✓ No image paths to update');
return;
}
let updated = 0;
recipes[0].values.forEach(([id, title, imageUrl]) => {
// Change 'images/file.jpg' to '/images/file.jpg'
const newUrl = '/' + imageUrl;
db.run(`UPDATE recipes SET image_url = ? WHERE id = ?`, [newUrl, id]);
console.log(`✓ Updated recipe ${id} (${title}): ${imageUrl}${newUrl}`);
updated++;
});
// Save the database
const data = db.export();
fs.writeFileSync(DB_PATH, data);
db.close();
console.log(`\n✓ Fixed ${updated} image paths`);
}
fixImagePaths().catch(console.error);