{"version":3,"file":"downloadUtils.mjs","names":[],"sources":["../../../../src/components/MessageActions/downloadUtils.ts"],"sourcesContent":["import {\n  type Attachment,\n  isAudioAttachment,\n  isFileAttachment,\n  isImageAttachment,\n  isVideoAttachment,\n  isVoiceRecordingAttachment,\n  type LocalAttachment,\n  type LocalUploadAttachment,\n} from 'stream-chat';\n\nexport type DownloadableAttachment = {\n  asset_url?: string;\n  image_url?: string;\n  localMetadata?: LocalUploadAttachment['localMetadata'];\n  title?: string;\n};\n\nexport const isDownloadableAttachment = (\n  attachment: unknown,\n): attachment is DownloadableAttachment => {\n  if (!attachment || typeof attachment !== 'object') return false;\n  const typedAttachment = attachment as Attachment | LocalAttachment;\n\n  return (\n    isFileAttachment(typedAttachment) ||\n    isImageAttachment(typedAttachment) ||\n    isVideoAttachment(typedAttachment) ||\n    isAudioAttachment(typedAttachment) ||\n    isVoiceRecordingAttachment(typedAttachment)\n  );\n};\n\nconst triggerUrlDownload = ({\n  filename,\n  openInNewTab,\n  revokeObjectUrl,\n  url,\n}: {\n  openInNewTab?: boolean;\n  revokeObjectUrl?: boolean;\n  url: string;\n  filename: string;\n}) => {\n  const anchor = document.createElement('a');\n  anchor.download = filename;\n  anchor.href = url;\n  anchor.rel = 'noopener noreferrer';\n  // A direct remote URL opens in a new tab when requested, so the chat stays in place.\n  if (openInNewTab && !url.startsWith('blob:')) {\n    anchor.target = '_blank';\n  }\n  document.body.append(anchor);\n  anchor.click();\n  anchor.remove();\n\n  // Revoke object URLs by default; direct network URLs are never revoked.\n  const shouldRevokeObjectUrl = revokeObjectUrl ?? url.startsWith('blob:');\n  if (shouldRevokeObjectUrl && url.startsWith('blob:')) {\n    setTimeout(() => URL.revokeObjectURL(url), 0);\n  }\n};\n\nconst fetchRemoteAttachmentAsObjectUrl = async (url: string) => {\n  if (typeof fetch !== 'function') return null;\n\n  const response = await fetch(url);\n  if (!response.ok) {\n    throw new Error(`Failed to fetch attachment, status ${response.status}`);\n  }\n  const blob = await response.blob();\n  return URL.createObjectURL(blob);\n};\n\n/**\n * Downloads an attachment from its local file when it has one, otherwise from its URL. A remote\n * URL is fetched into a blob first, because sequential direct clicks on cross-origin URLs can\n * yield a single file in Chromium and blob URLs keep the `a.download` name more reliably. When\n * the fetch fails (e.g. CORS), the URL is downloaded directly.\n *\n * @param attachment - Attachment to download. Does nothing without a local file or URL.\n * @param options.openRemoteInNewTab - Open a directly downloaded remote URL in a new tab.\n * Defaults to true.\n * @param options.preferRemoteFetch - Fetch a remote URL as a blob before downloading it.\n * Defaults to true.\n */\nexport const downloadAttachment = async (\n  attachment: DownloadableAttachment,\n  options: { openRemoteInNewTab?: boolean; preferRemoteFetch?: boolean } = {},\n) => {\n  const { openRemoteInNewTab = true, preferRemoteFetch = true } = options;\n  const titleFallback = attachment.title ?? 'downloaded-attachment';\n  const rawFile = attachment.localMetadata?.file;\n\n  if (rawFile instanceof Blob) {\n    const filename =\n      rawFile instanceof File && rawFile.name ? rawFile.name : titleFallback;\n    triggerUrlDownload({\n      filename,\n      url: URL.createObjectURL(rawFile),\n    });\n    return;\n  }\n\n  const filename =\n    rawFile &&\n    typeof rawFile === 'object' &&\n    'name' in rawFile &&\n    typeof (rawFile as { name: unknown }).name === 'string'\n      ? (rawFile as { name: string }).name\n      : titleFallback;\n\n  const url = attachment.asset_url ?? attachment.image_url;\n  if (!url) return;\n\n  if (preferRemoteFetch) {\n    try {\n      const objectUrl = await fetchRemoteAttachmentAsObjectUrl(url);\n      if (objectUrl) {\n        triggerUrlDownload({\n          filename,\n          url: objectUrl,\n        });\n        return;\n      }\n    } catch {\n      // Fall back to direct URL download when fetch is blocked (e.g. CORS/policy).\n    }\n  }\n\n  triggerUrlDownload({ filename, openInNewTab: openRemoteInNewTab, url });\n};\n\nexport const downloadAllAttachments = async (attachments: DownloadableAttachment[]) => {\n  // One at a time, to avoid a burst of simultaneous requests.\n  for (const attachment of attachments) {\n    await downloadAttachment(attachment, {\n      openRemoteInNewTab: true,\n      preferRemoteFetch: true,\n    });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