Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
fix: download preview images instead of opening a new tab (#42419)
Coding On Stardb9b9b899345
diff --git a/web/app/components/base/file-uploader/file-uploader-in-chat-input/__tests__/file-image-item.browser.spec.tsx b/web/app/components/base/file-uploader/file-uploader-in-chat-input/__tests__/file-image-item.browser.spec.tsxindex a3f4ab0dea9..1119fb9612f 100644--- a/web/app/components/base/file-uploader/file-uploader-in-chat-input/__tests__/file-image-item.browser.spec.tsx+++ b/web/app/components/base/file-uploader/file-uploader-in-chat-input/__tests__/file-image-item.browser.spec.tsx@@ -3,10 +3,6 @@ import { page } from 'vite-plus/test/browser'import { render } from 'vitest-browser-react'import FileImageItem from '../file-image-item'-vi.mock('@/utils/download', () => ({- downloadUrl: vi.fn(),-}))-const file: FileEntity = {id: 'file-1',name: 'photo.png',diff --git a/web/app/components/base/image-uploader/__tests__/image-preview.spec.tsx b/web/app/components/base/image-uploader/__tests__/image-preview.spec.tsxindex 26df56debe6..d04e3f73951 100644--- a/web/app/components/base/image-uploader/__tests__/image-preview.spec.tsx+++ b/web/app/components/base/image-uploader/__tests__/image-preview.spec.tsx@@ -23,7 +23,8 @@ vi.mock('@/app/notifications', () => ({},}))-vi.mock('@/utils/download', () => ({+vi.mock('@/utils/download', async (importOriginal) => ({+ ...(await importOriginal<typeof import('@/utils/download')>()),downloadUrl: (...args: Parameters<typeof mocks.downloadUrl>) => mocks.downloadUrl(...args),}))@@ -482,34 +483,65 @@ describe('ImagePreview', () => {)})- it.each(['https://example.com/image.png', '/image-proxy/github/example'])(- 'should download valid url %s',- async (url) => {- const user = userEvent.setup()- render(<ImagePreview url={url} title="Preview Image" onCancel={vi.fn()} />)- const downloadButton = getDownloadButton()- await user.click(downloadButton)-- expect(mocks.downloadUrl).toHaveBeenCalledWith({- url,- fileName: 'Preview Image',- target: '_blank',- })- },- )-- it('should show error toast for invalid download url', async () => {+ it.each([+ ['https://example.com/image.png', 'Preview Image'],+ ['/image-proxy/github/example', ''],+ [dataImage, 'Screenshot.png'],+ ])('downloads image bytes without opening a tab for %s', async (url, title) => {const user = userEvent.setup()- render(<ImagePreview url="invalid://image.png" title="Preview Image" onCancel={vi.fn()} />)- const downloadButton = getDownloadButton()- await user.click(downloadButton)-- expect(mocks.notify).toHaveBeenCalledWith({- type: 'error',- message: 'Unable to open image: invalid://image.png',+ const createObjectURL = vi+ .spyOn(window.URL, 'createObjectURL')+ .mockReturnValue('blob:download-image')+ const revokeObjectURL = vi.spyOn(window.URL, 'revokeObjectURL').mockImplementation(() => {})+ const downloads: { href: string; name: string; target: string }[] = []+ vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(function (+ this: HTMLAnchorElement,+ ) {+ downloads.push({ href: this.href, name: this.download, target: this.target })})+ render(<ImagePreview url={url} title={title} onCancel={vi.fn()} />)+ await user.click(getDownloadButton())++ await waitFor(() =>+ expect(downloads).toEqual([+ {+ href: 'blob:download-image',+ name: title || 'image.png',+ target: '',+ },+ ]),+ )+ const blob = createObjectURL.mock.calls[0]?.[0] as Blob+ expect(blob.type).toBe('image/png')+ expect(await blob.text()).toBe('image')+ expect(revokeObjectURL).toHaveBeenCalledWith('blob:download-image')+ expect(mocks.windowOpen).not.toHaveBeenCalled()+ expect(getOverlay()).toBeInTheDocument()})+ it.each(['network', 'http'])(+ 'keeps the preview open when a download fails: %s',+ async (failure) => {+ const user = userEvent.setup()+ if (failure === 'network')+ vi.mocked(fetch).mockRejectedValue(new TypeError('Failed to fetch'))+ else vi.mocked(fetch).mockResolvedValue(new Response(null, { status: 403 }))+ const click = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})+ render(<ImagePreview url="https://example.com/image.png" title="" onCancel={vi.fn()} />)+ await user.click(getDownloadButton())++ await waitFor(() =>+ expect(mocks.notify).toHaveBeenCalledWith({+ type: 'error',+ message: 'common.operation.downloadFailed',+ }),+ )+ expect(click).not.toHaveBeenCalled()+ expect(mocks.windowOpen).not.toHaveBeenCalled()+ expect(getOverlay()).toBeInTheDocument()+ },+ )+it('should zoom with dedicated zoom buttons', async () => {const user = userEvent.setup()render(diff --git a/web/app/components/base/image-uploader/image-preview.tsx b/web/app/components/base/image-uploader/image-preview.tsxindex 69db31c78cc..4a67f19b11d 100644--- a/web/app/components/base/image-uploader/image-preview.tsx+++ b/web/app/components/base/image-uploader/image-preview.tsx@@ -7,7 +7,7 @@ import * as React from 'react'import { useCallback, useEffect, useRef, useState } from 'react'import { useTranslation } from 'react-i18next'import { toast } from '@/app/notifications'-import { downloadUrl } from '@/utils/download'+import { downloadBlob } from '@/utils/download'type ImagePreviewProps = {url: string@@ -72,13 +72,25 @@ const ImagePreview: FC<ImagePreviewProps> = ({ url, title, onCancel, onPrev, onN}}- const downloadImage = () => {- // Open in a new window, considering the case when the page is inside an iframe- if (url.startsWith('http') || url.startsWith('/') || url.startsWith('data:image')) {- downloadUrl({ url, fileName: title, target: '_blank' })- return+ const downloadImage = async () => {+ try {+ const response = await fetch(isBase64(url) ? `data:image/png;base64,${url}` : url)+ if (!response.ok) throw new Error('Unable to load image')+ const data = await response.blob()+ const extensions: Record<string, string> = {+ 'image/png': 'png',+ 'image/jpeg': 'jpg',+ 'image/gif': 'gif',+ 'image/webp': 'webp',+ 'image/avif': 'avif',+ 'image/svg+xml': 'svg',+ }+ const extension = extensions[data.type]+ const fileName = title.trim() || (extension ? `image.${extension}` : 'image')+ downloadBlob({ data, fileName })+ } catch {+ toast.error(t(($) => $['operation.downloadFailed'], { ns: 'common' }))}- toast.error(`Unable to open image: ${url}`)}const zoomIn = () => {