import * as fs from 'fs' ; import * as Tools from "../../tools/index" ; import Logger from '../../tools/env.logger' ; import ServiceElectron , { IPCMessages } from '../../services/service.electron' ; import ServiceOutputExport from "../../services/output/service.output.export" ; import ServiceStreams from "../../services/service.streams" ; import indexer from "indexer-neon" ; import { CancelablePromise , Progress , Exporter } from "indexer-neon" ; import ServiceFileRecent from '../../services/files/service.file.recent' ; import { CommonInterfaces } from '../../interfaces/interface.common' ; import { dialog , SaveDialogReturnValue } from 'electron' ; import { CExportSelectionActionId , CExportAllActionId } from '../../consts/output.actions' ; export class DefaultOutputExport { private _logger : Logger ; private _guid : string ; private _saver : CancelablePromise < void , void , Exporter . TFileAsyncEvents , Exporter . TFileAsyncEventObject > | undefined ; constructor ( guid ) { this . _guid = guid ; this . _logger = new Logger ( `DefaultOutputExport: ${ this . _guid }` ) ; ServiceOutputExport . setAction ( this . _guid , { id : CExportAllActionId , caption : 'Export all' , handler : this . _exportAll . bind ( this , this . _guid ) , isEnabled : ( ) => true , source : IPCMessages . EOutputExportFeaturesSource . output , } ) ; ServiceOutputExport . setAction ( this . _guid , { id : CExportSelectionActionId , caption : 'Export selection' , handler : this . _exportSelection . bind ( this , this . _guid ) , isEnabled : ( ) => true , source : IPCMessages . EOutputExportFeaturesSource . all , } ) ; } public destroy ( ) { if ( this . _saver === undefined ) { return ; } this . _saver . abort ( ) ; } private _exportAll ( session ) { return new Promise ( ( resolve , reject ) => { const file = ServiceStreams . getStreamFile ( session ) ; if ( file instanceof Error ) { return reject ( new Error ( this . _logger . warn ( `Fail get stream file for session "${ session }" due error: ${ file . message }` ) ) ) ; } this . _saveAs ( file . file , [ ] ) . then ( ( ) => { resolve ( ) ; } ) . catch ( ( saveErr ) => { this . _logger . warn ( `Fail to save stream data due error: ${ saveErr . message }` ) ; reject ( saveErr ) ; } ) ; } ) ; } private _exportSelection ( session , request ) { return new Promise ( ( resolve , reject ) => { const sections = request . selection . map ( ( range ) => { return { first_line : range . from , last_line : range . to } ; } ) ; const file = ServiceStreams . getStreamFile ( session ) ; if ( file instanceof Error ) { return reject ( new Error ( this . _logger . warn ( `Fail get stream file for session "${ session }" due error: ${ file . message }` ) ) ) ; } this . _saveAs ( file . file , sections ) . then ( ( ) => { resolve ( ) ; } ) . catch ( ( saveErr ) => { this . _logger . warn ( `Fail to save stream data due error: ${ saveErr . message }` ) ; reject ( saveErr ) ; } ) ; } ) ; } private _saveAs ( sessionFile , sections = [ ] ) { return new Promise ( ( resolve , reject ) => { this . _getFileName ( ) . then ( ( filename ) => { if ( filename === undefined ) { return resolve ( undefined ) ; } this . _logger . info ( `Saving` ) ; this . _saver = indexer . exportLineBased ( sessionFile , filename , true , { sections : sections } ) . then ( ( ) => { fs . stat ( filename , ( error , stats ) => { if ( error ) { return reject ( error ) ; } ServiceFileRecent . save ( filename , stats . size ) ; } ) ; this . _logger . info ( `Saved` ) ; resolve ( filename ) ; } ) . canceled ( ( ) => { this . _logger . info ( `Saving was canceled` ) ; } ) . catch ( ( error ) => { this . _logger . warn ( `Exception during saving: ${ error . message }` ) ; reject ( error ) ; } ) . finally ( ( ) => { this . _saver = undefined ; } ) . on ( 'progress' , ( event ) => { } ) ; } ) . catch ( ( error ) => { reject ( error ) ; } ) ; } ) ; } private _getFileName ( ) { return new Promise ( ( resolve , reject ) => { const win = ServiceElectron . getBrowserWindow ( ) ; if ( win === undefined ) { return ; } dialog . showSaveDialog ( win , { title : 'Saving plain-text file' , filters : [ { name : 'Text Files' , extensions : [ 'txt' ] , } ] , } ) . then ( ( returnValue ) => { resolve ( returnValue . filePath ) ; } ) . catch ( ( error ) => { this . _logger . error ( `Fail get filename for saving due error: ${ error . message }` ) ; reject ( error ) ; } ) ; } ) ; } }