From 57586c478e0e4cac696ef2734f7045c0e46fecb1 Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Sat, 7 May 2022 15:15:52 +0200 Subject: [PATCH] only fetch pictures once using new Set --- package.json | 2 +- src/webmention/pictures.js | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 68770ed..2e7d263 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/webmention/pictures.js b/src/webmention/pictures.js index d012a9a..8d0e0e2 100644 --- a/src/webmention/pictures.js +++ b/src/webmention/pictures.js @@ -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