import { Injectable } from '@angular/core' ; import { Response } from '@angular/http' ; import { Observable } from 'rxjs/Observable' ; import "rxjs/Rx" ; import 'rxjs/add/operator/map' ; import { Transfer , TransferObject } from '@ionic-native/transfer' ; import { File } from '@ionic-native/file' ; import _ from "lodash" ; import { SafeHttpProvider } from "./safe-http" ; import { NovelsLocalService } from "./novels-local-service" ; import { EpubDownloadItem } from "../common/models/download-item" ; import { SettingsService } from "./settings-service" ; import { Novel } from "../common/models/novel" ; @ Injectable ( ) export class EpubService { fileTransfer : TransferObject ; queue : EpubDownloadItem [ ] = [ ] ; constructor ( private http , public file , private transfer , private novelsLocalService , private settingsService ) { console . log ( 'Hello Epubs Service' ) ; } init ( ) { this . fileTransfer = this . transfer . create ( ) ; } addToQueue ( novel ) { let item = this . getItemFromQueue ( novel ) ; if ( ! item ) { item = new EpubDownloadItem ( { novel : novel } ) ; console . log ( "adding item" , item ) ; this . queue . push ( item ) ; this . exportToEpub ( item ) ; } } removeFromQueue ( item ) { _ . pull ( this . queue , item ) ; } getItemFromQueue ( novel ) { return _ . find ( this . queue , item => item . novel . id === novel . id ) ; } generateEpub ( novelId ) { console . log ( "EpubService::generateEpub" ) ; return this . http . get ( `/api/epub/${ novelId }/generate` ) . map ( ( response ) => { let data = response . json ( ) || { } ; return data . percent ; } ) . catch ( error => { return Observable . throw ( error ) ; } ) ; } getGenerationProgress ( novelId ) { console . log ( "EpubService::getGenerationProgress" ) ; return this . http . get ( `/api/epub/${ novelId }/progress` ) . map ( ( response ) => { let data = response . json ( ) || { } ; return data . percent ; } ) . catch ( error => { return Observable . throw ( error ) ; } ) ; } downloadEpub ( downloadItem ) { this . novelsLocalService . getNovel ( downloadItem . novel . id . toString ( ) ) . then ( ( novel ) => { this . fileTransfer . download ( `/api/epub/${ downloadItem . novel . id }/download` , `${ this . settingsService . settings . epubLocation }${ novel . title }.epub` ) . then ( entry => { console . log ( "epub download complete: " , entry . toURL ( ) ) ; downloadItem . progress = 100 ; } ) . catch ( err => { console . log ( "error downloading epub" , err ) ; } ) ; } ) ; } exportToEpub ( downloadItem ) { this . generateEpub ( downloadItem . novel . id ) . subscribe ( ( ) => { let poller = ( ) => { this . epubGenerationProgress ( downloadItem ) . then ( ( ) => { console . log ( "trying to download epub" ) ; this . downloadEpub ( downloadItem ) ; } ) . catch ( ( ) => { setTimeout ( poller , 1000 ) ; } ) ; } ; poller ( ) ; } ) ; } private epubGenerationProgress ( downloadItem ) { return new Promise ( ( resolve , reject ) => { this . getGenerationProgress ( downloadItem . novel . id ) . subscribe ( progress => { downloadItem . progress = Math . min ( progress , 80 ) ; if ( progress != 100 ) { console . log ( "continue polling" ) ; reject ( ) ; } else { console . log ( "end polling" ) ; resolve ( ) ; } } ) ; } ) ; } getDownloadedEpubs ( ) { return new Promise ( ( resolve , reject ) => { let epubLocationDirHolder = this . settingsService . settings . epubLocation ; epubLocationDirHolder = epubLocationDirHolder . substr ( 0 , epubLocationDirHolder . length - 1 ) ; let tempDirSplit = epubLocationDirHolder . split ( "/" ) ; let dirName = tempDirSplit [ tempDirSplit . length - 1 ] ; epubLocationDirHolder = epubLocationDirHolder . substr ( 0 , epubLocationDirHolder . lastIndexOf ( "/" ) + 1 ) ; this . file . listDir ( epubLocationDirHolder , dirName ) . then ( entries => { console . log ( "entries: " , entries ) ; let probableFiles = _ . filter ( entries , entry => { return entry . isFile && /\.epub$/ . test ( entry . name ) ; } ) ; let titles = _ . map ( probableFiles , entry => entry . name . replace ( /\.epub$/ , "" ) ) ; this . novelsLocalService . get ( ) . then ( novels => { let filtered = _ . filter ( novels , ( novel ) => { return _ . includes ( titles , novel . title ) ; } ) ; filtered = _ . filter ( filtered , novel => ! ! novel ) ; let ids = _ . map ( filtered , "id" ) ; this . novelsLocalService . getNovels ( ids ) . then ( novels => { resolve ( novels ) ; } ) ; } ) ; } ) . catch ( ( ) => { resolve ( [ ] ) ; } ) ; } ) ; } async deleteEpubs ( epubs ) { return new Promise ( ( resolve , reject ) => { let epubLocationDirHolder = this . settingsService . settings . epubLocation ; epubLocationDirHolder = epubLocationDirHolder . substr ( 0 , epubLocationDirHolder . length - 1 ) ; _ . each ( epubs , async ( epub ) => { await this . file . removeFile ( epubLocationDirHolder , `${ epub . title }.epub` ) ; } ) ; resolve ( ) ; } ) ; } }