File size: 2,282 Bytes
2276d59
62527b4
2276d59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee1209b
 
 
2276d59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee1209b
 
 
 
 
2276d59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62527b4
2276d59
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { URL } from 'node:url';
import { logger } from '../infra/logger.util.js';

const SUPPORTED_DOMAINS: string[] = [
  'youtube.com',
  'youtu.be',
  'spotify.com',
  'open.spotify.com',
  'facebook.com',
  'fb.watch',
  'instagram.com',
  'threads.net',
  'threads.com',
  'tiktok.com',
  'twitter.com',
  'x.com',
  'soundcloud.com',
  'reddit.com',
  'bsky.app',
  'bilibili.tv',
  'biliintl.com',
  'bili.im',
];

export function isSupportedUrl(url: string | null | undefined): boolean {
  if (!url) return false;
  try {
    const parsed = new URL(url);
    return SUPPORTED_DOMAINS.some(
      (domain) =>
        parsed.hostname === domain || parsed.hostname.endsWith(`.${domain}`)
    );
  } catch {
    return false;
  }
}

export function isValidSpotifyUrl(url: string | null | undefined): boolean {
  if (!url) return false;
  try {
    const parsed = new URL(url);
    return (
      parsed.hostname === 'open.spotify.com' ||
      parsed.hostname === 'spotify.com'
    );
  } catch {
    return false;
  }
}

export function extractTrackId(url: string | null | undefined): string | null {
  if (!url || !isValidSpotifyUrl(url)) return null;
  const match = url.match(/\/track\/([a-zA-Z0-9]{22})/);
  return match ? match[1] : null;
}

const PROXY_ALLOWED_DOMAINS: string[] = [
  'googlevideo.com',
  'youtube.com',
  'youtu.be',
  'spotifycdn.com',
  'soundcharts.com',
  'i.scdn.co',
  'fbcdn.net',
  'cdninstagram.com',
  'facebook.com',
  'fb.watch',
  'instagram.com',
  'akamaihd.net',
  'bilibili.tv',
  'biliintl.com',
  'bili.im',
  'bilivideo.com',
  'bstarstatic.com',
];

export function isValidProxyUrl(url: string | null | undefined): boolean {
  if (!url) return false;
  try {
    const parsed = new URL(url);
    return PROXY_ALLOWED_DOMAINS.some(
      (domain) =>
        parsed.hostname === domain || parsed.hostname.endsWith(`.${domain}`)
    );
  } catch {
    return false;
  }
}

export function decodeUrlIfNeeded(url: string): string {
  if (url?.includes('%')) {
    try {
      const decoded = decodeURIComponent(url);
      if (decoded.startsWith('http')) return decoded;
    } catch (error: unknown) {
      logger.debug(
        '[VideoController] URL decode error:',
        (error as Error).message
      );
    }
  }
  return url;
}