Skip to content

Commit 95ba0a9

Browse files
committed
fix: download util improvements
1 parent a8bfc08 commit 95ba0a9

File tree

3 files changed

+69
-27
lines changed

3 files changed

+69
-27
lines changed

admin/app/services/docker_service.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ export class DockerService {
333333
'https://github.com/Crosstalk-Solutions/project-nomad/raw/refs/heads/master/install/wikipedia_en_100_mini_2025-06.zim'
334334
const zimPath = '/zim/wikipedia_en_100_mini_2025-06.zim'
335335
const filepath = path.join(DockerService.NOMAD_STORAGE_ABS_PATH, zimPath)
336+
logger.info(`[DockerService] Kiwix Serve pre-install: Downloading ZIM file to ${filepath}`)
336337

337338
this._broadcast(
338339
DockerService.KIWIX_SERVICE_NAME,
@@ -345,17 +346,26 @@ export class DockerService {
345346
`Downloading Wikipedia ZIM file from ${WIKIPEDIA_ZIM_URL}. This may take some time...`
346347
)
347348

348-
await doSimpleDownload({
349-
url: WIKIPEDIA_ZIM_URL,
350-
filepath,
351-
timeout: 60000,
352-
})
349+
try {
350+
await doSimpleDownload({
351+
url: WIKIPEDIA_ZIM_URL,
352+
filepath,
353+
timeout: 60000,
354+
})
353355

354-
this._broadcast(
355-
DockerService.KIWIX_SERVICE_NAME,
356-
'preinstall',
357-
`Downloaded Wikipedia ZIM file to ${filepath}`
358-
)
356+
this._broadcast(
357+
DockerService.KIWIX_SERVICE_NAME,
358+
'preinstall',
359+
`Downloaded Wikipedia ZIM file to ${filepath}`
360+
)
361+
} catch (error) {
362+
this._broadcast(
363+
DockerService.KIWIX_SERVICE_NAME,
364+
'preinstall-error',
365+
`Failed to download Wikipedia ZIM file: ${error.message}`
366+
)
367+
throw new Error(`Pre-install action failed: ${error.message}`)
368+
}
359369
}
360370

361371
private _broadcast(service: string, status: string, message: string) {

admin/app/utils/downloads.ts

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
DoResumableDownloadWithRetryParams,
66
DoSimpleDownloadParams,
77
} from '../../types/downloads.js'
8-
import axios from 'axios'
8+
import axios, { AxiosResponse } from 'axios'
99
import { Transform } from 'stream'
1010
import { deleteFileIfExists, ensureDirectoryExists, getFileStatsIfExists } from './fs.js'
1111
import { createWriteStream } from 'fs'
@@ -21,24 +21,56 @@ export async function doSimpleDownload({
2121
timeout = 30000,
2222
signal,
2323
}: DoSimpleDownloadParams): Promise<string> {
24-
const dirname = path.dirname(filepath)
25-
await ensureDirectoryExists(dirname)
24+
return new Promise(async (resolve, reject) => {
25+
let response: AxiosResponse<any> | undefined
26+
let writer: ReturnType<typeof createWriteStream> | undefined
27+
28+
const cleanup = (err?: Error) => {
29+
try {
30+
response?.data?.destroy?.()
31+
} catch {}
32+
try {
33+
writer?.destroy?.()
34+
} catch {}
35+
if (err) {
36+
try {
37+
logger.error(`Download failed for ${url}: ${err.message}`)
38+
} catch {}
39+
reject(err)
40+
}
41+
}
2642

27-
const response = await axios.get(url, {
28-
responseType: 'stream',
29-
signal,
30-
timeout,
31-
})
32-
const writer = createWriteStream(filepath)
33-
response.data.pipe(writer)
43+
try {
44+
const dirname = path.dirname(filepath)
45+
await ensureDirectoryExists(dirname)
3446

35-
return new Promise((resolve, reject) => {
36-
writer.on('finish', () => {
37-
resolve(filepath)
38-
})
39-
writer.on('error', (error) => {
40-
reject(error)
41-
})
47+
response = await axios.get(url, {
48+
responseType: 'stream',
49+
signal,
50+
timeout,
51+
})
52+
53+
writer = createWriteStream(filepath)
54+
response?.data.pipe(writer)
55+
56+
response?.data.on('error', cleanup)
57+
writer?.on('error', cleanup)
58+
59+
writer?.on('finish', () => {
60+
cleanup()
61+
resolve(filepath)
62+
})
63+
64+
signal?.addEventListener(
65+
'abort',
66+
() => {
67+
cleanup(new Error('Download aborted'))
68+
},
69+
{ once: true }
70+
)
71+
} catch (error) {
72+
cleanup(error as Error)
73+
}
4274
})
4375
}
4476

admin/app/utils/url.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)