Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create and publish assets in Contentful.
const createAndPublishAssets = (assets, managementToken, spaceId, locale, simpleLog = console.log) => new Promise(async resolve => {
simpleLog('Creating Contentful client')
const client = contentful.createClient({
accessToken: managementToken,
logHandler: (level, data) => simpleLog(`${level} | ${data}`)
})
const iterableAssets = makeIterator(assets)
const space = await client.getSpace(spaceId)
const cmsAssets = []
const assetProcessingTimes = []
const inProcess = new Map()
let processedAssetsCounter = 0
const createAndPublishSingleAsset = async ({asset, done, index}) => {
if (done) {
// Check if there are still requests in process.
if (inProcess.size > 0) return false
return resolve(cmsAssets)
}
// Process the asset.
const start = Date.now()
const id = '' + start + Math.round(Math.random() * 100)
inProcess.set(id, true)
let cmsAsset
try {
cmsAsset = await space.createAsset({
fields: {
title: {
[locale]: asset.title
},
description: {
[locale]: asset.description
},
file: {
[locale]: {
contentType: 'image/jpeg',
fileName: `${asset.title.toLowerCase().replace(/\s/g, '-')}.jpg`,
upload: encodeURI(asset.link)
}
}
}
})
} catch (e) {
simpleLog(`Asset "${asset.title}" failed to create, retrying...`)
createAndPublishSingleAsset({
asset,
done,
index
})
}
try {
const processedCMSAsset = await cmsAsset.processForAllLocales()
const publishedCMSAsset = await processedCMSAsset.publish()
// Save mapping information to WordPress
publishedCMSAsset.wpAsset = asset
cmsAssets.push(publishedCMSAsset)
assetProcessingTimes.push((Date.now() - start) / 1000)
inProcess.clear(id)
const eta = Math.floor(assetProcessingTimes.reduce((a, b) => a + b, 0)/assetProcessingTimes.length * (assets.length - index) / 60)
processedAssetsCounter += 1
simpleLog(`Processed asset ${processedAssetsCounter}/${assets.length} - eta: ${eta}m`)
createAndPublishSingleAsset(iterableAssets.next())
} catch (e) {
simpleLog(`Asset "${asset.title}" failed to process, retrying...`)
await cmsAsset.delete()
createAndPublishSingleAsset({
asset,
done,
index
})
}
}
// Run multiple processings in parallel while still keeping it better debuggable.
// Because Contentful has a rate limit (10/s) and we want nice console output.
// Manual experiments yielded three as a good border for no errors.
simpleLog('Starting to create assets')
createAndPublishSingleAsset(iterableAssets.next())
createAndPublishSingleAsset(iterableAssets.next())
createAndPublishSingleAsset(iterableAssets.next())
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.