utils.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import mime from 'mime'
  2. import { flatten } from 'lodash-es'
  3. import { FileAppearanceTypeEnum } from './types'
  4. import type { FileEntity } from './types'
  5. import { upload } from '@/service/base'
  6. import { FILE_EXTS } from '@/app/components/base/prompt-editor/constants'
  7. import { SupportUploadFileTypes } from '@/app/components/workflow/types'
  8. import type { FileResponse } from '@/types/workflow'
  9. import { TransferMethod } from '@/types/app'
  10. type FileUploadParams = {
  11. file: File
  12. onProgressCallback: (progress: number) => void
  13. onSuccessCallback: (res: { id: string }) => void
  14. onErrorCallback: () => void
  15. }
  16. type FileUpload = (v: FileUploadParams, isPublic?: boolean, url?: string) => void
  17. export const fileUpload: FileUpload = ({
  18. file,
  19. onProgressCallback,
  20. onSuccessCallback,
  21. onErrorCallback,
  22. }, isPublic, url) => {
  23. const formData = new FormData()
  24. formData.append('file', file)
  25. const onProgress = (e: ProgressEvent) => {
  26. if (e.lengthComputable) {
  27. const percent = Math.floor(e.loaded / e.total * 100)
  28. onProgressCallback(percent)
  29. }
  30. }
  31. upload({
  32. xhr: new XMLHttpRequest(),
  33. data: formData,
  34. onprogress: onProgress,
  35. }, isPublic, url)
  36. .then((res: { id: string }) => {
  37. onSuccessCallback(res)
  38. })
  39. .catch(() => {
  40. onErrorCallback()
  41. })
  42. }
  43. export const getFileExtension = (fileName: string, fileMimetype: string, isRemote?: boolean) => {
  44. let extension = ''
  45. if (fileMimetype)
  46. extension = mime.getExtension(fileMimetype) || ''
  47. if (fileName && !extension) {
  48. const fileNamePair = fileName.split('.')
  49. const fileNamePairLength = fileNamePair.length
  50. if (fileNamePairLength > 1)
  51. extension = fileNamePair[fileNamePairLength - 1]
  52. else
  53. extension = ''
  54. }
  55. if (isRemote)
  56. extension = ''
  57. return extension
  58. }
  59. export const getFileAppearanceType = (fileName: string, fileMimetype: string) => {
  60. const extension = getFileExtension(fileName, fileMimetype)
  61. if (extension === 'gif')
  62. return FileAppearanceTypeEnum.gif
  63. if (FILE_EXTS.image.includes(extension.toUpperCase()))
  64. return FileAppearanceTypeEnum.image
  65. if (FILE_EXTS.video.includes(extension.toUpperCase()))
  66. return FileAppearanceTypeEnum.video
  67. if (FILE_EXTS.audio.includes(extension.toUpperCase()))
  68. return FileAppearanceTypeEnum.audio
  69. if (extension === 'html')
  70. return FileAppearanceTypeEnum.code
  71. if (extension === 'pdf')
  72. return FileAppearanceTypeEnum.pdf
  73. if (extension === 'md' || extension === 'markdown')
  74. return FileAppearanceTypeEnum.markdown
  75. if (extension === 'xlsx' || extension === 'xls')
  76. return FileAppearanceTypeEnum.excel
  77. if (extension === 'docx' || extension === 'doc')
  78. return FileAppearanceTypeEnum.word
  79. if (extension === 'pptx' || extension === 'ppt')
  80. return FileAppearanceTypeEnum.ppt
  81. if (FILE_EXTS.document.includes(extension.toUpperCase()))
  82. return FileAppearanceTypeEnum.document
  83. return FileAppearanceTypeEnum.custom
  84. }
  85. export const getSupportFileType = (fileName: string, fileMimetype: string, isCustom?: boolean) => {
  86. if (isCustom)
  87. return SupportUploadFileTypes.custom
  88. const extension = getFileExtension(fileName, fileMimetype)
  89. for (const key in FILE_EXTS) {
  90. if ((FILE_EXTS[key]).includes(extension.toUpperCase()))
  91. return key
  92. }
  93. return ''
  94. }
  95. export const getProcessedFiles = (files: FileEntity[]) => {
  96. return files.filter(file => file.progress !== -1).map(fileItem => ({
  97. type: fileItem.supportFileType,
  98. transfer_method: fileItem.transferMethod,
  99. url: fileItem.url || '',
  100. upload_file_id: fileItem.uploadedId || '',
  101. }))
  102. }
  103. export const getProcessedFilesFromResponse = (files: FileResponse[]) => {
  104. return files.map((fileItem) => {
  105. return {
  106. id: fileItem.related_id,
  107. name: fileItem.filename,
  108. size: fileItem.size || 0,
  109. type: fileItem.mime_type,
  110. progress: 100,
  111. transferMethod: fileItem.transfer_method,
  112. supportFileType: fileItem.type,
  113. uploadedId: fileItem.related_id,
  114. url: fileItem.url,
  115. }
  116. })
  117. }
  118. export const getFileNameFromUrl = (url: string) => {
  119. const urlParts = url.split('/')
  120. return urlParts[urlParts.length - 1] || ''
  121. }
  122. export const getSupportFileExtensionList = (allowFileTypes: string[], allowFileExtensions: string[]) => {
  123. if (allowFileTypes.includes(SupportUploadFileTypes.custom))
  124. return allowFileExtensions.map(item => item.slice(1).toUpperCase())
  125. return allowFileTypes.map(type => FILE_EXTS[type]).flat()
  126. }
  127. export const isAllowedFileExtension = (fileName: string, fileMimetype: string, allowFileTypes: string[], allowFileExtensions: string[]) => {
  128. return getSupportFileExtensionList(allowFileTypes, allowFileExtensions).includes(getFileExtension(fileName, fileMimetype).toUpperCase())
  129. }
  130. export const getFilesInLogs = (rawData: any) => {
  131. const originalFiles = flatten(Object.keys(rawData || {}).map((key) => {
  132. if (typeof rawData[key] === 'object' || Array.isArray(rawData[key]))
  133. return rawData[key]
  134. return undefined
  135. }).filter(Boolean)).filter(item => item?.model_identity === '__dify__file__')
  136. return getProcessedFilesFromResponse(originalFiles)
  137. }
  138. export const fileIsUploaded = (file: FileEntity) => {
  139. if (file.uploadedId)
  140. return true
  141. if (file.transferMethod === TransferMethod.remote_url && file.progress === 100)
  142. return true
  143. }
  144. export const downloadFile = (url: string, filename: string) => {
  145. const anchor = document.createElement('a')
  146. anchor.href = url
  147. anchor.download = filename
  148. anchor.style.display = 'none'
  149. anchor.target = '_blank'
  150. anchor.title = filename
  151. document.body.appendChild(anchor)
  152. anchor.click()
  153. document.body.removeChild(anchor)
  154. }