diff options
| -rw-r--r-- | src/lib/BadgeWall/BadgePreview.svelte | 2 | ||||
| -rw-r--r-- | src/lib/Utility/cdn.ts | 22 | ||||
| -rw-r--r-- | src/lib/Utility/image.ts | 46 | ||||
| -rw-r--r-- | src/routes/user/[user]/badges/+page.svelte | 29 |
4 files changed, 49 insertions, 50 deletions
diff --git a/src/lib/BadgeWall/BadgePreview.svelte b/src/lib/BadgeWall/BadgePreview.svelte index dd2b7fd9..6515bc19 100644 --- a/src/lib/BadgeWall/BadgePreview.svelte +++ b/src/lib/BadgeWall/BadgePreview.svelte @@ -1,6 +1,6 @@ <script lang="ts"> import type { Badge } from '$lib/Database/userBadges'; - import cdn from '$lib/Utility/cdn'; + import { cdn } from '$lib/Utility/image'; import { databaseTimeToDate } from '$lib/Utility/time'; import locale from '$stores/locale'; import { cubicOut } from 'svelte/easing'; diff --git a/src/lib/Utility/cdn.ts b/src/lib/Utility/cdn.ts deleted file mode 100644 index e7d08d38..00000000 --- a/src/lib/Utility/cdn.ts +++ /dev/null @@ -1,22 +0,0 @@ -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 default cdn; 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; +}; diff --git a/src/routes/user/[user]/badges/+page.svelte b/src/routes/user/[user]/badges/+page.svelte index 7db57cd1..8adacce0 100644 --- a/src/routes/user/[user]/badges/+page.svelte +++ b/src/routes/user/[user]/badges/+page.svelte @@ -9,7 +9,7 @@ import root from '$lib/Utility/root.js'; import tooltip from '$lib/Tooltip/tooltip.js'; import proxy from '$lib/Utility/proxy.js'; - import cdn from '$lib/Utility/cdn.js'; + import { cdn } from '$lib/Utility/image'; import locale from '$stores/locale.js'; import Skeleton from '$lib/Loading/Skeleton.svelte'; import Message from '$lib/Loading/Message.svelte'; @@ -26,6 +26,7 @@ // import { io } from 'socket.io-client'; import Tooltip from '$lib/Tooltip/LinkedTooltip.svelte'; import BadgePreview from '$lib/BadgeWall/BadgePreview.svelte'; + import { thumbnail } from '$lib/Utility/image.js'; export let data; @@ -321,32 +322,6 @@ }, {}); }; - 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; - }; - const parsePost = async () => { if (importImages && importImages.length > 0) importImages = undefined; |