import { Injectable } from '@angular/core' ; import { HttpEvent , HttpRequest , HttpErrorResponse , HttpHandler } from '@angular/common/http' ; import { Observable , throwError } from 'rxjs' ; import { catchError } from 'rxjs/operators' ; @ Injectable ( { providedIn : 'root' } ) export class UploadFileService { constructor ( private handler ) { } pushFileToStorage ( file , url ) { console . log ( "Pushing file to " + url ) ; const data = new FormData ( ) ; data . append ( 'file' , file ) ; const newRequest = new HttpRequest ( 'POST' , url , data ) ; return this . handler . handle ( newRequest ) . pipe ( catchError ( this . handleError ) ) ; } private handleError ( error ) { if ( error . error instanceof ErrorEvent ) { console . error ( 'An error occurred:' , error . error . message ) ; } else { console . error ( `Backend returned code ${ error . status }, ` + `body was: ${ error . error }` ) ; if ( error . status == 403 ) { throw new Error ( "You are not permitted to make changes on this account" ) ; } throw new Error ( "Unexpected error - please try again later" ) ; } return throwError ( "Unexpected error - please try again later" ) ; } ; }