{"version":3,"file":"files.mjs","sources":["../../../../admin/src/future/utils/files.ts"],"sourcesContent":["import byteSize from 'byte-size';\n\nconst MAX_URLS = 20;\n\n/**\n * Formats a byte value into a human-readable string with units.\n *\n * @param receivedBytes - The number of bytes to format (as number or string)\n * @param decimals - The number of decimal places to display (default: 0)\n * @returns A formatted string with value and unit (e.g., \"1.5MB\")\n *\n * @example\n * ```ts\n * formatBytes(1024) // '1KB'\n * formatBytes(1536, 1) // '1.5KB'\n * ```\n */\nexport function formatBytes(receivedBytes: number | string, decimals = 0) {\n  const realBytes = typeof receivedBytes === 'string' ? Number(receivedBytes) : receivedBytes;\n  const { value, unit } = byteSize(realBytes * 1000, { precision: decimals });\n\n  if (!unit) {\n    return '0B';\n  }\n\n  return `${value}${unit.toUpperCase()}`;\n}\n\n/**\n * Extracts the file extension from a string, removing the leading dot if present.\n *\n * @param ext - The file extension string (may include leading dot)\n * @returns The extension without leading dot, or the original value if no dot\n *\n * @example\n * ```ts\n * getFileExtension('.jpg') // 'jpg'\n * getFileExtension('png') // 'png'\n * ```\n */\nexport const getFileExtension = (ext?: string | null) =>\n  ext && ext[0] === '.' ? ext.substring(1) : ext;\n\n/**\n * Prefixes a relative file URL with the backend URL if needed.\n *\n * @param fileURL - The file URL to potentially prefix\n * @returns The full URL with backend prefix if it was relative, otherwise unchanged\n *\n * @example\n * ```ts\n * prefixFileUrlWithBackendUrl('/uploads/image.jpg') // 'http://localhost:1337/uploads/image.jpg'\n * prefixFileUrlWithBackendUrl('https://cdn.example.com/image.jpg') // 'https://cdn.example.com/image.jpg'\n * ```\n */\nexport const prefixFileUrlWithBackendUrl = (fileURL?: string) => {\n  return !!fileURL && fileURL.startsWith('/') ? `${window.strapi.backendURL}${fileURL}` : fileURL;\n};\n\n/**\n * Extracts a filename from a URL's pathname.\n *\n * @param url - The URL to extract the filename from\n * @returns The filename extracted from the URL path, or 'file' if none found\n *\n * @example\n * ```ts\n * getFilenameFromUrl('https://example.com/images/photo.jpg') // 'photo.jpg'\n * getFilenameFromUrl('https://example.com/') // 'file'\n * ```\n */\nexport function getFilenameFromUrl(url: string): string {\n  const pathname = new URL(url).pathname;\n  const filename = pathname.split('/').pop();\n  return filename || 'file';\n}\n\n/**\n * Validates a newline-separated string of URLs.\n *\n * @param urlsString - A string containing URLs separated by newlines\n * @returns An object with validated URLs array and optional error message\n *\n * @example\n * ```ts\n * const { urls, error } = validateUrls('https://example.com/a.jpg\\nhttps://example.com/b.jpg');\n * if (error) {\n *   console.error(error);\n * } else {\n *   console.log(urls); // ['https://example.com/a.jpg', 'https://example.com/b.jpg']\n * }\n * ```\n */\nexport function validateUrls(urlsString: string): { urls: string[]; error: string | null } {\n  const urls = urlsString\n    .split(/\\r?\\n/)\n    .map((url) => url.trim())\n    .filter(Boolean);\n\n  if (urls.length === 0) {\n    return { urls: [], error: 'Please provide at least one URL' };\n  }\n\n  if (urls.length > MAX_URLS) {\n    return { urls: [], error: `Maximum ${MAX_URLS} URLs allowed` };\n  }\n\n  const invalidUrls: string[] = [];\n  for (const url of urls) {\n    try {\n      new URL(url);\n    } catch {\n      invalidUrls.push(url);\n    }\n  }\n\n  if (invalidUrls.length > 0) {\n    const message =\n      invalidUrls.length === 1\n        ? `Invalid URL: ${invalidUrls[0]}`\n        : `${invalidUrls.length} invalid URLs found`;\n    return { urls: [], error: message };\n  }\n\n  return { urls, error: null };\n}\n"],"names":["MAX_URLS","formatBytes","receivedBytes","decimals","realBytes","Number","value","unit","byteSize","precision","toUpperCase","getFileExtension","ext","substring","prefixFileUrlWithBackendUrl","fileURL","startsWith","window","strapi","backendURL","getFilenameFromUrl","url","pathname","URL","filename","split","pop","validateUrls","urlsString","urls","map","trim","filter","Boolean","length","error","invalidUrls","push","message"],"mappings":";;AAEA,MAAMA,QAAAA,GAAW,EAAA;AAEjB;;;;;;;;;;;;AAYC,IACM,SAASC,WAAAA,CAAYC,aAA8B,EAAEC,WAAW,CAAC,EAAA;AACtE,IAAA,MAAMC,SAAAA,GAAY,OAAO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