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",
"version": "1.0.25",
"version": "1.0.28",
"repository": {
"url": "https://github.com/wgroeneveld/jam-my-stack",
"type": "git"

View File

@ -15,23 +15,25 @@ const { dirname } = require('path');
**/
async function getPictures(webmentions, config) {
const pics = await Promise.all(
webmentions.filter(wm => wm.author && wm.author.picture).map(async wm => {
let fileName = `${config.directory}/${wm.author.picture}`
[...new Set(webmentions.filter(wm => wm.author && wm.author.picture).map(wm => wm.author.picture))]
.map(async picture => {
let fileName = `${config.directory}/${picture}`
if(config.extension && !fileName.endsWith(config.extension)) {
fileName += `.${config.extension}`
}
// TODO existsSync does not work, wrapped in Promise.all - does not wait for it!
if(config.override || !existsSync(fileName)) {
try {
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
await pipeline(
got.stream(`${config.endpoint}/${wm.author.picture}`),
got.stream(`${config.endpoint}/${picture}`),
createWriteStream(fileName)
)
} catch(err) {
console.log(` !! unable to download ${wm.author.picture}: ${err.message}`)
await fsp.unlink(fileName)
console.log(` !! unable to download ${picture}: ${err.message}`)
try { await fsp.unlink(fileName) } catch { }
}
}
return fileName