Skip to content

Fix for file upload failure issue in Safari #1001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions packages/web/src/hooks/useFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { create } from 'zustand';
import useFileApi from './useFileApi';
import { FileLimit, UploadedFileType } from 'generative-ai-use-cases';
import { produce } from 'immer';
import { fileTypeFromStream } from 'file-type';
import { fileTypeFromBuffer, fileTypeFromStream, MimeType } from 'file-type';
import { useCallback } from 'react';
import { v4 as uuidv4 } from 'uuid';
import i18next from 'i18next';
Expand Down Expand Up @@ -116,9 +116,19 @@ const useFilesState = create<{
const isMimeSpoofedResults = await Promise.all(
uploadedFiles.map(async (uploadedFile) => {
// file.type is based on the extension, while fileTypeFromStream checks the file header signature
const realMimeType = (
await fileTypeFromStream(uploadedFile.file.stream())
)?.mime;
let realMimeType: MimeType | undefined;
try {
realMimeType = (await fileTypeFromStream(uploadedFile.file.stream()))
?.mime;
} catch (error) {
realMimeType = (
await fileTypeFromBuffer(await uploadedFile.file.arrayBuffer())
)?.mime;
}
if (!realMimeType) {
console.error('Failed to get file type:', uploadedFile.file.name);
return false;
}
// exception when file is doc or xls
const isDocOrXls =
['application/msword', 'application/vnd.ms-excel'].includes(
Expand Down
Loading