Addressed a critical issue where the application would become unresponsive at the 99 percent mark during model download
118 lines
4.0 KiB
TypeScript
118 lines
4.0 KiB
TypeScript
import { app, ipcMain } from 'electron'
|
|
import { resolve, join } from 'path'
|
|
import { WindowManager } from './../managers/window'
|
|
import request from 'request'
|
|
import { createWriteStream, renameSync } from 'fs'
|
|
import { DownloadEvent, DownloadRoute } from '@janhq/core'
|
|
const progress = require('request-progress')
|
|
import { DownloadManager } from '@janhq/core/node'
|
|
|
|
export function handleDownloaderIPCs() {
|
|
/**
|
|
* Handles the "pauseDownload" IPC message by pausing the download associated with the provided fileName.
|
|
* @param _event - The IPC event object.
|
|
* @param fileName - The name of the file being downloaded.
|
|
*/
|
|
ipcMain.handle(DownloadRoute.pauseDownload, async (_event, fileName) => {
|
|
DownloadManager.instance.networkRequests[fileName]?.pause()
|
|
})
|
|
|
|
/**
|
|
* Handles the "resumeDownload" IPC message by resuming the download associated with the provided fileName.
|
|
* @param _event - The IPC event object.
|
|
* @param fileName - The name of the file being downloaded.
|
|
*/
|
|
ipcMain.handle(DownloadRoute.resumeDownload, async (_event, fileName) => {
|
|
DownloadManager.instance.networkRequests[fileName]?.resume()
|
|
})
|
|
|
|
/**
|
|
* Handles the "abortDownload" IPC message by aborting the download associated with the provided fileName.
|
|
* The network request associated with the fileName is then removed from the networkRequests object.
|
|
* @param _event - The IPC event object.
|
|
* @param fileName - The name of the file being downloaded.
|
|
*/
|
|
ipcMain.handle(DownloadRoute.abortDownload, async (_event, fileName) => {
|
|
const rq = DownloadManager.instance.networkRequests[fileName]
|
|
if (rq) {
|
|
DownloadManager.instance.networkRequests[fileName] = undefined
|
|
rq?.abort()
|
|
} else {
|
|
WindowManager?.instance.currentWindow?.webContents.send(
|
|
DownloadEvent.onFileDownloadError,
|
|
{
|
|
fileName,
|
|
err: { message: 'aborted' },
|
|
}
|
|
)
|
|
}
|
|
})
|
|
|
|
/**
|
|
* Downloads a file from a given URL.
|
|
* @param _event - The IPC event object.
|
|
* @param url - The URL to download the file from.
|
|
* @param fileName - The name to give the downloaded file.
|
|
*/
|
|
ipcMain.handle(DownloadRoute.downloadFile, async (_event, url, fileName) => {
|
|
const userDataPath = join(app.getPath('home'), 'jan')
|
|
if (
|
|
typeof fileName === 'string' &&
|
|
(fileName.includes('file:/') || fileName.includes('file:\\'))
|
|
) {
|
|
fileName = fileName.replace('file:/', '').replace('file:\\', '')
|
|
}
|
|
const destination = resolve(userDataPath, fileName)
|
|
const rq = request(url)
|
|
|
|
// Put request to download manager instance
|
|
DownloadManager.instance.setRequest(fileName, rq)
|
|
|
|
// Downloading file to a temp file first
|
|
const downloadingTempFile = `${destination}.download`
|
|
|
|
progress(rq, {})
|
|
.on('progress', function (state: any) {
|
|
WindowManager?.instance.currentWindow?.webContents.send(
|
|
DownloadEvent.onFileDownloadUpdate,
|
|
{
|
|
...state,
|
|
fileName,
|
|
}
|
|
)
|
|
})
|
|
.on('error', function (err: Error) {
|
|
WindowManager?.instance.currentWindow?.webContents.send(
|
|
DownloadEvent.onFileDownloadError,
|
|
{
|
|
fileName,
|
|
err,
|
|
}
|
|
)
|
|
})
|
|
.on('end', function () {
|
|
if (DownloadManager.instance.networkRequests[fileName]) {
|
|
// Finished downloading, rename temp file to actual file
|
|
renameSync(downloadingTempFile, destination)
|
|
|
|
WindowManager?.instance.currentWindow?.webContents.send(
|
|
DownloadEvent.onFileDownloadSuccess,
|
|
{
|
|
fileName,
|
|
}
|
|
)
|
|
DownloadManager.instance.setRequest(fileName, undefined)
|
|
} else {
|
|
WindowManager?.instance.currentWindow?.webContents.send(
|
|
DownloadEvent.onFileDownloadError,
|
|
{
|
|
fileName,
|
|
err: { message: 'aborted' },
|
|
}
|
|
)
|
|
}
|
|
})
|
|
.pipe(createWriteStream(downloadingTempFile))
|
|
})
|
|
}
|