Skip to content

fix: send headers for file download #355

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 2 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rustic-ai/ui-components",
"version": "0.0.41",
"version": "0.0.42",
"keywords": [
"rustic-ai",
"conversational-ui"
Expand Down
19 changes: 6 additions & 13 deletions src/components/multipart/chatCompletion/chatCompletion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import '../../../index.css'
import '../multipart/multipart.css'

import Box from '@mui/material/Box'
import IconButton from '@mui/material/IconButton'
import Tooltip from '@mui/material/Tooltip'
import Typography from '@mui/material/Typography'
import React from 'react'

import FilePreview from '../../filePreview/filePreview'
import Icon from '../../icon'
import MarkedMarkdown from '../../markdown'
import type { ChatCompletionProps, Content } from '../../types'
import DownloadButton from '../downloadButton'

function getFileName(fileUrl: string): string {
const url = new URL(fileUrl)
Expand Down Expand Up @@ -51,16 +49,11 @@ export default function ChatCompletion(props: ChatCompletionProps) {
getAuthHeaders={props.getAuthHeaders}
>
{file.url && (
<Tooltip title="Download" className="rustic-shift-to-right-by-8">
<IconButton
component="a"
href={file.url}
download={file.name}
data-cy="download-button"
>
<Icon name="download" />
</IconButton>
</Tooltip>
<DownloadButton
url={file.url}
fileName={file.name}
getAuthHeaders={props.getAuthHeaders}
/>
)}
</FilePreview>
)
Expand Down
60 changes: 60 additions & 0 deletions src/components/multipart/downloadButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import IconButton from '@mui/material/IconButton'
import Tooltip from '@mui/material/Tooltip'
import React from 'react'

import Icon from '../icon/icon'
import type { GetAuthHeaders } from '../types'

type DownloadButtonProps = {
url: string
fileName: string
getAuthHeaders?: GetAuthHeaders
}

export default function DownloadButton(props: DownloadButtonProps) {
function handleAuthenticatedDownload(getHeadersFn: GetAuthHeaders) {
getHeadersFn()
.then((headersObj) => {
return fetch(props.url, { ...headersObj })
})
.then((response) => {
return response.blob()
})
.then((blob) => {
const downloadUrl = window.URL.createObjectURL(blob)
const temporaryLink = document.createElement('a')
temporaryLink.href = downloadUrl
temporaryLink.download = props.fileName
document.body.appendChild(temporaryLink)
temporaryLink.click()

document.body.removeChild(temporaryLink)
window.URL.revokeObjectURL(downloadUrl)
})
.catch((error) => {
throw new Error('Error downloading file. ' + error.message)
})
}

return (
<Tooltip title="Download" className="rustic-shift-to-right-by-8">
{props.getAuthHeaders ? (
<IconButton
onClick={() => handleAuthenticatedDownload(props.getAuthHeaders!)}
data-cy="download-button"
>
<Icon name="download" />
</IconButton>
) : (
<IconButton
component="a"
href={props.url}
download={props.fileName}
data-cy="download-button"
>
<Icon name="download" />
</IconButton>
)}
</Tooltip>
)
}
19 changes: 6 additions & 13 deletions src/components/multipart/multipart/multipart.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import '../../../index.css'
import './multipart.css'

import IconButton from '@mui/material/IconButton'
import Tooltip from '@mui/material/Tooltip'
import Typography from '@mui/material/Typography'
import Box from '@mui/system/Box'
import React from 'react'

import FilePreview from '../../filePreview/filePreview'
import Icon from '../../icon/icon'
import MarkedMarkdown from '../../markdown'
import type { MultipartProps } from '../../types'
import DownloadButton from '../downloadButton'

/** The `Multipart` component is a versatile message format designed to accommodate both textual content and file attachments within a single message interface. */
export default function Multipart(props: MultipartProps) {
Expand All @@ -24,16 +22,11 @@ export default function Multipart(props: MultipartProps) {
key={index}
>
{file.url && (
<Tooltip title="Download" className="rustic-shift-to-right-by-8">
<IconButton
component="a"
href={file.url}
download={file.name}
data-cy="download-button"
>
<Icon name="download" />
</IconButton>
</Tooltip>
<DownloadButton
url={file.url}
fileName={file.name}
getAuthHeaders={props.getAuthHeaders}
/>
)}
</FilePreview>
)
Expand Down