]>
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' | |
ed4c3c09 | 4 | import { Observable, of } from 'rxjs' |
3dfa8494 | 5 | import { peertubeTranslate, ResultList } from '../../../../../shared' |
23db998f | 6 | import { RestExtractor } 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 restExtractor: RestExtractor |
19 | ) {} | |
20 | ||
21 | listCaptions (videoId: number | string): Observable<ResultList<VideoCaption>> { | |
22 | return this.authHttp.get<ResultList<VideoCaption>>(VideoService.BASE_VIDEO_URL + videoId + '/captions') | |
3dfa8494 C |
23 | .pipe( |
24 | switchMap(captionsResult => { | |
25 | return this.serverService.localeObservable | |
26 | .pipe(map(translations => ({ captionsResult, translations }))) | |
27 | }), | |
28 | map(({ captionsResult, translations }) => { | |
29 | for (const c of captionsResult.data) { | |
30 | c.language.label = peertubeTranslate(c.language.label, translations) | |
31 | } | |
32 | ||
33 | return captionsResult | |
34 | }), | |
35 | map(captionsResult => { | |
36 | sortBy(captionsResult.data, 'language', 'label') | |
ad774752 | 37 | |
3dfa8494 C |
38 | return captionsResult |
39 | }) | |
40 | ) | |
40e87e9e C |
41 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
42 | } | |
43 | ||
44 | removeCaption (videoId: number | string, language: string) { | |
45 | return this.authHttp.delete(VideoService.BASE_VIDEO_URL + videoId + '/captions/' + language) | |
46 | .pipe( | |
47 | map(this.restExtractor.extractDataBool), | |
48 | catchError(res => this.restExtractor.handleError(res)) | |
49 | ) | |
50 | } | |
51 | ||
52 | addCaption (videoId: number | string, language: string, captionfile: File) { | |
53 | const body = { captionfile } | |
54 | const data = objectToFormData(body) | |
55 | ||
56 | return this.authHttp.put(VideoService.BASE_VIDEO_URL + videoId + '/captions/' + language, data) | |
57 | .pipe( | |
58 | map(this.restExtractor.extractDataBool), | |
59 | catchError(res => this.restExtractor.handleError(res)) | |
60 | ) | |
61 | } | |
62 | ||
63 | updateCaptions (videoId: number | string, videoCaptions: VideoCaptionEdit[]) { | |
ed4c3c09 | 64 | let obs = of(true) |
40e87e9e C |
65 | |
66 | for (const videoCaption of videoCaptions) { | |
67 | if (videoCaption.action === 'CREATE') { | |
ed4c3c09 | 68 | obs = obs.pipe(switchMap(() => this.addCaption(videoId, videoCaption.language.id, videoCaption.captionfile))) |
40e87e9e | 69 | } else if (videoCaption.action === 'REMOVE') { |
ed4c3c09 | 70 | obs = obs.pipe(switchMap(() => this.removeCaption(videoId, videoCaption.language.id))) |
40e87e9e C |
71 | } |
72 | } | |
73 | ||
ed4c3c09 | 74 | return obs |
40e87e9e C |
75 | } |
76 | } |