2020-06-23 14:10:17 +02:00
|
|
|
import { SortMeta } from 'primeng/api'
|
|
|
|
import { concat, Observable } from 'rxjs'
|
2022-01-18 11:37:29 +01:00
|
|
|
import { catchError, toArray } from 'rxjs/operators'
|
2020-01-10 10:11:28 +01:00
|
|
|
import { HttpClient, HttpParams } from '@angular/common/http'
|
|
|
|
import { Injectable } from '@angular/core'
|
2020-06-23 14:10:17 +02:00
|
|
|
import { RestExtractor, RestPagination, RestService } from '@app/core'
|
2020-01-10 10:11:28 +01:00
|
|
|
import { ResultList, Video, VideoRedundanciesTarget, VideoRedundancy } from '@shared/models'
|
2020-06-23 14:10:17 +02:00
|
|
|
import { environment } from '../../../../environments/environment'
|
2020-01-10 10:11:28 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class RedundancyService {
|
|
|
|
static BASE_REDUNDANCY_URL = environment.apiUrl + '/api/v1/server/redundancy'
|
|
|
|
|
|
|
|
constructor (
|
|
|
|
private authHttp: HttpClient,
|
|
|
|
private restService: RestService,
|
|
|
|
private restExtractor: RestExtractor
|
|
|
|
) { }
|
|
|
|
|
|
|
|
updateRedundancy (host: string, redundancyAllowed: boolean) {
|
|
|
|
const url = RedundancyService.BASE_REDUNDANCY_URL + '/' + host
|
|
|
|
|
|
|
|
const body = { redundancyAllowed }
|
|
|
|
|
|
|
|
return this.authHttp.put(url, body)
|
2022-01-18 11:37:29 +01:00
|
|
|
.pipe(catchError(err => this.restExtractor.handleError(err)))
|
2020-01-10 10:11:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
listVideoRedundancies (options: {
|
2021-08-17 14:42:53 +02:00
|
|
|
pagination: RestPagination
|
|
|
|
sort: SortMeta
|
2020-01-10 10:11:28 +01:00
|
|
|
target?: VideoRedundanciesTarget
|
|
|
|
}): Observable<ResultList<VideoRedundancy>> {
|
|
|
|
const { pagination, sort, target } = options
|
|
|
|
|
|
|
|
let params = new HttpParams()
|
|
|
|
params = this.restService.addRestGetParams(params, pagination, sort)
|
|
|
|
|
|
|
|
if (target) params = params.append('target', target)
|
|
|
|
|
|
|
|
return this.authHttp.get<ResultList<VideoRedundancy>>(RedundancyService.BASE_REDUNDANCY_URL + '/videos', { params })
|
|
|
|
.pipe(
|
|
|
|
catchError(res => this.restExtractor.handleError(res))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
addVideoRedundancy (video: Video) {
|
|
|
|
return this.authHttp.post(RedundancyService.BASE_REDUNDANCY_URL + '/videos', { videoId: video.id })
|
|
|
|
.pipe(
|
|
|
|
catchError(res => this.restExtractor.handleError(res))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
removeVideoRedundancies (redundancy: VideoRedundancy) {
|
|
|
|
const observables = redundancy.redundancies.streamingPlaylists.map(r => r.id)
|
|
|
|
.concat(redundancy.redundancies.files.map(r => r.id))
|
|
|
|
.map(id => this.removeRedundancy(id))
|
|
|
|
|
|
|
|
return concat(...observables)
|
|
|
|
.pipe(toArray())
|
|
|
|
}
|
|
|
|
|
|
|
|
private removeRedundancy (redundancyId: number) {
|
|
|
|
return this.authHttp.delete(RedundancyService.BASE_REDUNDANCY_URL + '/videos/' + redundancyId)
|
2022-01-18 11:37:29 +01:00
|
|
|
.pipe(catchError(res => this.restExtractor.handleError(res)))
|
2020-01-10 10:11:28 +01:00
|
|
|
}
|
|
|
|
}
|