From 682d7429ed3cbb010b1db0921f6ae3b7cf6a7f67 Mon Sep 17 00:00:00 2001 From: ROpdebee <15186467+ROpdebee@users.noreply.github.com> Date: Wed, 20 Oct 2021 19:36:34 +0200 Subject: [PATCH] refactor(caa upload): split maximisation code Split maximisation code into two functions: A generic one using ImageMaxURL, and one for exceptional providers, like Discogs. This split will make it easier to handle Apple Music exceptions. --- src/mb_enhanced_cover_art_uploads/maximise.ts | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/mb_enhanced_cover_art_uploads/maximise.ts b/src/mb_enhanced_cover_art_uploads/maximise.ts index 4d92bfb3a..a2393a1ae 100644 --- a/src/mb_enhanced_cover_art_uploads/maximise.ts +++ b/src/mb_enhanced_cover_art_uploads/maximise.ts @@ -29,12 +29,15 @@ export interface MaximisedImage { } export async function* getMaximisedCandidates(smallurl: URL): AsyncIterableIterator { - // Workaround maxurl discogs difficulties - if (smallurl.hostname === 'img.discogs.com') { - yield getMaximisedCandidatesDiscogs(smallurl); - return; + const exceptions = await maximiseExceptions(smallurl); + if (exceptions) { + yield* exceptions; + } else { + yield* maximiseGeneric(smallurl); } +} +async function* maximiseGeneric(smallurl: URL): AsyncIterableIterator { const p = new Promise((resolve) => { maxurl(smallurl.href, { ...options, @@ -58,12 +61,21 @@ export async function* getMaximisedCandidates(smallurl: URL): AsyncIterableItera } } -async function getMaximisedCandidatesDiscogs(smallurl: URL): Promise { +async function maximiseExceptions(smallurl: URL): Promise { + // Various workarounds for certain image providers + if (smallurl.hostname === 'img.discogs.com') { + return maximiseDiscogs(smallurl); + } + + return; +} + +async function maximiseDiscogs(smallurl: URL): Promise { // Workaround for maxurl returning broken links and webp images const fullSizeURL = await DiscogsProvider.maximiseImage(smallurl); - return { + return [{ url: fullSizeURL, filename: fullSizeURL.pathname.split('/').at(-1), headers: {}, - }; + }]; }