File tree 1 file changed +14
-4
lines changed
1 file changed +14
-4
lines changed Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import { create } from 'zustand';
2
2
import useFileApi from './useFileApi' ;
3
3
import { FileLimit , UploadedFileType } from 'generative-ai-use-cases' ;
4
4
import { produce } from 'immer' ;
5
- import { fileTypeFromStream } from 'file-type' ;
5
+ import { fileTypeFromBuffer , fileTypeFromStream , MimeType } from 'file-type' ;
6
6
import { useCallback } from 'react' ;
7
7
import { v4 as uuidv4 } from 'uuid' ;
8
8
import i18next from 'i18next' ;
@@ -116,9 +116,19 @@ const useFilesState = create<{
116
116
const isMimeSpoofedResults = await Promise . all (
117
117
uploadedFiles . map ( async ( uploadedFile ) => {
118
118
// file.type is based on the extension, while fileTypeFromStream checks the file header signature
119
- const realMimeType = (
120
- await fileTypeFromStream ( uploadedFile . file . stream ( ) )
121
- ) ?. mime ;
119
+ let realMimeType : MimeType | undefined ;
120
+ try {
121
+ realMimeType = ( await fileTypeFromStream ( uploadedFile . file . stream ( ) ) )
122
+ ?. mime ;
123
+ } catch ( error ) {
124
+ realMimeType = (
125
+ await fileTypeFromBuffer ( await uploadedFile . file . arrayBuffer ( ) )
126
+ ) ?. mime ;
127
+ }
128
+ if ( ! realMimeType ) {
129
+ console . error ( 'Failed to get file type:' , uploadedFile . file . name ) ;
130
+ return false ;
131
+ }
122
132
// exception when file is doc or xls
123
133
const isDocOrXls =
124
134
[ 'application/msword' , 'application/vnd.ms-excel' ] . includes (
You can’t perform that action at this time.
0 commit comments