File tree 1 file changed +15
-4
lines changed
1 file changed +15
-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,20 @@ 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 = (
122
+ await fileTypeFromStream ( uploadedFile . file . stream ( ) )
123
+ ) ?. mime ;
124
+ } catch ( error ) {
125
+ realMimeType = (
126
+ await fileTypeFromBuffer ( await uploadedFile . file . arrayBuffer ( ) )
127
+ ) ?. mime ;
128
+ }
129
+ if ( ! realMimeType ) {
130
+ console . error ( 'Failed to get file type:' , uploadedFile . file . name ) ;
131
+ return false ;
132
+ }
122
133
// exception when file is doc or xls
123
134
const isDocOrXls =
124
135
[ 'application/msword' , 'application/vnd.ms-excel' ] . includes (
You can’t perform that action at this time.
0 commit comments