import angular from 'angular' ; import { Injectable } from 'angular-ts-decorators' ; import autobind from 'autobind-decorator' ; import { ApiServiceType } from '../api/api.enum' ; import { ApiService } from '../api/api.interface' ; import { Bookmark , BookmarkService } from '../bookmark/bookmark.interface' ; import { FailedRestoreDataError } from '../errors/errors' ; import { MessageCommand } from '../global-shared.enum' ; import { PlatformService } from '../global-shared.interface' ; import { LogService } from '../log/log.service' ; import { StoreKey } from '../store/store.enum' ; import { StoreService } from '../store/store.service' ; import { SyncType } from '../sync/sync.enum' ; import { UpgradeService } from '../upgrade/upgrade.service' ; import { UtilityService } from '../utility/utility.service' ; import { AutoBackUpSchedule , Backup , BackupSync } from './backup-restore.interface' ; @ autobind @ Injectable ( 'BackupRestoreService' ) export class BackupRestoreService { $q : ng . IQService ; apiSvc : ApiService ; bookmarkSvc : BookmarkService ; logSvc : LogService ; platformSvc : PlatformService ; storeSvc : StoreService ; upgradeSvc : UpgradeService ; utilitySvc : UtilityService ; static $inject = [ '$q' , 'ApiService' , 'BookmarkService' , 'LogService' , 'PlatformService' , 'StoreService' , 'UpgradeService' , 'UtilityService' ] ; constructor ( $q , ApiSvc , BookmarkSvc , LogSvc , PlatformSvc , StoreSvc , UpgradeSvc , UtilitySvc ) { this . $q = $q ; this . apiSvc = ApiSvc ; this . bookmarkSvc = BookmarkSvc ; this . logSvc = LogSvc ; this . platformSvc = PlatformSvc ; this . storeSvc = StoreSvc ; this . upgradeSvc = UpgradeSvc ; this . utilitySvc = UtilitySvc ; } createBackupData ( bookmarksData , syncId , serviceUrl , syncVersion ) { const backupData = { xbrowsersync : { date : this . utilitySvc . getDateTimeString ( new Date ( ) ) , sync : { } , data : { } } } ; if ( syncId ) { backupData . xbrowsersync ! . sync = this . createSyncInfoObject ( syncId , serviceUrl , syncVersion ) ; } backupData . xbrowsersync ! . data . bookmarks = bookmarksData ; return backupData ; } createSyncInfoObject ( syncId , serviceUrl , syncVersion ? ) { return { id : syncId , type : ApiServiceType . xBrowserSync , url : serviceUrl , version : syncVersion } as BackupSync ; } getBackupFilename ( ) { const fileName = `xbs_backup_${ this . utilitySvc . getDateTimeString ( new Date ( ) ) }.txt` ; return fileName ; } getSetAutoBackUpSchedule ( newValue ? ) { if ( angular . isUndefined ( newValue ) ) { return this . storeSvc . get < AutoBackUpSchedule > ( StoreKey . AutoBackUpSchedule ) ; } return this . storeSvc . set ( StoreKey . AutoBackUpSchedule , newValue ) . then ( ( ) => { if ( newValue === null ) { this . logSvc . logInfo ( `Auto back up schedule cleared` ) ; } else { this . logSvc . logInfo ( `Auto back up schedule: ${ newValue . autoBackUpHour }:${ newValue . autoBackUpMinute } every ${ newValue . autoBackUpNumber } ${ newValue . autoBackUpUnit }` ) ; } return newValue ; } ) ; } restoreBackupData ( backupData ) { let bookmarksToRestore ; let serviceUrl ; let syncEnabled ; let syncId ; let syncVersion ; switch ( true ) { case ! angular . isUndefined ( backupData . xbrowsersync ) : bookmarksToRestore = backupData . xbrowsersync . data ?. bookmarks ; serviceUrl = backupData . xbrowsersync . sync ?. url ; syncId = backupData . xbrowsersync . sync ?. id ; syncVersion = backupData . xbrowsersync . sync ?. version ; break ; case ! angular . isUndefined ( backupData . xBrowserSync ) : bookmarksToRestore = backupData . xBrowserSync . bookmarks ; syncId = backupData . xBrowserSync . id ; break ; default : throw new FailedRestoreDataError ( ) ; } this . logSvc . logInfo ( 'Restoring data' ) ; return this . platformSvc . getAppVersion ( ) . then ( ( currentVersion ) => { return syncVersion === currentVersion ? bookmarksToRestore : this . upgradeSvc . upgradeBookmarks ( currentVersion , syncVersion , bookmarksToRestore ) ; } ) . then ( ( upgradedBookmarks ) => this . utilitySvc . isSyncEnabled ( ) . then ( ( cachedSyncEnabled ) => { syncEnabled = cachedSyncEnabled ; return syncEnabled ? this . apiSvc . checkServiceStatus ( ) : this . $q ( ( resolve , reject ) => { this . $q . all ( [ this . storeSvc . remove ( StoreKey . Password ) , syncId ? this . storeSvc . set ( StoreKey . SyncId , syncId ) : this . $q . resolve ( ) , serviceUrl ? this . utilitySvc . updateServiceUrl ( serviceUrl ) : this . $q . resolve ( ) ] ) . then ( resolve ) . catch ( reject ) ; } ) ; } ) . then ( ( ) => this . platformSvc . queueSync ( { bookmarks : upgradedBookmarks , type : ! syncEnabled ? SyncType . Local : SyncType . LocalAndRemote } , MessageCommand . RestoreBookmarks ) ) ) . then ( ( ) => { } ) ; } runAutoBackUp ( ) { this . logSvc . logInfo ( 'Running auto back up' ) ; this . saveBackupFile ( false ) ; } saveBackupFile ( displaySaveDialog ? ) { let filename ; return this . $q . all ( [ this . bookmarkSvc . getBookmarksForExport ( ) , this . storeSvc . get < string > ( StoreKey . SyncId ) , this . utilitySvc . getServiceUrl ( ) , this . utilitySvc . getSyncVersion ( ) , this . utilitySvc . isSyncEnabled ( ) ] ) . then ( ( data ) => { const [ bookmarksData , syncId , serviceUrl , syncVersion , syncEnabled ] = data ; const backupData = this . createBackupData ( bookmarksData , syncEnabled ? syncId : undefined , syncEnabled ? serviceUrl : undefined , syncEnabled ? syncVersion : undefined ) ; const beautifiedJson = JSON . stringify ( backupData , null , 2 ) ; filename = this . getBackupFilename ( ) ; return this . platformSvc . downloadFile ( filename , beautifiedJson , displaySaveDialog ) ; } ) ; } }