← back to Designer Wallcoverings

mailers/upload-hollywood-rooms.js

49 lines

// Upload the 3 approved Hollywood room-setting heroes to Shopify Files → prints their CDN URLs.
// Run:  node upload-hollywood-rooms.js
const fs = require('fs');
const env = Object.fromEntries(
  fs.readFileSync('/Users/macstudio3/Projects/secrets-manager/.env', 'utf8')
    .split('\n').filter(l => l.includes('=')).map(l => { const i = l.indexOf('='); return [l.slice(0, i).trim(), l.slice(i + 1).trim()]; })
);
const STORE = env.SHOPIFY_STORE_DOMAIN || 'designer-laboratory-sandbox.myshopify.com';
const TOKEN = env.SHOPIFY_ADMIN_TOKEN;
const API = `https://${STORE}/admin/api/2024-10/graphql.json`;
const FILES = ['hollywood-room-croco.jpg', 'hollywood-room-grasscloth.jpg', 'hollywood-room-fauxlinen.jpg'];
const gql = (q, v) => fetch(API, { method: 'POST', headers: { 'X-Shopify-Access-Token': TOKEN, 'Content-Type': 'application/json' }, body: JSON.stringify({ query: q, variables: v }) }).then(r => r.json());

async function uploadOne(file) {
  const path = __dirname + '/' + file;
  const bytes = fs.statSync(path).size;
  const s = await gql(`mutation($i:[StagedUploadInput!]!){stagedUploadsCreate(input:$i){stagedTargets{url resourceUrl parameters{name value}} userErrors{message}}}`,
    { i: [{ resource: 'FILE', filename: file, mimeType: 'image/jpeg', httpMethod: 'POST', fileSize: String(bytes) }] });
  const t = s.data?.stagedUploadsCreate?.stagedTargets?.[0];
  if (!t) throw new Error('staged: ' + JSON.stringify(s));
  const fd = new FormData();
  for (const p of t.parameters) fd.append(p.name, p.value);
  fd.append('file', new Blob([fs.readFileSync(path)], { type: 'image/jpeg' }), file);
  const up = await fetch(t.url, { method: 'POST', body: fd });
  if (up.status >= 400) throw new Error('POST ' + up.status);
  const c = await gql(`mutation($f:[FileCreateInput!]!){fileCreate(files:$f){files{... on MediaImage{id}} userErrors{message}}}`,
    { f: [{ originalSource: t.resourceUrl, contentType: 'IMAGE', alt: file.replace(/\.jpg$/, '').replace(/-/g, ' ') }] });
  const id = c.data?.fileCreate?.files?.[0]?.id;
  if (!id) throw new Error('fileCreate: ' + JSON.stringify(c));
  for (let i = 0; i < 16; i++) {
    await new Promise(r => setTimeout(r, 2500));
    const q = await gql(`query($id:ID!){node(id:$id){... on MediaImage{fileStatus image{url}}}}`, { id });
    const u = q.data?.node?.image?.url;
    if (u) return u;
  }
  return '(still processing, id ' + id + ')';
}

(async () => {
  const out = {};
  for (const f of FILES) {
    process.stdout.write('uploading ' + f + ' … ');
    try { out[f] = await uploadOne(f); console.log('ok'); }
    catch (e) { out[f] = 'ERROR ' + e.message; console.log('FAIL', e.message); }
  }
  console.log('\n=== READY — paste this whole block back to Claude ===');
  for (const f of FILES) console.log(f + '  ->  ' + out[f]);
})();