Skip to content

Commit

Permalink
Fix for download without bearer token and extension
Browse files Browse the repository at this point in the history
Fix for download without bearer token and extension.
  • Loading branch information
ekkelenkamp authored Mar 29, 2024
2 parents ca7142a + ed99049 commit 2c60a63
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
4 changes: 1 addition & 3 deletions src/components/download/TimeSeriesFileDownloadComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ const downloadFile = (downloadFormat: string) => {
if (props.filter) {
if (isFilterActionsFilter(props.filter)) {
const queryParameters = filterToParams(props.filter)
const encodedFileName = encodeURIComponent(fileName.value)
const url = new URL(
`${baseUrl}rest/fewspiservice/v1/timeseries/filters/actions${queryParameters}&downloadAsFile=${encodedFileName}&documentFormat=${downloadFormat}${viewPeriod}`,
`${baseUrl}rest/fewspiservice/v1/timeseries/filters/actions${queryParameters}&documentFormat=${downloadFormat}${viewPeriod}`,
)
return downloadFileAttachment(
url.href,
Expand All @@ -150,7 +149,6 @@ const downloadFile = (downloadFormat: string) => {
timeSeriesDisplayIndex: props.config?.index ?? 0,
convertDatum: props.options.convertDatum,
useDisplayUnits: props.options.useDisplayUnits,
downloadAsFile: true,
}
const queryParameters = filterToParams(timeSeriesFilter)
const url = new URL(
Expand Down
18 changes: 10 additions & 8 deletions src/lib/download/downloadFiles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { DocumentFormat } from '@deltares/fews-pi-requests'

function downloadWithLink(url: string, fileName: string) {
const encodedFileName = encodeURIComponent(fileName)
url = `${url}&downloadAsFile=${encodedFileName}`
const link = document.createElement('a')
link.href = url
link.setAttribute('download', fileName)
Expand Down Expand Up @@ -41,15 +43,15 @@ export async function downloadFileAttachment(
) {
try {
const headers = new Headers()
let extension: string = 'csv'
if (documentFormat === DocumentFormat.PI_JSON) extension = '.json'
if (documentFormat === DocumentFormat.PI_XML) extension = '.xml'
if (documentFormat === DocumentFormat.PI_CSV) extension = '.csv'
const downloadFileName = fileName + extension
if (accessToken)
if (accessToken) {
let extension: string = 'csv'
if (documentFormat === DocumentFormat.PI_JSON) extension = '.json'
if (documentFormat === DocumentFormat.PI_XML) extension = '.xml'
if (documentFormat === DocumentFormat.PI_CSV) extension = '.csv'
const downloadFileName = fileName + extension
await downloadFileWithFetch(headers, url, downloadFileName, accessToken)
if (!accessToken || accessToken == '')
downloadWithLink(url, downloadFileName)
}
if (!accessToken || accessToken == '') downloadWithLink(url, fileName)
} catch (error) {
console.error('Error downloading file:', error)
}
Expand Down
1 change: 1 addition & 0 deletions src/services/authentication/AuthenticationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class AuthenticationManager {
}

public getAccessToken(): string {
if (!configManager.authenticationIsEnabled) return ''
if (
configManager.get('VITE_REQUEST_HEADER_AUTHORIZATION') !==
RequestHeaderAuthorization.BEARER
Expand Down

0 comments on commit 2c60a63

Please sign in to comment.