11import path from "path" ;
22import { promisify } from 'util'
3+ import { pipeline } from 'stream/promises' ;
34import { exec , spawn } from 'child_process'
45import { app , nativeImage } from "electron" ;
56import axios from "axios" ;
6- import o_fs from "original-fs" ;
7+ import fso from "original-fs" ;
78import unzipper from "unzipper" ;
89import fs from "fs" ;
910import { Logger } from "./Logger.js" ;
@@ -24,7 +25,7 @@ export const getNativeImg = (relativePath) => {
2425 : path . join ( __dirname , '..' , '..' , 'assets' )
2526
2627 const filePath = path . join ( basePath , relativePath ) ;
27- if ( ! filePath || ! o_fs . existsSync ( filePath ) ) {
28+ if ( ! filePath || ! fso . existsSync ( filePath ) ) {
2829 logger . log ( `File path is undefined for relative path: ${ filePath } ` ) ;
2930 }
3031 return nativeImage . createFromPath ( filePath )
@@ -130,36 +131,19 @@ export async function downloadFile(url, dest, callback) {
130131 onDownloadProgress : progress => {
131132 callback ( progress . progress , `Downloading ${ path . basename ( dest ) } ...` ) ;
132133 }
133- } )
134- return new Promise ( ( resolve , reject ) => {
135- const writer = o_fs . createWriteStream ( dest ) ;
136-
137- response . data . pipe ( writer ) ;
138-
139- writer . on ( 'finish' , ( ) => {
140- writer . close ( ( err ) => {
141- if ( err ) {
142- callback ( - 1 , 'Error closing file: ' + err ) ;
143- reject ( err ) ;
144- return ;
145- }
146- callback ( 1 , 'Download completed: ' + path . basename ( dest ) ) ;
147- } ) ;
148- resolve ( dest ) ;
149- } ) ;
150-
151- writer . on ( 'error' , ( error ) => {
152- writer . close ?. ( ) ;
153- callback ( - 1 , 'Download error: ' + error ) ;
154- reject ( error ) ;
155- } ) ;
156-
157- response . data . on ( 'error' , ( error ) => {
158- writer . close ?. ( ) ;
159- callback ( - 1 , 'Download error: ' + error ) ;
160- reject ( error ) ;
161- } ) ;
162134 } ) ;
135+
136+ const writer = fso . createWriteStream ( dest ) ;
137+
138+ try {
139+ await pipeline ( response . data , writer ) ;
140+ callback ( 1 , 'Download completed: ' + path . basename ( dest ) ) ;
141+ return dest ;
142+ } catch ( err ) {
143+ writer . close ( ) ;
144+ callback ( - 1 , 'Download error: ' + err ) ;
145+ throw err ;
146+ }
163147}
164148
165149export async function checkIfLegacyYMInstalled ( ) {
0 commit comments