getAttachmentLinks.ts 1.3 KB

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