Skip to content

Commit

Permalink
handle bad http status for ntp bg image
Browse files Browse the repository at this point in the history
  • Loading branch information
petemill committed May 17, 2023
1 parent 525cd94 commit 2993dfe
Showing 1 changed file with 50 additions and 48 deletions.
98 changes: 50 additions & 48 deletions scripts/generateNTPBackgroundImages.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,56 +51,51 @@ const validatePhotoData = (photoJsonObj) => {
return isValid
}

const prepareAssets = (jsonFileUrl, targetResourceDir) => {
return new Promise(function (resolve, reject) {
let body = '{}'

// Download and parse jsonFileUrl.
fetch(jsonFileUrl).then(async function (response) {
if (response.status !== 200) {
throw new Error(`Error from ${jsonFileUrl}: ${response.status} ${response.statusText}`)
}

if (response.status === 200) {
body = await response.text()
}
let photoData = {}
try {
console.log(`Start - json file ${jsonFileUrl} parsing`)
photoData = JSON.parse(body)
} catch (err) {
const error = `Invalid json file ${jsonFileUrl}`
console.error(error)
return reject(error)
}
console.log(`Done - json file ${jsonFileUrl} parsing`)
async function prepareAssets (jsonFileUrl, targetResourceDir) {
let body = '{}'
// Download and parse jsonFileUrl.
const response = await fetch(jsonFileUrl)
if (!response.ok) {
throw new Error(`Error from ${jsonFileUrl}: ${response.status} ${response.statusText}`)
}
body = await response.text()
let photoData = {}
try {
console.log(`Start - json file ${jsonFileUrl} parsing`)
photoData = JSON.parse(body)
} catch (err) {
throw new Error(`Invalid json file ${jsonFileUrl}`, { cause: err })
}
console.log(`Done - json file ${jsonFileUrl} parsing`)

console.log(`Start - json file ${jsonFileUrl} validation`)
if (!validatePhotoData(photoData)) {
const error = `Failed to validate json file ${jsonFileUrl}`
console.error(error)
return reject(error)
}
console.log(`Done - json file ${jsonFileUrl} validation`)

createPhotoJsonFile(path.join(targetResourceDir, 'photo.json'), JSON.stringify(photoData))

// Download image files that specified in jsonFileUrl
const imageFileNameList = getImageFileNameListFrom(photoData)
const downloadOps = imageFileNameList.map(async (imageFileName) => {
const targetImageFilePath = path.join(targetResourceDir, imageFileName)
const targetImageFileUrl = new URL(imageFileName, jsonFileUrl).href
const response = await fetch(targetImageFileUrl)
const ws = fs.createWriteStream(targetImageFilePath)
return finished(Readable.fromWeb(response.body).pipe(ws))
.then(() => console.log(`Downloaded ${targetImageFileUrl}`))
})
await Promise.all(downloadOps)
resolve()
}).catch(error => {
throw new Error(`Error from ${jsonFileUrl}: ${error.cause}`)
})
console.log(`Start - json file ${jsonFileUrl} validation`)
if (!validatePhotoData(photoData)) {
throw new Error(`Failed to validate json file ${jsonFileUrl}`)
}
console.log(`Done - json file ${jsonFileUrl} validation`)

createPhotoJsonFile(path.join(targetResourceDir, 'photo.json'), JSON.stringify(photoData))

// Download image files that specified in jsonFileUrl
const imageFileNameList = getImageFileNameListFrom(photoData)
const imageErrors = []
const downloadOps = imageFileNameList.map(async (imageFileName) => {
const targetImageFilePath = path.join(targetResourceDir, imageFileName)
const targetImageFileUrl = new URL(imageFileName, jsonFileUrl).href
const response = await fetch(targetImageFileUrl)
if (!response.ok) {
imageErrors.push(`Bad http response (status code ${response.status}) downloading image file at ${targetImageFileUrl}`)
return
}
const ws = fs.createWriteStream(targetImageFilePath)
await finished(Readable.fromWeb(response.body).pipe(ws))
console.log(`Downloaded ${targetImageFileUrl}`)
})
await Promise.all(downloadOps)
if (imageErrors.length) {
imageErrors.forEach(e => console.error(e))
throw new Error('There were some image download errors. Aborting!')
}
}

async function generateNTPBackgroundImages (dataUrl) {
Expand All @@ -119,3 +114,10 @@ commander
.parse(process.argv)

generateNTPBackgroundImages(commander.dataUrl)
.catch(e => {
console.error('There was a fatal problem:', e.message)
if (e.cause) {
console.error(e.cause)
}
process.exit(1)
})

0 comments on commit 2993dfe

Please sign in to comment.