langgenius / dify

Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.
https://dify.ai
Other
52.37k stars 7.64k forks source link

When uploading a file locally, a .docx file saved by WPS was selected. The frontend reports Unsupported file type. #9992

Open RichardFans opened 3 weeks ago

RichardFans commented 3 weeks ago

Self Checks

Dify version

0.10.2

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

When uploading a file locally, a .docx file saved by WPS was selected. The frontend reports Unsupported file type.

Analysis of the error: In web/app/components/base/file-uploader/utils.ts, the getFileExtension method is entirely dependent on the third-party package mime for determination and cannot correctly handle files where fileMimetype is application/wps-office.docx (files generated by WPS):

export const getFileExtension = (fileName: string, fileMimetype: string) => {
  if (fileMimetype)
    return mime.getExtension(fileMimetype) || ''

  if (fileName) {
    const fileNamePair = fileName.split('.')
    const fileNamePairLength = fileNamePair.length

    if (fileNamePairLength > 1)
      return fileNamePair[fileNamePairLength - 1]
  }

  return ''
}

It is suggested to adjust the method so that it does not solely rely on mime. If mime cannot handle it, directly return the suffix:

export const getFileExtension = (fileName: string, fileMimetype: string) => {
  if (fileMimetype) {
    const extensions = mime.getExtension(fileMimetype)
    if (extensions)
      return extensions
  }

  if (fileName) {
    const fileNamePair = fileName.split('.')
    const fileNamePairLength = fileNamePair.length

    if (fileNamePairLength > 1)
      return fileNamePair[fileNamePairLength - 1]
  }

  return ''
}

✔️ Expected Behavior

Capable of supporting files of WPS and other office software types.

❌ Actual Behavior

Actually not supported

dosubot[bot] commented 3 weeks ago

Hey @RichardFans, I'm here to help you with bug fixes, questions, and becoming a contributor. Let's squash those bugs together! 🐛

To address the issue with .docx files saved by WPS being reported as an unsupported file type, you can modify the getFileExtension method to handle cases where the mime type might not be correctly identified. Here's an adjusted version of the method:

export const getFileExtension = (fileName: string, fileMimetype: string) => {
  if (fileMimetype) {
    const extension = mime.getExtension(fileMimetype);
    if (extension) {
      return extension;
    }
  }

  if (fileName) {
    const fileNamePair = fileName.split('.');
    const fileNamePairLength = fileNamePair.length;

    if (fileNamePairLength > 1) {
      const extension = fileNamePair[fileNamePairLength - 1];
      // Add a specific check for .docx files saved by WPS
      if (extension.toLowerCase() === 'docx') {
        return 'docx';
      }
      return extension;
    }
  }

  return '';
}

This modification ensures that if the mime type does not provide a valid extension, the function will fall back to checking the file name, specifically handling .docx extensions [1].

To continue talking to Dosu, mention @dosu.