import ServiceElectron , { IPCMessages } from '../service.electron' ; import ServiceStreams , { IStreamInfo } from '../service.streams' ; import ServiceStorage , { IStorageScheme } from '../service.storage' ; import ServiceStreamSource from '../service.stream.sources' ; import ServiceHotkeys from '../service.hotkeys' ; import { getDefaultFileParser , AFileParser , getParserForFile } from '../../controllers/files.parsers/index' ; import { IMapItem , ITicks } from '../../controllers/files.parsers/interface' ; import { dialog , OpenDialogReturnValue , FileFilter } from 'electron' ; import Logger from '../../tools/env.logger' ; import * as Tools from '../../tools/index' ; import * as fs from 'fs' ; import * as path from 'path' ; import { Subscription } from '../../tools/index' ; import { IService } from '../../interfaces/interface.service' ; class description class ServiceFilePicker implements IService { private _logger : Logger = new Logger ( 'ServiceFilePicker' ) ; private _subscription : { [ key ] : Subscription ; } = { } ; returns public init ( ) { return new Promise ( ( resolve , reject ) => { ServiceElectron . IPC . subscribe ( IPCMessages . FilePickerRequest , this . _ipc_FilePickerRequest . bind ( this ) ) . then ( ( subscription ) => { this . _subscription . FilePickerRequest = subscription ; resolve ( ) ; } ) . catch ( ( error ) => { this . _logger . error ( `Fail to init module due error: ${ error . message }` ) ; reject ( error ) ; } ) ; } ) ; } public destroy ( ) { return new Promise ( ( resolve ) => { Object . keys ( this . _subscription ) . forEach ( ( key ) => { this . _subscription [ key ] . destroy ( ) ; } ) ; resolve ( ) ; } ) ; } public getName ( ) { return 'ServiceFilePicker' ; } private _ipc_FilePickerRequest ( request , response ) { const req = request as IPCMessages . FilePickerRequest ; this . _open ( req . filter instanceof Array ? req . filter : [ ] , typeof req . multiple === 'boolean' ? req . multiple : false , typeof req . defaultPath === 'string' ? path . dirname ( req . defaultPath ) : '' ) . then ( ( files ) => { if ( files . length === 0 ) { return response ( new IPCMessages . FilePickerResponse ( { files : [ ] , } ) ) ; } Promise . all ( files . map ( ( file ) => { return this . _getInfo ( file ) ; } ) ) . then ( ( data ) => { response ( new IPCMessages . FilePickerResponse ( { files : data , } ) ) ; } ) . catch ( ( gettingInfoError ) => { response ( new IPCMessages . FilePickerResponse ( { files : [ ] , error : gettingInfoError . message , } ) ) ; } ) ; } ) . catch ( ( error ) => { response ( new IPCMessages . FilePickerResponse ( { files : [ ] , error : error . message , } ) ) ; } ) ; } private _open ( filters = [ ] , multiple = false , defaultPath ) { return new Promise ( ( resolve , reject ) => { const win = ServiceElectron . getBrowserWindow ( ) ; if ( win === undefined ) { return ; } const options = [ 'openFile' , 'showHiddenFiles' ] ; if ( multiple ) { options . push ( 'multiSelections' ) ; } dialog . showOpenDialog ( win , { properties : options as any [ ] , filters : filters , defaultPath : defaultPath , } ) . then ( ( returnValue ) => { resolve ( returnValue . filePaths ) ; } ) . catch ( ( error ) => { this . _logger . error ( `Fail open file due error: ${ error . message }` ) ; } ) ; } ) ; } private _getInfo ( file ) { return new Promise ( ( resolve , reject ) => { fs . stat ( file , ( error , stats ) => { if ( error ) { return reject ( error ) ; } resolve ( { name : path . basename ( file ) , path : file , size : stats . size , created : stats . ctimeMs , changed : stats . mtimeMs , } ) ; } ) ; } ) ; } } export default ( new ServiceFilePicker ( ) ) ;