From b23ffc34407dd9e5cf7aec67c8618d329b36bca0 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Fri, 19 Apr 2024 16:52:28 -0700 Subject: refactor(image): move cdn to image --- src/lib/Utility/image.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/lib/Utility/image.ts (limited to 'src/lib/Utility/image.ts') diff --git a/src/lib/Utility/image.ts b/src/lib/Utility/image.ts new file mode 100644 index 00000000..94f0859c --- /dev/null +++ b/src/lib/Utility/image.ts @@ -0,0 +1,46 @@ +import { env } from '$env/dynamic/public'; + +export const cdn = (urlString: string | undefined) => + !urlString || + !['http', 'https'].some((protocol) => urlString.startsWith(protocol)) || + env.PUBLIC_ANILIST_REDIRECT_URI?.includes('localhost') || + [ + 'api.telegram.org', + 'telegra.ph', + 't.me', + 'discord.com', + 'cdn.discordapp.com', + 'media.discordapp.net', + 'images-ext-1.discordapp.net', + 'images-ext-2.discordapp.net', + 'media.trace.moe', + 'files.catbox.moe' + ].includes(new URL(urlString).hostname) + ? urlString + : `https://cdn.due.moe?url=${encodeURIComponent(urlString)}`; + +export const thumbnail = (url: string | undefined) => { + const width = 144; + const height = 200; + + if (url && url.includes('catbox.moe') && !url.includes('gif')) + return url.replace('catbox.moe/', 'catbox.moe/thumbs/t_'); + + if (url && url.includes('imgur') && !url.includes('gif')) + return (!url.includes('i.imgur.com') ? url.replace('imgur.com', 'i.imgur.com') : url).replace( + /(\.\w+)$/, + `_d.webp?maxwidth=${width}&shape=thumb&fidelity=high` + ); + + if (url && url.includes('discordapp')) { + const match = url.match(/attachments\/(\d+)\/(\d+)\/(.+)/); + + if (match) { + const [_, server, id, file] = match; + + return `https://media.discordapp.net/attachments/${server}/${id}/${file}?width=${width}&height=${height}`; + } + } + + return url; +}; -- cgit v1.2.3