From 477b93638c035ae9772376736726dbdc51845f51 Mon Sep 17 00:00:00 2001 From: David Skoland Date: Wed, 29 Nov 2023 23:10:57 +0100 Subject: Rename fileExists.js to fileExists.ts --- webAO/utils/fileExists.js | 18 ------------------ webAO/utils/fileExists.ts | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 18 deletions(-) delete mode 100644 webAO/utils/fileExists.js create mode 100644 webAO/utils/fileExists.ts (limited to 'webAO/utils') diff --git a/webAO/utils/fileExists.js b/webAO/utils/fileExists.js deleted file mode 100644 index 5b60976..0000000 --- a/webAO/utils/fileExists.js +++ /dev/null @@ -1,18 +0,0 @@ -const fileExists = async (url) => new Promise((resolve) => { - const xhr = new XMLHttpRequest(); - xhr.open('HEAD', url); - xhr.onload = function checkLoad() { - if (xhr.readyState === 4) { - if (xhr.status === 200) { - resolve(true); - } else { - resolve(false); - } - } - }; - xhr.onerror = function checkError() { - resolve(false); - }; - xhr.send(null); -}); -export default fileExists; diff --git a/webAO/utils/fileExists.ts b/webAO/utils/fileExists.ts new file mode 100644 index 0000000..5b60976 --- /dev/null +++ b/webAO/utils/fileExists.ts @@ -0,0 +1,18 @@ +const fileExists = async (url) => new Promise((resolve) => { + const xhr = new XMLHttpRequest(); + xhr.open('HEAD', url); + xhr.onload = function checkLoad() { + if (xhr.readyState === 4) { + if (xhr.status === 200) { + resolve(true); + } else { + resolve(false); + } + } + }; + xhr.onerror = function checkError() { + resolve(false); + }; + xhr.send(null); +}); +export default fileExists; -- cgit From 7661b6c7314327b6b8523976ba89755db4d281b9 Mon Sep 17 00:00:00 2001 From: David Skoland Date: Wed, 29 Nov 2023 23:14:35 +0100 Subject: Simplify and make fileExists proper typescript --- webAO/utils/fileExists.ts | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) (limited to 'webAO/utils') diff --git a/webAO/utils/fileExists.ts b/webAO/utils/fileExists.ts index 5b60976..06621e4 100644 --- a/webAO/utils/fileExists.ts +++ b/webAO/utils/fileExists.ts @@ -1,18 +1,7 @@ -const fileExists = async (url) => new Promise((resolve) => { - const xhr = new XMLHttpRequest(); - xhr.open('HEAD', url); - xhr.onload = function checkLoad() { - if (xhr.readyState === 4) { - if (xhr.status === 200) { - resolve(true); - } else { - resolve(false); - } - } - }; - xhr.onerror = function checkError() { - resolve(false); - }; - xhr.send(null); -}); -export default fileExists; +export default async function fileExists(url: string): Promise { + return fetch(url, { + method: 'HEAD', + }).then((response) => { + return response.ok; + }); +} -- cgit From 26feae73cc8b30f4a9b1189bc87d41d8e1ee2a7d Mon Sep 17 00:00:00 2001 From: David Skoland Date: Wed, 29 Nov 2023 23:37:11 +0100 Subject: Add filesExist --- webAO/utils/filesExist.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 webAO/utils/filesExist.ts (limited to 'webAO/utils') diff --git a/webAO/utils/filesExist.ts b/webAO/utils/filesExist.ts new file mode 100644 index 0000000..2f39427 --- /dev/null +++ b/webAO/utils/filesExist.ts @@ -0,0 +1,28 @@ +import fileExists from "./fileExists"; + +/** + * This function takes a list of urls and returns the first one that exists. + * It checks all the URLs in parallel. + * @param urls the list of URLs to check + * @returns either the first URL that exists or null if none were found + */ +export default async function filesExist(urls: string[]): Promise { + const promises = urls.map(async (url) => { + if (await fileExists(url)) { + return url; + } + return null; + }); + + // Run all in parallel + const results = await Promise.all(promises); + + // Find the first URL that exists (not null) or return null if none exist + for (const result of results) { + if (result !== null) { + return result; + } + } + + return null; // None of the URLs exist +} -- cgit From 0688c1852eaf8024ea041149303df63050ffe186 Mon Sep 17 00:00:00 2001 From: David Skoland Date: Wed, 29 Nov 2023 23:45:28 +0100 Subject: Add findImgSrc --- webAO/utils/findImgSrc.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 webAO/utils/findImgSrc.ts (limited to 'webAO/utils') diff --git a/webAO/utils/findImgSrc.ts b/webAO/utils/findImgSrc.ts new file mode 100644 index 0000000..b4db849 --- /dev/null +++ b/webAO/utils/findImgSrc.ts @@ -0,0 +1,19 @@ +import filesExist from "./filesExist"; +import transparentPng from '../constants/transparentPng' + +/** + * This function takes a list of urls and returns the first one that exists. + * If none is found, return a transparent png. + * The function will always return a value that is appriopriate for an img src. + * @param urls The list of urls to try + * @returns The image source of the first url that exists, or a transparent png if none exist + */ +export default async function findImgSrc(urls: string[]): Promise { + return filesExist(urls).then((url) => { + if (url !== null) { + return url; + } + // If none of the images exist, return a transparent png + return transparentPng; + }); +} -- cgit From 4766a801382e6373b982a2731cdc7ea24f0c70b3 Mon Sep 17 00:00:00 2001 From: David Skoland Date: Wed, 29 Nov 2023 23:54:23 +0100 Subject: Catch errors in fileExists --- webAO/utils/fileExists.ts | 2 ++ 1 file changed, 2 insertions(+) (limited to 'webAO/utils') diff --git a/webAO/utils/fileExists.ts b/webAO/utils/fileExists.ts index 06621e4..f1a9924 100644 --- a/webAO/utils/fileExists.ts +++ b/webAO/utils/fileExists.ts @@ -3,5 +3,7 @@ export default async function fileExists(url: string): Promise { method: 'HEAD', }).then((response) => { return response.ok; + }).catch(() => { + return false; }); } -- cgit From 90ed4b74cac7e8e410175cc3445d7140671e221f Mon Sep 17 00:00:00 2001 From: David Skoland Date: Thu, 30 Nov 2023 00:35:08 +0100 Subject: Revert fileExists --- webAO/utils/fileExists.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'webAO/utils') diff --git a/webAO/utils/fileExists.ts b/webAO/utils/fileExists.ts index f1a9924..abb2928 100644 --- a/webAO/utils/fileExists.ts +++ b/webAO/utils/fileExists.ts @@ -1,9 +1,19 @@ export default async function fileExists(url: string): Promise { - return fetch(url, { - method: 'HEAD', - }).then((response) => { - return response.ok; - }).catch(() => { - return false; + return new Promise((resolve) => { + const xhr = new XMLHttpRequest(); + xhr.open('HEAD', url); + xhr.onload = function checkLoad() { + if (xhr.readyState === 4) { + if (xhr.status === 200) { + resolve(true); + } else { + resolve(false); + } + } + }; + xhr.onerror = function checkError() { + resolve(false); + }; + xhr.send(null); }); } -- cgit