import {HttpClient} from '@angular/common/http'; import {Injectable} from '@angular/core'; import {NGXLogger} from 'ngx-logger'; import {environment} from '../../environments/environment'; import {Observable} from 'rxjs'; import {tap} from 'rxjs/operators'; import {ImageResponse, StorageHealth} from '../interfaces/interface'; @Injectable() export class RestService { constructor( private logger: NGXLogger, private http: HttpClient ) { } private currentLocation = 'http://' + environment.location + ':' + environment.port; private imageUrl = this.currentLocation + '/image'; private getImageUrl = this.imageUrl + '/get'; private deleteImageUrl = this.imageUrl + '/delete'; private postImageUrl = this.imageUrl + '/post'; private updateImageUrl = this.imageUrl + '/update'; private healthCheckUrl = this.currentLocation + '/check'; private static stripUploadFileName(name: string) { // const generalUploadFileName: string = // (document.getElementById('generalUpload').firstChild as HTMLElement).children[5].children[0].textContent; // this.logger.debug('Get filename', generalUploadFileName); const temp: string[] = name.split('.'); return temp.slice(0, temp.length - 1).join('.'); } public getAllImages(): Observable { return this.http.get(this.getImageUrl + '/all').pipe( tap(next => this.logger.debug('getAllImages', next)) ); } public getImage(id: string): Observable { return this.http.get(this.getImageUrl + '/' + id).pipe( tap(next => this.logger.debug('getImage', id, next)) ); } public getImageVersion(id: string, version: string): Observable { return this.http.get(this.getImageUrl + '/' + id + '/version/' + version).pipe( tap(next => this.logger.debug('getImageVersion', id, version, next)) ); } public deleteAllImages(): Observable { return this.http.delete(this.deleteImageUrl + 'all').pipe( tap(next => this.logger.debug('deleteAllImages', next)) ); } public deleteImage(filenameWithExtension: string): Observable { const stripped = RestService.stripUploadFileName(filenameWithExtension); return this.http.delete(this.deleteImageUrl + '/' + stripped).pipe( tap(next => this.logger.debug('deleteImage', stripped, next)) ); } // public postImage(id: string, image: any): Observable { // return this.http.post(this.postImageUrl + '/' + id, image).pipe( // tap(next => this.logger.debug('postImage', id, image, next)) // ); // } public getPostImageUrl(): string { return this.postImageUrl; } // public updateImage(id: string, image: any): Observable { // return this.http.put(this.updateImageUrl + '/' + id, image).pipe( // tap(next => this.logger.debug('updateImage', id, image, next)) // ); // } public getUpdateImageUrl(filenameWithExtension: string): string { return this.updateImageUrl + '/' + RestService.stripUploadFileName(filenameWithExtension); } public healthCheck(): Observable { return this.http.get(this.healthCheckUrl).pipe( tap(next => this.logger.debug('healthCheck', next)) ); } }