import { BrowserWindow , dialog , shell , WebContents } from 'electron' ; import * as fs from 'fs' ; import { i18n } from '../common/i18n' ; import { logger } from '../common/logger' ; import { windowExists } from './window-utils' ; const DOWNLOAD_MANAGER_NAMESPACE = 'DownloadManager' ; export interface IDownloadItem { _id : string ; fileName : string ; savedPath : string ; total : string ; } class DownloadHandler { /**
     * Show dialog for failed cases
     */ private static async showDialog ( ) { const focusedWindow = BrowserWindow . getFocusedWindow ( ) ; const message = i18n . t ( 'The file you are trying to open cannot be found in the specified path.' , DOWNLOAD_MANAGER_NAMESPACE ) ( ) ; const title = i18n . t ( 'File not Found' , DOWNLOAD_MANAGER_NAMESPACE ) ( ) ; if ( ! focusedWindow || ! windowExists ( focusedWindow ) ) { return ; } await dialog . showMessageBox ( focusedWindow , { message , title , type : 'error' , } ) ; } private window ! : WebContents | null ; private items : IDownloadItem [ ] = [ ] ; param window public setWindow ( window ) { this . window = window ; logger . info ( `download-handler: Initialized download handler` ) ; } param id { string } public async openFile ( id ) { const filePath = this . getFilePath ( id ) ; const openResponse = fs . existsSync ( `${ filePath }` ) ; if ( openResponse ) { const result = await shell . openPath ( `${ filePath }` ) ; if ( result === '' ) { return ; } } await DownloadHandler . showDialog ( ) ; } param id { string } public showInFinder ( id ) { const filePath = this . getFilePath ( id ) ; if ( fs . existsSync ( filePath ) ) { shell . showItemInFolder ( filePath ) ; return ; } DownloadHandler . showDialog ( ) ; } /**
     * Clears download items
     */ public clearDownloadedItems ( ) { this . items = [ ] ; } param item public onDownloadSuccess ( item ) { this . items . push ( item ) ; this . sendDownloadCompleted ( item ) ; } /**
     * Handle a failed download
     */ public onDownloadFailed ( ) { this . sendDownloadFailed ( ) ; } /**
     * Send download completed event to the renderer process
     */ private sendDownloadCompleted ( item ) { if ( this . window && ! this . window . isDestroyed ( ) ) { logger . info ( `download-handler: Download completed! Informing the client!` ) ; this . window . send ( 'download-completed' , { id : item . _id , fileDisplayName : item . fileName , fileSize : item . total , } ) ; } } /**
     * Send download failed event to the renderer process
     */ private sendDownloadFailed ( ) { if ( this . window && ! this . window . isDestroyed ( ) ) { logger . info ( `download-handler: Download failed! Informing the client!` ) ; this . window . send ( 'download-failed' ) ; } } param id private getFilePath ( id ) { const fileIndex = this . items . findIndex ( ( item ) => { return item . _id === id ; } ) ; return this . items [ fileIndex ] . savedPath ; } } const downloadHandler = new DownloadHandler ( ) ; export { downloadHandler } ;