files.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import { useApplicationStore } from '@shared/stores/application'
  3. export interface ImageFileData {
  4. name: string
  5. type: string
  6. content: string
  7. }
  8. export const blobToBase64 = async (blob: Blob) =>
  9. new Promise<string>((resolve, reject) => {
  10. const reader = new FileReader()
  11. reader.onload = () => resolve(reader.result as string)
  12. reader.onerror = () => reject(reader.error)
  13. reader.readAsDataURL(blob)
  14. })
  15. export const convertFileList = async (
  16. filesList?: Maybe<FileList>,
  17. ): Promise<ImageFileData[]> => {
  18. const files = Array.from(filesList || [])
  19. const promises = files.map(async (file) => {
  20. return {
  21. name: file.name,
  22. type: file.type,
  23. content: await blobToBase64(file),
  24. }
  25. })
  26. return Promise.all(promises)
  27. }
  28. export const canDownloadFile = (type?: Maybe<string>) => {
  29. return Boolean(type && type !== 'application/pdf' && type !== 'text/html')
  30. }
  31. export const canPreviewFile = (type?: Maybe<string>) => {
  32. if (!type) return false
  33. const { config } = useApplicationStore()
  34. const allowedPreviewContentTypes =
  35. (config['active_storage.web_image_content_types'] as string[]) || []
  36. return allowedPreviewContentTypes.includes(type)
  37. }