From 5ea7cae63fde7b9e326f96371de9ba84eb175839 Mon Sep 17 00:00:00 2001 From: adb Date: Fri, 26 Feb 2021 00:31:09 +0100 Subject: [PATCH] retry media upload --- node_app/main.js | 2 +- node_app/mastodonHandler.js | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/node_app/main.js b/node_app/main.js index 09626f4..eb706a0 100644 --- a/node_app/main.js +++ b/node_app/main.js @@ -21,7 +21,7 @@ await tokenHandler.getAccessToken((accessToken) => { if (allowedMedia.find(type => type === sub.url.split(/[.]+/).pop())){ let filepath = `./media/${sub.url.split(/[/]+/).pop()}`; memeHandler.downloadMedia(sub.url, filepath, () => { - client.postMedia(status, filepath); + client.postMedia(status, filepath, 5, () => postRandomMeme()); }) }else{ console.log("no valid media, fetching new sub") diff --git a/node_app/mastodonHandler.js b/node_app/mastodonHandler.js index 5be92c1..1608f3e 100644 --- a/node_app/mastodonHandler.js +++ b/node_app/mastodonHandler.js @@ -8,8 +8,17 @@ export class MastodonHandler extends Mastodon{ console.log(resp.data); }); } - postMedia(status, mediaPath){ + postMedia(status, mediaPath, retry, onError){ this.post('media', { file: fs.createReadStream(mediaPath) }).then(resp => { + if (retry <= 0){ + onError(); + return; + } + if (!resp.data.id){ + this.postMedia(status, mediaPath, --retry); + console.log("issue while uploading, retry") + return; + } let id = resp.data.id; console.log("media uploaded => "); console.log(resp.data);