MCPcopy Create free account
hub / github.com/APIParkLab/APIPark / downloadFile

Function downloadFile

frontend/packages/common/src/utils/download.ts:34–63  ·  view source on GitHub ↗
({ body, contentType, responseType, filename, uri }: DownloadOptions)

Source from the content-addressed store, hash-verified

32}
33
34export const downloadFile = ({ body, contentType, responseType, filename, uri }: DownloadOptions): void => {
35 let content = body
36
37 // Decode the body if needed.
38 if (responseType === 'longText' || responseType === 'stream') {
39 content = decodeBase64ToUnicode(content)
40 } else if (responseType === 'text') {
41 try {
42 content = JSON.stringify(JSON.parse(content), null, 4)
43 } catch {
44 // Fallback to raw content on parsing error.
45 }
46 }
47
48 // Create a Blob URL for downloading.
49 const blobUrl = contentType.startsWith('image') ? uri : createBlobUrl(content, contentType)
50 const downloadFilename = decodeURI(filename || 'default_name')
51
52 // Create a temporary anchor element for downloading.
53 const anchor = document.createElement('a')
54 anchor.style.display = 'none'
55 anchor.href = blobUrl
56 anchor.download = downloadFilename
57 document.body.appendChild(anchor)
58 anchor.click()
59 document.body.removeChild(anchor)
60
61 // Revoke the Blob URL to free resources.
62 URL.revokeObjectURL(blobUrl)
63}
64
65export const downloadFileFromText = (fileName: string, text: string) => {
66 const file = new Blob([text], { type: 'data:text/plain;charset=utf-8' })

Callers 1

ApiResponseFunction · 0.90

Calls 3

decodeBase64ToUnicodeFunction · 0.85
createBlobUrlFunction · 0.85
createElementMethod · 0.45

Tested by

no test coverage detected