]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/video/redundancy.service.ts
Remove unnecessary function
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / video / redundancy.service.ts
1 import { SortMeta } from 'primeng/api'
2 import { concat, Observable } from 'rxjs'
3 import { catchError, 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'
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)
26 .pipe(catchError(err => this.restExtractor.handleError(err)))
27 }
28
29 listVideoRedundancies (options: {
30 pagination: RestPagination
31 sort: SortMeta
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)
65 .pipe(catchError(res => this.restExtractor.handleError(res)))
66 }
67 }