← back to Fabricut Landing

scripts/restore-images.js

37 lines

#!/usr/bin/env node
/**
 * REVERSE of localize-images.js — TK-10823.
 * Replays data/image-restore-map.json to restore the ORIGINAL S3 URLs into
 * fabricut_catalog.image_url / all_images. Makes the localization fully reversible.
 *
 *   node scripts/restore-images.js           # dry (show what would revert)
 *   node scripts/restore-images.js --apply    # write the old S3 urls back
 *
 * (Local files in public/img/fabricut are left in place — harmless; delete the
 *  dir manually if you truly want the bytes gone.)
 */
const fs = require('fs');
const path = require('path');
const { Pool } = require('pg');
const APPLY = process.argv.includes('--apply');
const MAP = path.join(__dirname, '..', 'data', 'image-restore-map.json');
const pool = new Pool(fs.existsSync('/tmp/.s.PGSQL.5432')
  ? { host: '/tmp', database: 'dw_unified' } : { host: '127.0.0.1', port: 5432, user: 'dw_admin', database: 'dw_unified', password: process.env.PGPASSWORD || '' });

async function main() {
  const m = JSON.parse(fs.readFileSync(MAP, 'utf8'));
  console.log(`[restore] ${m.restore.length} rows in map | apply=${APPLY}`);
  let n = 0;
  for (const rec of m.restore) {
    const sets = [], vals = [rec.dw_sku]; let i = 2;
    if (rec.image_url) { sets.push(`image_url = $${i++}`); vals.push(rec.image_url.old); }
    if (rec.all_images) { sets.push(`all_images = $${i++}`); vals.push(rec.all_images.old); }
    if (!sets.length) continue;
    if (APPLY) await pool.query(`UPDATE fabricut_catalog SET ${sets.join(', ')}, updated_at = now() WHERE dw_sku = $1`, vals);
    n++;
  }
  console.log(`[restore] ${APPLY ? 'REVERTED' : 'would revert'} ${n} rows to original S3 URLs.`);
  await pool.end();
}
main().catch(e => { console.error(e); process.exit(1); });