]>
Commit | Line | Data |
---|---|---|
3dfa8494 | 1 | import { catchError, map, switchMap } from 'rxjs/operators' |
40e87e9e C |
2 | import { HttpClient } from '@angular/common/http' |
3 | import { Injectable } from '@angular/core' | |
329d9086 | 4 | import { forkJoin, Observable, of } from 'rxjs' |
3dfa8494 | 5 | import { peertubeTranslate, ResultList } from '../../../../../shared' |
40e87e9e | 6 | import { RestExtractor, RestService } from '../rest' |
40e87e9e | 7 | import { VideoService } from '@app/shared/video/video.service' |
ad774752 | 8 | import { objectToFormData, sortBy } from '@app/shared/misc/utils' |
40e87e9e | 9 | import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model' |
e63dbd42 | 10 | import { VideoCaption } from '../../../../../shared/models/videos/caption/video-caption.model' |
3dfa8494 | 11 | import { ServerService } from '@app/core' |
40e87e9e C |
12 | |
13 | @Injectable() | |
14 | export class VideoCaptionService { | |
15 | constructor ( | |
16 | private authHttp: HttpClient, | |
3dfa8494 | 17 | private serverService: ServerService, |
40e87e9e C |
18 | private restService: RestService, |
19 | private restExtractor: RestExtractor | |
20 | ) {} | |
21 | ||
22 | listCaptions (videoId: number | string): Observable<ResultList<VideoCaption>> { | |
23 | return this.authHttp.get<ResultList<VideoCaption>>(VideoService.BASE_VIDEO_URL + videoId + '/captions') | |
3dfa8494 C |
24 | .pipe( |
25 | switchMap(captionsResult => { | |
26 | return this.serverService.localeObservable | |
27 | .pipe(map(translations => ({ captionsResult, translations }))) | |
28 | }), | |
29 | map(({ captionsResult, translations }) => { | |
30 | for (const c of captionsResult.data) { | |
31 | c.language.label = peertubeTranslate(c.language.label, translations) | |
32 | } | |
33 | ||
34 | return captionsResult | |
35 | }), | |
36 | map(captionsResult => { | |
37 | sortBy(captionsResult.data, 'language', 'label') | |
ad774752 | 38 | |
3dfa8494 C |
39 | return captionsResult |
40 | }) | |
41 | ) | |
40e87e9e C |
42 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
43 | } | |
44 | ||
45 | removeCaption (videoId: number | string, language: string) { | |
46 | return this.authHttp.delete(VideoService.BASE_VIDEO_URL + videoId + '/captions/' + language) | |
47 | .pipe( | |
48 | map(this.restExtractor.extractDataBool), | |
49 | catchError(res => this.restExtractor.handleError(res)) | |
50 | ) | |
51 | } | |
52 | ||
53 | addCaption (videoId: number | string, language: string, captionfile: File) { | |
54 | const body = { captionfile } | |
55 | const data = objectToFormData(body) | |
56 | ||
57 | return this.authHttp.put(VideoService.BASE_VIDEO_URL + videoId + '/captions/' + language, data) | |
58 | .pipe( | |
59 | map(this.restExtractor.extractDataBool), | |
60 | catchError(res => this.restExtractor.handleError(res)) | |
61 | ) | |
62 | } | |
63 | ||
64 | updateCaptions (videoId: number | string, videoCaptions: VideoCaptionEdit[]) { | |
65 | const observables: Observable<any>[] = [] | |
66 | ||
67 | for (const videoCaption of videoCaptions) { | |
68 | if (videoCaption.action === 'CREATE') { | |
69 | observables.push( | |
70 | this.addCaption(videoId, videoCaption.language.id, videoCaption.captionfile) | |
71 | ) | |
72 | } else if (videoCaption.action === 'REMOVE') { | |
73 | observables.push( | |
74 | this.removeCaption(videoId, videoCaption.language.id) | |
75 | ) | |
76 | } | |
77 | } | |
78 | ||
0f7fedc3 C |
79 | if (observables.length === 0) return of(true) |
80 | ||
40e87e9e C |
81 | return forkJoin(observables) |
82 | } | |
83 | } |