import { DEFAULT_ENCODING_KEY } from "@/constants/encoding"; export const removeHttpS = (url: string): string => { if (url.startsWith("http://")) { return url.slice(7); } else if (url.startsWith("https://")) { return url.slice(8); } return url; }; export const encodeVideoUrlPath = (input: string): string => { return encodeURIComponent(input.replace(/^\/+/, '')) }; export const decodeVideoUrlPath = (input: string): string => { return `/${decodeURIComponent(input)}`; }; const getEncodingKey = ():string => { return process.env.ENCODING_KEY ?? DEFAULT_ENCODING_KEY; } export function encodeUrl(url: string): string { const key = getEncodingKey() // Convert the URL and key to UTF-8 bytes const urlBytes = new TextEncoder().encode(url); const keyBytes = new TextEncoder().encode(key); // XOR the bytes of the URL with the key bytes const encodedBytes = urlBytes.map((byte, index) => byte ^ keyBytes[index % keyBytes.length]); // Convert the XORed bytes to a base64 string //@ts-ignore return btoa(String.fromCharCode(...encodedBytes)); } export function decodeUrl(encodedUrl: string): string { const key = getEncodingKey() // Decode the base64 string to get the XORed bytes const encodedBytes = Uint8Array.from(atob(encodedUrl), char => char.charCodeAt(0)); const keyBytes = new TextEncoder().encode(key); // XOR the encoded bytes with the key bytes to get the original URL bytes const urlBytes = encodedBytes.map((byte, index) => byte ^ keyBytes[index % keyBytes.length]); // Convert the bytes back to a string return new TextDecoder().decode(urlBytes); }