Created
September 13, 2022 13:36
-
-
Save JohnRSim/d30be0c488fb1bed1e2444697cfa4cf0 to your computer and use it in GitHub Desktop.
Example function for creating digital assets on OCM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* createDigitalAsset | |
* Creates a digital asset in OCM and returns reference | |
* @param {*} assetInfo product data | |
* @param {*} image image data | |
* @returns OCM digital asset data | |
*/ | |
async function createDigitalAsset(assetInfo,image) { | |
console.log('[createDigitalAsset]',assetInfo); | |
return new Promise(async (resolve, reject) => { | |
//check if asset has been created and skip | |
if (uploadedDigitalAssets[image.id]) { | |
resolve(uploadedDigitalAssets[image.id]); | |
return; | |
} | |
//download set path to new asset and collect file extension | |
const assetPath = await downloadImg(image.src); | |
const filename = Path.basename(assetPath); | |
const fileExtension = Path.extname(filename); | |
//setup formdata for post | |
const fd = new FormData(); | |
//asset meta info | |
const fileMeta = JSON.stringify({ | |
name: filename.replace(/[&\/\\#,+()$~%'":;*?<>{}]/g,''),//strip invalid chars for OCM asset name | |
type: `${process.env.OCM_Image_Type}`, //set Digital Asset type ie Image or Custom | |
repositoryId: `${process.env.REPO}`, // OCM Repository to store digital asset | |
//fields - if digital asset type = 'Image' then fields:{} empty object | |
fields:{ | |
//title: filename, // set title as asset filename | |
//alternatetext: image.alt || ' ', //try to grab alt data from shopify and set | |
}, | |
'fileExtension': fileExtension, //define extension | |
/*additional info to assign ie taxonomies, collection etc here if needed | |
'collections': { | |
'data': [] | |
}, | |
'tags': { | |
'data': [] | |
}, | |
*/ | |
}); | |
//create metadata blob and assign to form data | |
const blob = new Blob([fileMeta], {type: 'application/json'}); | |
fd.append('item', blob,'blob'); | |
//grab file and store in fd | |
const theFile = fileFromSync(assetPath, `image/${fileExtension}`) | |
fd.append('file', theFile, filename); | |
//begin upload attempt | |
try { | |
await fetch(`${process.env.OCM_URL}/content/management/api/v1.1/items`, { | |
method: 'post', | |
headers: { | |
//'Content-Type': 'application/json', //don't add | |
//'Content-Type': 'multipart/form-data', //don't add | |
'X-Requested-With': 'XMLHttpRequest', | |
'Authorization': `Bearer ${process.env.OCM_TOKEN}`, | |
}, | |
body: fd | |
}).then((res) => { | |
return res.json(); | |
}).then((json) => { | |
//console.log(json); | |
//store reference to image that it has been created - to prevent duplicates | |
uploadedDigitalAssets[image.id] = json; | |
resolve(json); | |
}); | |
} catch (err) { | |
console.error('[createDigitalAsset][post] err:', err); | |
reject() | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment