getArticleAttachmentsLinks.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type { ConfigList } from '#shared/types/store.ts'
  3. import { canDownloadFile } from '#shared/utils/files.ts'
  4. interface Attachment {
  5. type?: Maybe<string>
  6. internalId: number
  7. articleInternalId: number
  8. ticketInternalId: number
  9. }
  10. export const getArticleAttachmentsLinks = (
  11. attachment: Attachment,
  12. config: ConfigList,
  13. ) => {
  14. const buildBaseUrl = () => {
  15. const { ticketInternalId, articleInternalId, internalId } = attachment
  16. const apiUrl = config.api_path
  17. return `${apiUrl}/ticket_attachment/${ticketInternalId}/${articleInternalId}/${internalId}`
  18. }
  19. const buildPreviewUrl = (baseUrl: string) => `${baseUrl}?view=preview`
  20. const buildInlineUrl = (baseUrl: string) => `${baseUrl}?view=inline`
  21. const canDownloadAttachment = (attachment: { type?: Maybe<string> }) => {
  22. return canDownloadFile(attachment.type)
  23. }
  24. const buildDownloadUrl = (baseUrl: string, canDownload: boolean) => {
  25. const dispositionParams = canDownload ? '?disposition=attachment' : ''
  26. return `${baseUrl}${dispositionParams}`
  27. }
  28. const baseUrl = buildBaseUrl()
  29. const previewUrl = buildPreviewUrl(baseUrl)
  30. const canDownload = canDownloadAttachment(attachment)
  31. const downloadUrl = buildDownloadUrl(baseUrl, canDownload)
  32. const inlineUrl = buildInlineUrl(baseUrl)
  33. return {
  34. baseUrl,
  35. inlineUrl,
  36. previewUrl,
  37. canDownload,
  38. downloadUrl,
  39. }
  40. }