Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: restore image paste in browser runtime (#17644)

Arvin Xu0a697e80b5ad
diff --git a/packages/utils/src/mimeType.test.ts b/packages/utils/src/mimeType.test.ts
index 080e196f451..104e7f82f0e 100644
--- a/packages/utils/src/mimeType.test.ts
+++ b/packages/utils/src/mimeType.test.ts
@@ -1,7 +1,23 @@
-import { describe, expect, it } from 'vitest';
+import { afterEach, describe, expect, it, vi } from 'vitest';
import { getMimeType, resolveMimeType, tryGetMimeType } from './mimeType';
+afterEach(() => {
+ vi.doUnmock('node:path');
+ vi.resetModules();
+});
+
+describe('browser compatibility', () => {
+ it('detects a pasted image without relying on Node path.extname', async () => {
+ vi.doMock('node:path', () => ({ default: {} }));
+ vi.resetModules();
+
+ const browserMimeType = await import('./mimeType');
+
+ expect(browserMimeType.getMimeType('pasted-image.png')).toBe('image/png');
+ });
+});
+
describe('getMimeType', () => {
describe('custom code file MIME types', () => {
it('should return correct MIME type for Python files', () => {
diff --git a/packages/utils/src/mimeType.ts b/packages/utils/src/mimeType.ts
index cbce71a0220..410504a2936 100644
--- a/packages/utils/src/mimeType.ts
+++ b/packages/utils/src/mimeType.ts
@@ -1,5 +1,3 @@
-import path from 'node:path';
-
import { fileTypeFromBuffer } from 'file-type';
import mime from 'mime';
@@ -38,8 +36,15 @@ const withCharset = (mimeType: string): string =>
? `${mimeType}; charset=utf-8`
: mimeType;
+const getExtension = (filePath: string): string => {
+ const fileName = filePath.replace(/^.*[/\\]/s, '');
+ const dotIndex = fileName.lastIndexOf('.');
+
+ return dotIndex > 0 ? fileName.slice(dotIndex).toLowerCase() : '';
+};
+
const lookupExtensionMime = (filePath: string): string | null => {
- const ext = path.extname(filePath).toLowerCase();
+ const ext = getExtension(filePath);
return CUSTOM_MIME_TYPES[ext] ?? mime.getType(filePath);
};