Skip to content

Commit

Permalink
updated file download function
Browse files Browse the repository at this point in the history
  • Loading branch information
flutistar committed Jan 13, 2025
1 parent 4a576bd commit a715881
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,27 +315,14 @@ export default defineComponent({
state.isDownloading = true
const documentClass = 'CORP'
try {
const docUrl: string = await BusinessService.getDownloadUrl(documentKey, documentClass)
const link = document.createElement('a')
link.href = docUrl
link.download = documentName
link.target = '_blank' // This opens the link in a new browser tab
// Append to the document and trigger the download
document.body.appendChild(link)
link.click()
// Remove the link after the download is triggered
document.body.removeChild(link)
state.isDownloading = false
} catch (error) {
await BusinessService.downloadDocument(documentKey, documentName, documentClass).catch(error => {
// eslint-disable-next-line no-console
console.log('downloadDocument() error =', error)
state.dialogTitle = 'Unable to download document'
state.dialogText = 'An error occurred while downloading the document. Please try again.'
const v = errorDialogComponent.value as any; v.open()
}
})
state.isDownloading = false
}
/**
Expand Down
77 changes: 19 additions & 58 deletions auth-web/src/services/business.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default class BusinessService {
static async fetchFiling (url: string): Promise<any> {
// safety check
if (!url) throw new Error('Invalid parameters')

return axios.get(url)
.then(response => {
const filing = response?.data?.filing
Expand Down Expand Up @@ -158,73 +158,34 @@ export default class BusinessService {
return axios.post(url, data)
}

/**
* Downloads a Minio document from Legal API and prompts browser to open/save it.
* @param documentKey the document key
* @param documentName the document filename
* @returns a promise to return the axios response or the error response
* @see CommonUtils.fileDownload() for a similar method
*/
static async downloadDocument (documentKey: string, documentName: string): Promise<AxiosResponse> {
// safety checks
if (!documentKey || !documentName) throw new Error('Invalid parameters')

const url = `${ConfigHelper.getLegalAPIV2Url()}/documents/${documentKey}`
const config = {
headers: { 'Accept': 'application/pdf' },
responseType: 'blob' as 'json'
}

return axios.get(url, config).then(response => {
if (!response) throw new Error('Null response')

/* solution below is from https://github.com/axios/axios/issues/1392 */

// it is necessary to create a new blob object with mime-type explicitly set
// otherwise only Chrome works like it should
const blob = new Blob([response.data], { type: 'application/pdf' })

// use Navigator.msSaveOrOpenBlob if available (possibly IE)
// warning: this is now deprecated
// ref: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/msSaveOrOpenBlob
if (window.navigator && window.navigator['msSaveOrOpenBlob']) {
window.navigator['msSaveOrOpenBlob'](blob, documentName)
} else {
// for other browsers, create a link pointing to the ObjectURL containing the blob
const url = window.URL.createObjectURL(blob)
const a = window.document.createElement('a')
window.document.body.appendChild(a)
a.setAttribute('style', 'display: none')
a.href = url
a.download = documentName
a.click()
window.URL.revokeObjectURL(url)
a.remove()
}

return response
})
}

/**
* Get download url from Document Record Service
* @param documentServiceId the unique id on Document Record Service
* @param documentName the file name to download.
* @param documentClass the document class defined for the document service. e.g. 'CORP'
* @returns a promise to return the string of file download.
* @returns void
*/
static async getDownloadUrl (documentKey: string, documentClass: string): Promise<string> {
static async downloadDocument (documentServiceId: string, documentName: string, documentClass: string): Promise<void> {
// safety checks
if (!documentKey || !documentClass) throw new Error('Invalid parameters')
if (!documentServiceId || !documentClass) throw new Error('Invalid parameters')

const url = `${ConfigHelper.getLegalAPIV2Url()}/documents/drs/${documentClass}/${documentKey}`
const url = `${ConfigHelper.getLegalAPIV2Url()}/documents/drs/${documentClass}/${documentServiceId}`

return axios.get(url).then(response => {
axios.get(url).then(response => {
if (!response) throw new Error('Null response')

return response.data.documentURL
}).catch(error => {
console.log(error)
return ''
const docUrl: string = response.data.documentURL
const link = document.createElement('a')
link.href = docUrl
link.download = documentName
link.target = '_blank' // This opens the link in a new browser tab

// Append to the document and trigger the download
document.body.appendChild(link)
link.click()

// Remove the link after the download is triggered
document.body.removeChild(link)
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,13 @@ describe('HomeJurisdictionInformation component', () => {
})

it('rendered a functional authorization download button', () => {
BusinessService.getDownloadUrl = vi.fn().mockResolvedValue(null)
BusinessService.downloadDocument = vi.fn().mockResolvedValue(null)

const button = wrapper.findAll('section').at(5).find('.download-authorization-btn')
button.trigger('click')
expect(BusinessService.getDownloadUrl).toHaveBeenCalledWith(
expect(BusinessService.downloadDocument).toHaveBeenCalledWith(
'DS01000000',
'My Authorization Document.pdf',
documentClass
)

Expand Down

0 comments on commit a715881

Please sign in to comment.