Skip to content

Commit 2ef47c8

Browse files
committed
fix(DHIS2-16988): download uncompressed json rather than open inline
1 parent 6cd4f22 commit 2ef47c8

File tree

2 files changed

+82
-12
lines changed

2 files changed

+82
-12
lines changed

src/utils/helper.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,22 +188,32 @@ const uploadFile = ({
188188
})
189189
}
190190

191-
const downloadWindowTitle = i18n.t('Loading exported data')
192-
const downloadWindowHtml = `
193-
<div style="height: 100%; width: 100%; display: flex; justify-content: center; align-items: center; color: rgb(33, 41, 52)">
194-
<p>${downloadWindowTitle}</p>
195-
</div>
196-
`
197-
198191
// call stub function if available
199-
const locationAssign = (url) => {
192+
const locationAssign = (relativeUrl) => {
200193
if (window.locationAssign) {
201-
window.locationAssign(url)
194+
window.locationAssign(relativeUrl)
202195
} else {
203-
const downloadWindow = window.open(url, '_blank')
196+
try {
197+
const url = relativeUrl.startsWith('..')
198+
? new URL(relativeUrl, document.baseURI).href
199+
: relativeUrl
200+
201+
const urlFilePart = new URL(url).pathname.split('/').pop()
202+
const [, file, extension] = urlFilePart.match(/(^[^.]+)(\..+$)/)
204203

205-
downloadWindow.document.title = downloadWindowTitle
206-
downloadWindow.document.body.innerHTML = downloadWindowHtml // does not work in Chrome
204+
const downloadedFileName = `${file}${extension}`
205+
206+
const link = document.createElement('a')
207+
link.href = url
208+
link.download = downloadedFileName
209+
link.target = '_blank'
210+
link.click()
211+
212+
return link
213+
} catch (err) {
214+
console.error(err)
215+
window.open(relativeUrl, '_blank')
216+
}
207217
}
208218
}
209219

src/utils/helper.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { locationAssign } from './helper.js'
2+
3+
describe('locationAssign', () => {
4+
it('should create a file name based on the params', () => {
5+
const url =
6+
'https://debug.dhis2.org/dev/api/tracker/trackedEntities.json?ouMode=CAPTURE&format=json&includeDeleted=false&dataElementIdScheme=UID&eventIdScheme=UID&orgUnitIdScheme=UID&idScheme=UID&attachment=trackedEntities.json&paging=false&totalPages=false&program=lxAQ7Zs9VYR'
7+
const link = locationAssign(url)
8+
expect(link.download).toEqual('trackedEntities.json')
9+
})
10+
it('should create url with orgUnits', () => {
11+
const url =
12+
'https://debug.dhis2.org/dev/api/tracker/trackedEntities.json?ouMode=SELECTED&includeDeleted=false&dataElementIdScheme=UID&eventIdScheme=UID&orgUnitIdScheme=UID&idScheme=UID&attachment=trackedEntities.json&paging=false&totalPages=false&orgUnits=O6uvpzGd5pu,fdc6uOvgoji&program=kla3mAPgvCH'
13+
const link = locationAssign(url)
14+
expect(link.download).toEqual('trackedEntities.json')
15+
})
16+
it('should create url with tracked entities', () => {
17+
const url =
18+
'https://debug.dhis2.org/dev/api/tracker/trackedEntities.json?ouMode=SELECTED&includeDeleted=false&dataElementIdScheme=UID&eventIdScheme=UID&orgUnitIdScheme=UID&idScheme=UID&attachment=trackedEntities.json&paging=false&totalPages=false&orgUnits=ImspTQPwCqd&trackedEntityType=bVkFYAvoUCP'
19+
const link = locationAssign(url)
20+
expect(link.download).toEqual('trackedEntities.json')
21+
})
22+
it('should create url with CSV', () => {
23+
const url =
24+
'https://debug.dhis2.org/dev/api/tracker/trackedEntities.csv?ouMode=SELECTED&includeDeleted=false&dataElementIdScheme=UID&eventIdScheme=UID&orgUnitIdScheme=UID&idScheme=UID&attachment=trackedEntities.csv&paging=false&totalPages=false&orgUnits=ImspTQPwCqd&program=lxAQ7Zs9VYR'
25+
const link = locationAssign(url)
26+
expect(link.download).toEqual('trackedEntities.csv')
27+
})
28+
29+
it('should create url with events zip', () => {
30+
const url =
31+
'https://debug.dhis2.org/dev/api/tracker/events.json.zip?links=false&paging=false&totalPages=false&orgUnit=fwH9ipvXde9&program=VBqh0ynB2wv&includeDeleted=false&dataElementIdScheme=UID&orgUnitIdScheme=UID&idScheme=UID&attachment=events.json.zip&occurredAfter=2023-12-12&occurredBefore=2024-03-12&ouMode=CHILDREN&format=json'
32+
const link = locationAssign(url)
33+
expect(link.download).toEqual('events.json.zip')
34+
})
35+
36+
it('should create url with events gzip', () => {
37+
const url =
38+
'https://debug.dhis2.org/dev/api/tracker/events.json.gz?links=false&paging=false&totalPages=false&orgUnit=ImspTQPwCqd&program=lxAQ7Zs9VYR&includeDeleted=false&dataElementIdScheme=UID&orgUnitIdScheme=UID&idScheme=UID&attachment=events.json.gz&occurredAfter=2023-12-12&occurredBefore=2024-03-12&ouMode=SELECTED&format=json'
39+
const link = locationAssign(url)
40+
expect(link.download).toEqual('events.json.gz')
41+
})
42+
it('should work with relative URLs when bundled in DHIS2', () => {
43+
Object.defineProperty(global.document, 'baseURI', {
44+
value: 'http://localhost:8080/dhis-web-import-export/index.html#/export/tei',
45+
})
46+
const url =
47+
'../api/tracker/trackedEntities.json?ouMode=SELECTED&includeDeleted=false&dataElementIdScheme=UID&eventIdScheme=UID&orgUnitIdScheme=UID&idScheme=UID&attachment=trackedEntities.json&paging=false&totalPages=false&orgUnits=ImspTQPwCqd&program=lxAQ7Zs9VYR'
48+
const link = locationAssign(url)
49+
expect(link.download).toEqual('trackedEntities.json')
50+
})
51+
it('should work with relative URLs when bundled in DHIS2 for zip', () => {
52+
Object.defineProperty(global.document, 'baseURI', {
53+
value: 'http://localhost:8080/dhis-web-import-export/index.html#/export/tei',
54+
})
55+
const url =
56+
'../api/tracker/events.json.zip?links=false&paging=false&totalPages=false&orgUnit=ImspTQPwCqd&program=lxAQ7Zs9VYR&includeDeleted=false&dataElementIdScheme=UID&orgUnitIdScheme=UID&idScheme=UID&attachment=events.json.zip&occurredAfter=2023-12-12&occurredBefore=2024-03-12&ouMode=SELECTED&format=json'
57+
const link = locationAssign(url)
58+
expect(link.download).toEqual('events.json.zip')
59+
})
60+
})

0 commit comments

Comments
 (0)