{"version":3,"file":"mediaKinds.cjs","sources":["../../../../../../../src/components/ui/editor/ihub-editor/upload/mediaKinds.ts"],"sourcesContent":["import type { MediaKind } from \"../types\";\n\nexport const ACCEPT_BY_KIND: Record<MediaKind, string> = {\n  image: \"image/*\",\n  video: \"video/*\",\n  audio: \"audio/*\",\n  pdf: \"application/pdf,.pdf\",\n  file: \"*/*\",\n  embed: \"\",\n};\n\nexport const DEFAULT_MAX_SIZE_MB: Record<MediaKind, number> = {\n  image: 10,\n  video: 500,\n  audio: 100,\n  pdf: 50,\n  file: 100,\n  embed: 0,\n};\n\nexport const KIND_LABEL: Record<MediaKind, string> = {\n  image: \"image\",\n  video: \"video\",\n  audio: \"audio\",\n  pdf: \"PDF\",\n  file: \"file\",\n  embed: \"embed\",\n};\n\n/** Pick the block kind that best displays a file. */\nexport function kindFromMime(mime: string, fileName = \"\"): MediaKind {\n  const type = (mime || \"\").toLowerCase();\n  if (type.startsWith(\"image/\")) return \"image\";\n  if (type.startsWith(\"video/\")) return \"video\";\n  if (type.startsWith(\"audio/\")) return \"audio\";\n  if (type === \"application/pdf\" || /\\.pdf$/i.test(fileName)) return \"pdf\";\n  return \"file\";\n}\n\n/** Guess a kind from a pasted URL's extension (used for \"Embed link\"). */\nexport function kindFromUrl(url: string, fallback: MediaKind): MediaKind {\n  const path = url.split(/[?#]/)[0].toLowerCase();\n  if (/\\.(png|jpe?g|gif|webp|svg|avif)$/.test(path)) return \"image\";\n  if (/\\.(mp4|webm|ogg|mov|m4v)$/.test(path)) return \"video\";\n  if (/\\.(mp3|wav|m4a|aac|oga)$/.test(path)) return \"audio\";\n  if (/\\.pdf$/.test(path)) return \"pdf\";\n  if (toEmbedUrl(url)) return \"embed\";\n  return fallback;\n}\n\nexport function formatBytes(bytes?: number | null): string {\n  if (!bytes || bytes < 0) return \"\";\n  const units = [\"B\", \"KB\", \"MB\", \"GB\"];\n  const exp = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), units.length - 1);\n  const value = bytes / Math.pow(1024, exp);\n  return `${value.toFixed(exp === 0 ? 0 : 1)} ${units[exp]}`;\n}\n\nexport function fileExtension(name?: string | null): string {\n  const match = (name || \"\").match(/\\.([a-z0-9]+)$/i);\n  return match ? match[1].toUpperCase() : \"FILE\";\n}\n\nexport function fileNameFromUrl(url: string): string {\n  try {\n    const last = new URL(url).pathname.split(\"/\").filter(Boolean).pop();\n    return last ? decodeURIComponent(last) : url;\n  } catch {\n    return url;\n  }\n}\n\nconst EMBED_PROVIDERS: Array<{ regex: RegExp; toUrl: (m: RegExpMatchArray) => string }> = [\n  {\n    regex: /(?:youtube\\.com\\/(?:watch\\?(?:.*&)?v=|embed\\/|shorts\\/)|youtu\\.be\\/)([\\w-]{6,})/i,\n    toUrl: (m) => `https://www.youtube.com/embed/${m[1]}`,\n  },\n  {\n    regex: /vimeo\\.com\\/(?:video\\/)?(\\d+)/i,\n    toUrl: (m) => `https://player.vimeo.com/video/${m[1]}`,\n  },\n  {\n    regex: /loom\\.com\\/(?:share|embed)\\/([\\w-]+)/i,\n    toUrl: (m) => `https://www.loom.com/embed/${m[1]}`,\n  },\n  {\n    regex: /drive\\.google\\.com\\/file\\/d\\/([\\w-]+)/i,\n    toUrl: (m) => `https://drive.google.com/file/d/${m[1]}/preview`,\n  },\n  {\n    regex: /(https:\\/\\/(?:www\\.)?figma\\.com\\/(?:file|design|proto)\\/[^\\s]+)/i,\n    toUrl: (m) => `https://www.figma.com/embed?embed_host=instincthub&url=${encodeURIComponent(m[1])}`,\n  },\n  {\n    regex: /(https:\\/\\/codepen\\.io\\/[\\w-]+)\\/(?:pen|embed)\\/([\\w-]+)/i,\n    toUrl: (m) => `${m[1]}/embed/${m[2]}?default-tab=result`,\n  },\n];\n\n/** Convert a known provider URL into its embeddable iframe URL, or null. */\nexport function toEmbedUrl(url: string): string | null {\n  const trimmed = (url || \"\").trim();\n  if (!/^https?:\\/\\//i.test(trimmed)) return null;\n  for (const provider of EMBED_PROVIDERS) {\n    const match = trimmed.match(provider.regex);\n    if (match) return provider.toUrl(match);\n  }\n  return null;\n}\n\nconst EMBED_HOSTS = [\"youtube.com\", \"youtube-nocookie.com\", \"player.vimeo.com\", \"loom.com\", \"drive.google.com\", \"figma.com\", \"codepen.io\"];\n\n/** Embeds are limited to known providers so pasted HTML can't frame arbitrary sites. */\nexport function isAllowedEmbed(url: string): boolean {\n  try {\n    const { protocol, hostname } = new URL(url);\n    if (protocol !== \"https:\") return false;\n    return EMBED_HOSTS.some((host) => hostname === host || hostname.endsWith(`.${host}`));\n  } catch {\n    return false;\n  }\n}\n\n/** Accept only plain widths (e.g. \"60%\", \"480px\") so the attribute can't inject CSS. */\nexport function safeWidth(value: unknown): string | null {\n  return typeof value === \"string\" && /^\\d{1,4}(px|%)$/.test(value.trim()) ? value.trim() : null;\n}\n\n/** Only allow http(s) and site-relative URLs into src/href attributes. */\nexport function isSafeUrl(url?: string | null): boolean {\n  if (!url) return false;\n  const trimmed = url.trim();\n  const siteRelative = trimmed.startsWith(\"/\") && !trimmed.startsWith(\"//\");\n  return /^https?:\\/\\//i.test(trimmed) || siteRelative || trimmed.startsWith(\"blob:\");\n}\n\n/** Provider players need scripts + same-origin; forms and top navigation stay blocked. */\nexport const EMBED_SANDBOX = \"allow-scripts allow-same-origin allow-presentation allow-popups\";\n\n/** The src to render for a media block, or \"\" when it isn't allowed for that kind. */\nexport function renderableSrc(attrs: { kind: MediaKind; src?: string | null }): string {\n  if (!isSafeUrl(attrs.src)) return \"\";\n  const src = (attrs.src as string).trim();\n  if (attrs.kind === \"embed\" && !isAllowedEmbed(src)) return \"\";\n  return src;\n}\n\n/**\n * PDFs get an inline viewer only when the URL is really a .pdf (or a local blob).\n * Chrome won't render PDFs in a sandboxed iframe, so this check is what stops a\n * \"PDF\" block from framing an arbitrary page; other URLs render as a download card.\n */\nexport function isInlinePdf(src: string): boolean {\n  if (src.startsWith(\"blob:\")) return true;\n  return /\\.pdf$/i.test(src.split(/[?#]/)[0]);\n}\n"],"names":["EMBED_PROVIDERS","regex","toUrl","m","concat","encodeURIComponent","toEmbedUrl","url","trimmed","trim","test","_step","_iterator","_createForOfIteratorHelper","createForOfIteratorHelper","s","n","done","provider","value","match","err","e","f","EMBED_HOSTS","isAllowedEmbed","_URL","URL","protocol","hostname","some","host","endsWith","_unused2","isSafeUrl","siteRelative","startsWith","image","video","audio","pdf","file","embed","name","toUpperCase","last","pathname","split","filter","Boolean","pop","decodeURIComponent","_unused","bytes","units","exp","Math","min","floor","log","length","pow","toFixed","src","mime","fileName","arguments","undefined","type","toLowerCase","fallback","path","attrs","kind"],"mappings":"8IAwEA,IAAMA,EAAoF,CACxF,CACEC,MAAO,mFACPC,MAAO,SAACC,GAAC,MAAA,iCAAAC,OAAsCD,EAAE,GAAE,GAErD,CACEF,MAAO,iCACPC,MAAO,SAACC,GAAC,MAAA,kCAAAC,OAAuCD,EAAE,GAAE,GAEtD,CACEF,MAAO,wCACPC,MAAO,SAACC,GAAC,MAAA,8BAAAC,OAAmCD,EAAE,GAAE,GAElD,CACEF,MAAO,yCACPC,MAAO,SAACC,GAAC,MAAA,mCAAAC,OAAwCD,EAAE,GAAE,WAAA,GAEvD,CACEF,MAAO,mEACPC,MAAO,SAACC,GAAC,MAAAC,0DAAAA,OAA+DC,mBAAmBF,EAAE,IAAG,GAElG,CACEF,MAAO,4DACPC,MAAO,SAACC,GAAC,MAAA,GAAAC,OAAQD,EAAE,GAAEC,WAAAA,OAAUD,EAAE,GAAE,sBAAA,IAKjC,SAAUG,EAAWC,GACzB,IAAMC,GAAWD,GAAO,IAAIE,OAC5B,IAAK,gBAAgBC,KAAKF,GAAU,OAAO,KAAK,IACVG,EADUC,EAAAC,EAAAC,0BACzBd,GAAe,IAAtC,IAAAY,EAAAG,MAAAJ,EAAAC,EAAAI,KAAAC,MAAwC,CAAA,IAA7BC,EAAQP,EAAAQ,MACXC,EAAQZ,EAAQY,MAAMF,EAASjB,OACrC,GAAImB,EAAO,OAAOF,EAAShB,MAAMkB,EAClC,CAAA,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