1 import { SortMeta } from 'primeng/api'
2 import { concat, Observable } from 'rxjs'
3 import { catchError, map, toArray } from 'rxjs/operators'
4 import { HttpClient, HttpParams } from '@angular/common/http'
5 import { Injectable } from '@angular/core'
6 import { RestExtractor, RestPagination, RestService } from '@app/core'
7 import { ResultList, Video, VideoRedundanciesTarget, VideoRedundancy } from '@shared/models'
8 import { environment } from '../../../../environments/environment'
11 export class RedundancyService {
12 static BASE_REDUNDANCY_URL = environment.apiUrl + '/api/v1/server/redundancy'
15 private authHttp: HttpClient,
16 private restService: RestService,
17 private restExtractor: RestExtractor
20 updateRedundancy (host: string, redundancyAllowed: boolean) {
21 const url = RedundancyService.BASE_REDUNDANCY_URL + '/' + host
23 const body = { redundancyAllowed }
25 return this.authHttp.put(url, body)
27 map(this.restExtractor.extractDataBool),
28 catchError(err => this.restExtractor.handleError(err))
32 listVideoRedundancies (options: {
33 pagination: RestPagination,
35 target?: VideoRedundanciesTarget
36 }): Observable<ResultList<VideoRedundancy>> {
37 const { pagination, sort, target } = options
39 let params = new HttpParams()
40 params = this.restService.addRestGetParams(params, pagination, sort)
42 if (target) params = params.append('target', target)
44 return this.authHttp.get<ResultList<VideoRedundancy>>(RedundancyService.BASE_REDUNDANCY_URL + '/videos', { params })
46 catchError(res => this.restExtractor.handleError(res))
50 addVideoRedundancy (video: Video) {
51 return this.authHttp.post(RedundancyService.BASE_REDUNDANCY_URL + '/videos', { videoId: video.id })
53 catchError(res => this.restExtractor.handleError(res))
57 removeVideoRedundancies (redundancy: VideoRedundancy) {
58 const observables = redundancy.redundancies.streamingPlaylists.map(r => r.id)
59 .concat(redundancy.redundancies.files.map(r => r.id))
60 .map(id => this.removeRedundancy(id))
62 return concat(...observables)
66 private removeRedundancy (redundancyId: number) {
67 return this.authHttp.delete(RedundancyService.BASE_REDUNDANCY_URL + '/videos/' + redundancyId)
69 map(this.restExtractor.extractDataBool),
70 catchError(res => this.restExtractor.handleError(res))