mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-20 15:02:02 +00:00
fix(client): fall back to extension MIME inference when SharePoint returns octet-stream
This commit is contained in:
parent
9529c99f5e
commit
086d53d72c
2 changed files with 81 additions and 3 deletions
58
client/src/data-provider/Files/__tests__/sharepoint.spec.ts
Normal file
58
client/src/data-provider/Files/__tests__/sharepoint.spec.ts
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
jest.mock('@tanstack/react-query', () => ({
|
||||
useMutation: () => ({}),
|
||||
}));
|
||||
|
||||
import { resolveSharePointContentType, GENERIC_MIME_TYPE } from '../sharepoint';
|
||||
|
||||
describe('resolveSharePointContentType', () => {
|
||||
it('preserves specific content-type headers and strips charset/boundary parameters', () => {
|
||||
expect(resolveSharePointContentType('application/pdf; charset=UTF-8', 'document.pdf')).toBe(
|
||||
'application/pdf',
|
||||
);
|
||||
expect(resolveSharePointContentType('image/png', 'photo.png')).toBe('image/png');
|
||||
expect(resolveSharePointContentType('text/plain; charset=utf-8', 'notes.txt')).toBe(
|
||||
'text/plain',
|
||||
);
|
||||
});
|
||||
|
||||
it('falls back to getMimeTypeFromFileName when header is application/octet-stream', () => {
|
||||
expect(resolveSharePointContentType('application/octet-stream', 'data.csv')).toBe('text/csv');
|
||||
expect(
|
||||
resolveSharePointContentType('application/octet-stream; charset=binary', 'financials.xlsx'),
|
||||
).toBe('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
||||
expect(resolveSharePointContentType('application/octet-stream', 'slides.pptx')).toBe(
|
||||
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
||||
);
|
||||
expect(resolveSharePointContentType('application/octet-stream', 'doc.docx')).toBe(
|
||||
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||
);
|
||||
expect(resolveSharePointContentType('application/octet-stream', 'manual.pdf')).toBe(
|
||||
'application/pdf',
|
||||
);
|
||||
});
|
||||
|
||||
it('falls back to inferMimeType for source and code extensions when header is application/octet-stream', () => {
|
||||
expect(resolveSharePointContentType('application/octet-stream', 'script.py')).toBe(
|
||||
'text/x-python',
|
||||
);
|
||||
expect(resolveSharePointContentType('application/octet-stream', 'config.json')).toBe(
|
||||
'application/json',
|
||||
);
|
||||
expect(resolveSharePointContentType('application/octet-stream', 'readme.md')).toBe(
|
||||
'text/markdown',
|
||||
);
|
||||
});
|
||||
|
||||
it('falls back to extension resolution when header is null or undefined', () => {
|
||||
expect(resolveSharePointContentType(null, 'table.csv')).toBe('text/csv');
|
||||
expect(resolveSharePointContentType(undefined, 'app.py')).toBe('text/x-python');
|
||||
expect(resolveSharePointContentType('', 'document.pdf')).toBe('application/pdf');
|
||||
});
|
||||
|
||||
it('returns application/octet-stream when neither header nor extension can be mapped', () => {
|
||||
expect(resolveSharePointContentType('application/octet-stream', 'archive.7z')).toBe(
|
||||
GENERIC_MIME_TYPE,
|
||||
);
|
||||
expect(resolveSharePointContentType(null, 'unrecognized_file')).toBe(GENERIC_MIME_TYPE);
|
||||
});
|
||||
});
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
import { useMutation } from '@tanstack/react-query';
|
||||
import { getClientLimit } from 'librechat-data-provider';
|
||||
import type { UseMutationResult } from '@tanstack/react-query';
|
||||
import { inferMimeType } from 'librechat-data-provider';
|
||||
|
||||
export interface SharePointFile {
|
||||
id: string;
|
||||
|
|
@ -69,8 +70,10 @@ export const useSharePointBatchDownload = (): UseMutationResult<
|
|||
} finally {
|
||||
clearTimeout(abortTimer);
|
||||
}
|
||||
const contentType =
|
||||
response.headers.get('content-type') || getMimeTypeFromFileName(file.name);
|
||||
const contentType = resolveSharePointContentType(
|
||||
response.headers.get('content-type'),
|
||||
file.name,
|
||||
);
|
||||
|
||||
const downloadedFile = new File([blob], file.name, {
|
||||
type: contentType,
|
||||
|
|
@ -119,7 +122,24 @@ export const useSharePointBatchDownload = (): UseMutationResult<
|
|||
});
|
||||
};
|
||||
|
||||
function getMimeTypeFromFileName(fileName: string): string {
|
||||
export const GENERIC_MIME_TYPE = 'application/octet-stream';
|
||||
|
||||
export function resolveSharePointContentType(
|
||||
headerContentType: string | null | undefined,
|
||||
fileName: string,
|
||||
): string {
|
||||
const header = headerContentType?.split(';')[0].trim().toLowerCase() ?? '';
|
||||
if (header && header !== GENERIC_MIME_TYPE) {
|
||||
return header;
|
||||
}
|
||||
const byName = getMimeTypeFromFileName(fileName);
|
||||
if (byName !== GENERIC_MIME_TYPE) {
|
||||
return byName;
|
||||
}
|
||||
return inferMimeType(fileName, '') || GENERIC_MIME_TYPE;
|
||||
}
|
||||
|
||||
export function getMimeTypeFromFileName(fileName: string): string {
|
||||
const extension = fileName.split('.').pop()?.toLowerCase();
|
||||
|
||||
const mimeTypes: Record<string, string> = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue