import { LoggingService } from './logging-service' ; import { Settings } from './settings' ; import { Response } from '@angular/http' ; import { IDropboxAuth } from './idropbox-auth' ; import { Observable } from 'rxjs' ; import { map , catchError } from 'rxjs/operators' ; import { HttpClient , HttpHeaders } from '@angular/common/http' ; import { Utils } from './utils' ; export class DropboxBase { constructor ( private http , protected settings , private loggingService ) { } initialize ( auth ) { let headers = this . createHeader ( auth ) ; headers = headers . append ( 'Content-Type' , 'application/json' ) ; return new Promise ( ( resolve , reject ) => { this . add401catch ( this . http . post ( 'https://api.dropboxapi.com/2/files/get_metadata' , { path : '/PersonalWiki' } , { headers } ) ) . subscribe ( ( ) => resolve ( ) , ( err ) => { this . loggingService . log ( 'error posting to dropbox api ' + Utils . serializeError ( err ) ) ; this . add401catch ( this . http . post ( 'https://api.dropboxapi.com/2/files/create_folder' , null , { headers } ) ) . subscribe ( ( ) => resolve ( ) , err2 => reject ( 'Error creating PersonalWiki folder ' + Utils . serializeError ( err2 ) ) ) ; } ) ; } ) ; } private createHeader ( auth ) { let headers = new HttpHeaders ( ) ; headers = headers . append ( 'Authorization' , 'Bearer ' + auth . accessToken ) ; return headers ; } protected doRequest ( uri , auth , data ) { let headers = this . createHeader ( auth ) ; headers = headers . append ( 'Content-Type' , 'application/json' ) ; return this . add401catch ( this . http . post ( uri , data , { headers } ) ) ; } private add401catch ( obs ) { return obs . pipe ( catchError ( ( err ) => { if ( err . status === 401 ) { this . settings . removeDropboxAuthInfo ( ) ; } throw err ; } ) ) ; } protected downloadFile ( path , isText , auth ) { let headers = this . createHeader ( auth ) ; headers = headers . append ( 'Dropbox-API-Arg' , this . stringifyApiArg ( { path } ) ) ; const url = 'https://content.dropboxapi.com/2/files/download' ; if ( isText ) { return this . add401catch ( this . http . post ( url , null , { headers , responseType : 'text' } ) ) ; } else { return this . add401catch ( this . http . post ( url , null , { headers , responseType : 'arraybuffer' } ) ) ; } } private stringifyApiArg ( apiArg ) { let json = JSON . stringify ( apiArg ) ; json = json . replace ( /[\u007F-\uFFFF]/g , ( chr ) => '\\u' + ( '0000' + chr . charCodeAt ( 0 ) . toString ( 16 ) ) . substr ( - 4 ) ) ; return json ; } protected uploadFile ( path , contents , auth ) { let headers = this . createHeader ( auth ) ; headers = headers . append ( 'Dropbox-API-Arg' , this . stringifyApiArg ( { path , mode : 'overwrite' } ) ) ; headers = headers . append ( 'Content-Type' , 'application/octet-stream' ) ; const body = new Blob ( [ contents ] ) ; return this . add401catch ( this . http . post ( 'https://content.dropboxapi.com/2/files/upload' , body , { headers } ) ) ; } }