aboutsummaryrefslogtreecommitdiff
path: root/utils/imageUtils.ts
blob: 6220134c6f7c845b6e0f16847e7b6c1985c7a2e0 (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
51
52
53
54
55
export function getHeaders(providerId: string) {
  switch (providerId) {
    case "mangahere":
      return { Referer: "https://mangahere.org" };
    case "mangadex":
      return { Referer: "https://mangadex.org" };
    case "mangakakalot":
      return { Referer: "https://mangakakalot.com" };
    case "mangapill":
      return { Referer: "https://mangapill.com" };
    case "mangasee123":
      return { Referer: "https://mangasee123.com" };
    case "comick":
      return { Referer: "https://comick.app" };
    default:
      return null;
  }
}

export function getRandomId() {
  return Math.random().toString(36).substr(2, 9);
}

export function truncateImgUrl(url: string | undefined) {
  if (!url) return null;

  // Find the index of .png if not found find the index of .jpg
  let index =
    url?.indexOf(".png") !== -1 ? url?.indexOf(".png") : url?.indexOf(".jpg");

  if (index && index !== -1) {
    // If .png or .jpg is found
    url = url?.slice(0, index + 4); // Slice the string from the start to the index of .png or .jpg plus 4 (the length of .png or .jpg)
  } else {
    // If .png or .jpg is not found
    return url; // Return the original url string
  }

  return url;
}

export function parseImageProxy(
  url: string | undefined | null,
  providerId: string | undefined
) {
  if (!url) return;

  return providerId
    ? `https://aoi.moopa.live/utils/image-proxy?url=${truncateImgUrl(
        url
      )}${`&headers=${encodeURIComponent(
        JSON.stringify({ Referer: providerId })
      )}`}`
    : url;
}