← back to Dw Sarah Rowland Searchfix

upload-asset.mjs

17 lines

import fs from 'node:fs'; import os from 'node:os';
const env=fs.readFileSync(`${os.homedir()}/Projects/secrets-manager/.env`,'utf8');
const tok=(k)=>(env.match(new RegExp('^'+k+'=(.+)$','m'))||[])[1]?.trim();
const T=tok('SHOPIFY_THEME_TOKEN')||tok('SHOPIFY_CONTENT_TOKEN')||tok('SHOPIFY_ADMIN_TOKEN');
const S='designer-laboratory-sandbox.myshopify.com',A='2024-10',ID='144396058675';
const IMG='https://koroseal.com/getattachment/redefine-design/2024-June/Meet-the-Creator-Sarah-Rowland/BLOG-BODY-IMAGE-PORTRAIT.png';
const img=await fetch(IMG,{headers:{'User-Agent':'Mozilla/5.0'}});
const buf=Buffer.from(await img.arrayBuffer());
console.log('downloaded portrait:', buf.length, 'bytes', img.headers.get('content-type'));
const put=await fetch(`https://${S}/admin/api/${A}/themes/${ID}/assets.json`,{method:'PUT',
  headers:{'X-Shopify-Access-Token':T,'Content-Type':'application/json'},
  body:JSON.stringify({asset:{key:'assets/designer-sarah-rowland.png', attachment: buf.toString('base64')}})});
const j=await put.json();
if(put.status!==200){ console.log('PUT FAILED', put.status, JSON.stringify(j).slice(0,300)); process.exit(1); }
console.log('theme asset saved. public_url:', j.asset?.public_url || '(none)');
fs.writeFileSync(new URL('./asset-url.txt',import.meta.url), j.asset?.public_url||'');