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(chat): download chat images through the file proxy (#18784)

Innei76fecbc7145d
diff --git a/package.json b/package.json
index 66328d1c265..97543b44908 100644
--- a/package.json
+++ b/package.json
@@ -308,7 +308,7 @@
"@lobehub/icons": "^5.15.0",
"@lobehub/market-sdk": "0.39.2",
"@lobehub/tts": "^5.1.2",
- "@lobehub/ui": "^5.33.3",
+ "@lobehub/ui": "^5.35.0",
"@modelcontextprotocol/sdk": "^1.30.0",
"@neondatabase/serverless": "^1.1.0",
"@next/third-parties": "^16.3.0",
diff --git a/src/features/Conversation/Messages/User/components/ImageFileListViewer.tsx b/src/features/Conversation/Messages/User/components/ImageFileListViewer.tsx
index 9cc25765110..6f7efe3b4c6 100644
--- a/src/features/Conversation/Messages/User/components/ImageFileListViewer.tsx
+++ b/src/features/Conversation/Messages/User/components/ImageFileListViewer.tsx
@@ -4,6 +4,8 @@ import { memo } from 'react';
import GalleyGrid from '@/components/GalleyGrid';
import ImageItem from '@/components/ImageItem';
+import { downloadPreviewImage } from '../../components/downloadPreviewImage';
+
interface ImageFileItem {
alt?: string;
id: string;
@@ -18,7 +20,7 @@ interface FileListProps {
const ImageFileListViewer = memo<FileListProps>(({ items }) => {
return (
- <PreviewGroup>
+ <PreviewGroup preview={{ onDownload: downloadPreviewImage }}>
<GalleyGrid items={items} renderItem={ImageItem} />
</PreviewGroup>
);
diff --git a/src/features/Conversation/Messages/components/ImageFileListViewer.tsx b/src/features/Conversation/Messages/components/ImageFileListViewer.tsx
index 9cc25765110..e8581f9053c 100644
--- a/src/features/Conversation/Messages/components/ImageFileListViewer.tsx
+++ b/src/features/Conversation/Messages/components/ImageFileListViewer.tsx
@@ -4,6 +4,8 @@ import { memo } from 'react';
import GalleyGrid from '@/components/GalleyGrid';
import ImageItem from '@/components/ImageItem';
+import { downloadPreviewImage } from './downloadPreviewImage';
+
interface ImageFileItem {
alt?: string;
id: string;
@@ -18,7 +20,7 @@ interface FileListProps {
const ImageFileListViewer = memo<FileListProps>(({ items }) => {
return (
- <PreviewGroup>
+ <PreviewGroup preview={{ onDownload: downloadPreviewImage }}>
<GalleyGrid items={items} renderItem={ImageItem} />
</PreviewGroup>
);
diff --git a/src/features/Conversation/Messages/components/downloadPreviewImage.test.ts b/src/features/Conversation/Messages/components/downloadPreviewImage.test.ts
new file mode 100644
index 00000000000..83360d40ce8
--- /dev/null
+++ b/src/features/Conversation/Messages/components/downloadPreviewImage.test.ts
@@ -0,0 +1,35 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { downloadPreviewImage } from './downloadPreviewImage';
+
+const downloadFile = vi.hoisted(() => vi.fn());
+
+vi.mock('@lobechat/utils/client', () => ({ downloadFile }));
+
+describe('downloadPreviewImage', () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ vi.spyOn(window, 'open').mockReturnValue(null);
+ });
+
+ it('opens the proxy download response for a file proxy url', async () => {
+ await downloadPreviewImage('https://app.lobehub.com/f/file_abc');
+
+ expect(window.open).toHaveBeenCalledWith(
+ 'https://app.lobehub.com/f/file_abc?download=1',
+ '_blank',
+ 'noopener,noreferrer',
+ );
+ expect(downloadFile).not.toHaveBeenCalled();
+ });
+
+ it('downloads other sources as a blob under the file name', async () => {
+ await downloadPreviewImage('https://cdn.lobehub.com/images/cat%20photo.png');
+
+ expect(downloadFile).toHaveBeenCalledWith(
+ 'https://cdn.lobehub.com/images/cat%20photo.png',
+ 'cat photo.png',
+ );
+ expect(window.open).not.toHaveBeenCalled();
+ });
+});
diff --git a/src/features/Conversation/Messages/components/downloadPreviewImage.ts b/src/features/Conversation/Messages/components/downloadPreviewImage.ts
new file mode 100644
index 00000000000..681f7e9a2cc
--- /dev/null
+++ b/src/features/Conversation/Messages/components/downloadPreviewImage.ts
@@ -0,0 +1,27 @@
+import { downloadFile } from '@lobechat/utils/client';
+
+import { getFileDownloadUrl } from '@/features/EditorCanvas/fileDownload';
+
+const fileNameFromUrl = (source: string) => {
+ try {
+ const name = new URL(source, window.location.href).pathname.split('/').pop();
+ return name ? decodeURIComponent(name) : 'image';
+ } catch {
+ return 'image';
+ }
+};
+
+export const downloadPreviewImage = async (source: string) => {
+ const downloadUrl = getFileDownloadUrl(source, { appOrigin: window.location.origin });
+
+ // The file proxy redirects to a presigned URL on an origin that sends no CORS
+ // headers — and a cross-origin redirect strips the Origin header to `null`, so
+ // no bucket rule can allow it either. Its `download=1` response carries a
+ // Content-Disposition instead, which saves the file under its stored name.
+ if (downloadUrl !== source) {
+ window.open(downloadUrl, '_blank', 'noopener,noreferrer');
+ return;
+ }
+
+ await downloadFile(source, fileNameFromUrl(source));
+};