aboutsummaryrefslogtreecommitdiff
path: root/webAO/utils
diff options
context:
space:
mode:
Diffstat (limited to 'webAO/utils')
-rw-r--r--webAO/utils/__tests__/tryUrls.test.ts31
-rw-r--r--webAO/utils/aoml.ts3
-rw-r--r--webAO/utils/tryUrls.ts20
3 files changed, 53 insertions, 1 deletions
diff --git a/webAO/utils/__tests__/tryUrls.test.ts b/webAO/utils/__tests__/tryUrls.test.ts
new file mode 100644
index 0000000..444664e
--- /dev/null
+++ b/webAO/utils/__tests__/tryUrls.test.ts
@@ -0,0 +1,31 @@
+import fileExists from '../fileExists'
+import tryUrls from '../tryUrls';
+import transparentPng from '../../constants/transparentPng'
+jest.mock('../fileExists')
+
+const mockFileExists = fileExists as jest.MockedFunction<typeof fileExists>;
+
+describe('tryUrls', () => {
+ it('Should try multiple file extensions', async () => {
+ const url = "localhost/stoneddiscord/assets"
+ mockFileExists
+ .mockReturnValueOnce(Promise.resolve(false))
+ .mockReturnValueOnce(Promise.resolve(false))
+ .mockReturnValueOnce(Promise.resolve(false))
+ .mockReturnValueOnce(Promise.resolve(true))
+ const actual = await tryUrls(url)
+ const expected = 'localhost/stoneddiscord/assets.apng'
+ expect(actual).toBe(expected);
+ });
+
+ it('Should return a transparent png if it cant find any assets', async () => {
+ const url = "localhost/stoneddiscord/assets"
+ mockFileExists
+ .mockReturnValue(Promise.resolve(false))
+ const actual = await tryUrls(url)
+ const expected = transparentPng
+ expect(actual).toBe(expected);
+ });
+})
+
+
diff --git a/webAO/utils/aoml.ts b/webAO/utils/aoml.ts
index da66d0c..fbdee21 100644
--- a/webAO/utils/aoml.ts
+++ b/webAO/utils/aoml.ts
@@ -1,6 +1,7 @@
import request from "../services/request"
interface Aoml {
+ [key: string]: string | number,
name: string;
start: string;
end: string;
@@ -32,7 +33,7 @@ const aomlParser = (text: string) => {
return parsed
}
-const mlConfig = (AO_HOST) => {
+const mlConfig = (AO_HOST: string) => {
const defaultUrl = `${AO_HOST}themes/default/chat_config.ini`
let aomlParsed: Promise<{[key: string]: Aoml}> = request(defaultUrl).then((data) => aomlParser(data));
diff --git a/webAO/utils/tryUrls.ts b/webAO/utils/tryUrls.ts
new file mode 100644
index 0000000..14ef885
--- /dev/null
+++ b/webAO/utils/tryUrls.ts
@@ -0,0 +1,20 @@
+import fileExists from './fileExists'
+import transparentPng from '../constants/transparentPng'
+const urlExtensionsToTry = [
+ '.png',
+ '.gif',
+ '.webp',
+ '.apng'
+]
+const tryUrls = async (url: string) => {
+ for (let i = 0; i < urlExtensionsToTry.length; i++) {
+ const extension = urlExtensionsToTry[i]
+ const fullFileUrl = url + extension
+ const exists = await fileExists(fullFileUrl);
+ if (exists) {
+ return fullFileUrl
+ }
+ }
+ return transparentPng
+}
+export default tryUrls \ No newline at end of file