2021-01-15 20:21:33 +01:00

90 lines
3.2 KiB
TypeScript

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<any> {
return this.http.get<any>(this.getImageUrl + '/all').pipe(
tap(next => this.logger.debug('getAllImages', next))
);
}
public getImage(id: string): Observable<ImageResponse> {
return this.http.get<ImageResponse>(this.getImageUrl + '/' + id).pipe(
tap(next => this.logger.debug('getImage', id, next))
);
}
public getImageVersion(id: string, version: string): Observable<any> {
return this.http.get<any>(this.getImageUrl + '/' + id + '/version/' + version).pipe(
tap(next => this.logger.debug('getImageVersion', id, version, next))
);
}
public deleteAllImages(): Observable<any> {
return this.http.delete<any>(this.deleteImageUrl + 'all').pipe(
tap(next => this.logger.debug('deleteAllImages', next))
);
}
public deleteImage(filenameWithExtension: string): Observable<any> {
const stripped = RestService.stripUploadFileName(filenameWithExtension);
return this.http.delete<any>(this.deleteImageUrl + '/' + stripped).pipe(
tap(next => this.logger.debug('deleteImage', stripped, next))
);
}
// public postImage(id: string, image: any): Observable<any> {
// return this.http.post<any>(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<any> {
// return this.http.put<any>(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<StorageHealth> {
return this.http.get<StorageHealth>(this.healthCheckUrl).pipe(
tap(next => this.logger.debug('healthCheck', next))
);
}
}