import { Injectable } from '@angular/core' ; import { Observable , timer } from 'rxjs' ; import { catchError , map , switchMap } from 'rxjs/operators' ; import { HttpHeaders , HttpParams , HttpResponse } from '@angular/common/http' ; import { FileMetadata } from '../model/metadata' ; import { FormControl } from '@angular/forms' ; import { FileElement } from '../model/file-element' ; import { APIGetMultiResponse , APIGetSingleResponse } from '../../../../services/models/api-response' ; import { ApiCallService , ApiService , httpFileOptions , httpObserveOptions , resp } from '../../../../services/api-call.service' ; import { ValidatorService } from '../../../../services/validator.service' ; export const checkFolderExistsValidator = ( fileService , metadata , time = 500 ) => { return ( control ) => { return timer ( time ) . pipe ( switchMap ( ( ) => { return fileService . getFileElement ( control . value , metadata ) . pipe ( map ( ( apiResponse ) => { if ( apiResponse === null ) { return null ; } else { return { typeExists : true } ; } } ) , catchError ( ( ) => { return new Promise ( resolve => { resolve ( null ) ; } ) ; } ) ) ; } ) ) ; } ; } ; export const PARAMETER = 'params' ; export const RESPONSETYPE = 'responseType' ; @ Injectable ( { providedIn : 'root' } ) export class FileService < T = any > implements ApiService { public servicePrefix : string = 'media_file' ; constructor ( private api ) { } param metadata param options public getAllFilesList ( metadata , ... options ) { let params = new HttpParams ( ) ; for ( const option of options ) { for ( const key of Object . keys ( option ) ) { params = params . append ( key , option [ key ] ) ; } } params = params . append ( 'metadata' , JSON . stringify ( metadata ) ) ; const httpOptions = httpObserveOptions ; httpOptions [ PARAMETER ] = params ; return this . api . callGet < Array < T > > ( this . servicePrefix + '/' , httpOptions ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body ; } ) ) ; } param file param metadata public postFile ( file , metadata ) { const formData = new FormData ( ) ; formData . append ( 'file' , file ) ; formData . append ( 'metadata' , JSON . stringify ( metadata ) ) ; const httpOptions = httpFileOptions ; httpOptions [ RESPONSETYPE ] = 'json' ; return this . api . callPost < any > ( `${ this . servicePrefix }/` , formData , httpOptions ) . pipe ( map ( ( apiResponse ) => { if ( apiResponse . status === 204 ) { return [ ] ; } return apiResponse . body . raw ; } ) ) ; } param file param hasReference public putFile ( file , hasReference = false ) { let params = new HttpParams ( ) ; params = params . append ( 'attachment' , JSON . stringify ( { reference : hasReference } ) ) ; const httpOptions = httpObserveOptions ; httpOptions [ PARAMETER ] = params ; return this . api . callPut < number > ( this . servicePrefix + '/' , JSON . stringify ( file ) , httpOptions ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body ; } ) ) ; } param filename param metadata public downloadFile ( filename , metadata ) { let params = new HttpParams ( ) ; params = params . append ( 'metadata' , JSON . stringify ( metadata ) ) ; const observeOptions = { headers : new HttpHeaders ( { 'Content-Type' : 'application/json' } ) , params : { } , observe : resp } ; observeOptions [ PARAMETER ] = params ; observeOptions [ RESPONSETYPE ] = 'blob' ; return this . api . callGet ( this . servicePrefix + '/download/' + filename , observeOptions ) ; } param regex public getFilesListByRegex ( regex ) { regex = ValidatorService . validateRegex ( regex ) . trim ( ) ; return this . api . callGet < T [ ] > ( this . servicePrefix + '/download/' + encodeURIComponent ( regex ) ) . pipe ( map ( ( apiResponse ) => { if ( apiResponse . status === 204 ) { return [ ] ; } return apiResponse . body ; } ) ) ; } param fileID param metadata public deleteFile ( fileID , metadata ) { httpFileOptions [ PARAMETER ] = { metadata : JSON . stringify ( metadata ) } ; return this . api . callDelete < any > ( this . servicePrefix + '/' + fileID , httpFileOptions ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body ; } ) ) ; } param filename param metadata public getFileElement ( filename , metadata ) { httpObserveOptions [ PARAMETER ] = { metadata : JSON . stringify ( metadata ) } ; return this . api . callGet < T > ( `${ this . servicePrefix }/${ filename }` , httpObserveOptions ) . pipe ( map ( ( apiResponse ) => { if ( apiResponse . body . result === null ) { return null ; } return apiResponse . body . result as T ; } ) ) ; } param publicID param path param tree public pathBuilder ( publicID , path , tree ) { const temp = tree . find ( f => f . public_id === publicID ) ; const checker = temp ? temp . filename : '' ; path . push ( checker ) ; if ( temp && temp . metadata && temp . metadata . parent ) { return this . pathBuilder ( temp . metadata . parent , path , tree ) ; } return path . reverse ( ) ; } }