only fetch pictures once using new Set

This commit is contained in:
Wouter Groeneveld 2022-05-07 15:15:52 +02:00
parent 66d0fd3e69
commit 57586c478e
2 changed files with 8 additions and 6 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "jam-my-stack", "name": "jam-my-stack",
"version": "1.0.25", "version": "1.0.28",
"repository": { "repository": {
"url": "https://github.com/wgroeneveld/jam-my-stack", "url": "https://github.com/wgroeneveld/jam-my-stack",
"type": "git" "type": "git"

View File

@ -15,23 +15,25 @@ const { dirname } = require('path');
**/ **/
async function getPictures(webmentions, config) { async function getPictures(webmentions, config) {
const pics = await Promise.all( const pics = await Promise.all(
webmentions.filter(wm => wm.author && wm.author.picture).map(async wm => { [...new Set(webmentions.filter(wm => wm.author && wm.author.picture).map(wm => wm.author.picture))]
let fileName = `${config.directory}/${wm.author.picture}` .map(async picture => {
let fileName = `${config.directory}/${picture}`
if(config.extension && !fileName.endsWith(config.extension)) { if(config.extension && !fileName.endsWith(config.extension)) {
fileName += `.${config.extension}` fileName += `.${config.extension}`
} }
// TODO existsSync does not work, wrapped in Promise.all - does not wait for it!
if(config.override || !existsSync(fileName)) { if(config.override || !existsSync(fileName)) {
try { try {
await fsp.mkdir(dirname(fileName), { recursive: true }), await fsp.mkdir(dirname(fileName), { recursive: true }),
// retry API with streams is ridiculously complicated: https://github.com/sindresorhus/got/blob/main/documentation/7-retry.md // retry API with streams is ridiculously complicated: https://github.com/sindresorhus/got/blob/main/documentation/7-retry.md
await pipeline( await pipeline(
got.stream(`${config.endpoint}/${wm.author.picture}`), got.stream(`${config.endpoint}/${picture}`),
createWriteStream(fileName) createWriteStream(fileName)
) )
} catch(err) { } catch(err) {
console.log(` !! unable to download ${wm.author.picture}: ${err.message}`) console.log(` !! unable to download ${picture}: ${err.message}`)
await fsp.unlink(fileName) try { await fsp.unlink(fileName) } catch { }
} }
} }
return fileName return fileName