]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { SortMeta } from 'primeng/api' |
2 | import { concat, Observable } from 'rxjs' | |
e8bffe96 | 3 | import { catchError, toArray } from 'rxjs/operators' |
b764380a C |
4 | import { HttpClient, HttpParams } from '@angular/common/http' |
5 | import { Injectable } from '@angular/core' | |
67ed6552 | 6 | import { RestExtractor, RestPagination, RestService } from '@app/core' |
b764380a | 7 | import { ResultList, Video, VideoRedundanciesTarget, VideoRedundancy } from '@shared/models' |
67ed6552 | 8 | import { environment } from '../../../../environments/environment' |
b764380a C |
9 | |
10 | @Injectable() | |
11 | export class RedundancyService { | |
12 | static BASE_REDUNDANCY_URL = environment.apiUrl + '/api/v1/server/redundancy' | |
13 | ||
14 | constructor ( | |
15 | private authHttp: HttpClient, | |
16 | private restService: RestService, | |
17 | private restExtractor: RestExtractor | |
18 | ) { } | |
19 | ||
20 | updateRedundancy (host: string, redundancyAllowed: boolean) { | |
21 | const url = RedundancyService.BASE_REDUNDANCY_URL + '/' + host | |
22 | ||
23 | const body = { redundancyAllowed } | |
24 | ||
25 | return this.authHttp.put(url, body) | |
e8bffe96 | 26 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
b764380a C |
27 | } |
28 | ||
29 | listVideoRedundancies (options: { | |
9df52d66 C |
30 | pagination: RestPagination |
31 | sort: SortMeta | |
b764380a C |
32 | target?: VideoRedundanciesTarget |
33 | }): Observable<ResultList<VideoRedundancy>> { | |
34 | const { pagination, sort, target } = options | |
35 | ||
36 | let params = new HttpParams() | |
37 | params = this.restService.addRestGetParams(params, pagination, sort) | |
38 | ||
39 | if (target) params = params.append('target', target) | |
40 | ||
41 | return this.authHttp.get<ResultList<VideoRedundancy>>(RedundancyService.BASE_REDUNDANCY_URL + '/videos', { params }) | |
42 | .pipe( | |
43 | catchError(res => this.restExtractor.handleError(res)) | |
44 | ) | |
45 | } | |
46 | ||
47 | addVideoRedundancy (video: Video) { | |
48 | return this.authHttp.post(RedundancyService.BASE_REDUNDANCY_URL + '/videos', { videoId: video.id }) | |
49 | .pipe( | |
50 | catchError(res => this.restExtractor.handleError(res)) | |
51 | ) | |
52 | } | |
53 | ||
54 | removeVideoRedundancies (redundancy: VideoRedundancy) { | |
55 | const observables = redundancy.redundancies.streamingPlaylists.map(r => r.id) | |
56 | .concat(redundancy.redundancies.files.map(r => r.id)) | |
57 | .map(id => this.removeRedundancy(id)) | |
58 | ||
59 | return concat(...observables) | |
60 | .pipe(toArray()) | |
61 | } | |
62 | ||
63 | private removeRedundancy (redundancyId: number) { | |
64 | return this.authHttp.delete(RedundancyService.BASE_REDUNDANCY_URL + '/videos/' + redundancyId) | |
e8bffe96 | 65 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
b764380a C |
66 | } |
67 | } |