]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video-caption/video-caption.service.ts
9c29bc05208b8e9bd99c068240b475a91f1bc676
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video-caption / video-caption.service.ts
1 import { catchError, map } from 'rxjs/operators'
2 import { HttpClient } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { forkJoin, Observable, of } from 'rxjs'
5 import { ResultList } from '../../../../../shared'
6 import { RestExtractor, RestService } from '../rest'
7 import { VideoService } from '@app/shared/video/video.service'
8 import { objectToFormData, sortBy } from '@app/shared/misc/utils'
9 import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model'
10 import { VideoCaption } from '../../../../../shared/models/videos/caption/video-caption.model'
11
12 @Injectable()
13 export class VideoCaptionService {
14 constructor (
15 private authHttp: HttpClient,
16 private restService: RestService,
17 private restExtractor: RestExtractor
18 ) {}
19
20 listCaptions (videoId: number | string): Observable<ResultList<VideoCaption>> {
21 return this.authHttp.get<ResultList<VideoCaption>>(VideoService.BASE_VIDEO_URL + videoId + '/captions')
22 .pipe(map(res => {
23 sortBy(res.data, 'language', 'label')
24
25 return res
26 }))
27 .pipe(catchError(res => this.restExtractor.handleError(res)))
28 }
29
30 removeCaption (videoId: number | string, language: string) {
31 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + videoId + '/captions/' + language)
32 .pipe(
33 map(this.restExtractor.extractDataBool),
34 catchError(res => this.restExtractor.handleError(res))
35 )
36 }
37
38 addCaption (videoId: number | string, language: string, captionfile: File) {
39 const body = { captionfile }
40 const data = objectToFormData(body)
41
42 return this.authHttp.put(VideoService.BASE_VIDEO_URL + videoId + '/captions/' + language, data)
43 .pipe(
44 map(this.restExtractor.extractDataBool),
45 catchError(res => this.restExtractor.handleError(res))
46 )
47 }
48
49 updateCaptions (videoId: number | string, videoCaptions: VideoCaptionEdit[]) {
50 const observables: Observable<any>[] = []
51
52 for (const videoCaption of videoCaptions) {
53 if (videoCaption.action === 'CREATE') {
54 observables.push(
55 this.addCaption(videoId, videoCaption.language.id, videoCaption.captionfile)
56 )
57 } else if (videoCaption.action === 'REMOVE') {
58 observables.push(
59 this.removeCaption(videoId, videoCaption.language.id)
60 )
61 }
62 }
63
64 if (observables.length === 0) return of(true)
65
66 return forkJoin(observables)
67 }
68 }