aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Utility/image.ts
blob: c1c2ae0ed14f48b7bc1e9720f958c2d0053d744a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { isLocalApp } from "$lib/Utility/appOrigin";

export const cdn = (urlString: string | undefined) =>
	!urlString ||
	!["http", "https"].some((protocol) => urlString.startsWith(protocol)) ||
	isLocalApp() ||
	[
		"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;
};