]>
Commit | Line | Data |
---|---|---|
40e87e9e C |
1 | import { catchError, map } from 'rxjs/operators' |
2 | import { HttpClient } from '@angular/common/http' | |
3 | import { Injectable } from '@angular/core' | |
329d9086 | 4 | import { forkJoin, Observable, of } from 'rxjs' |
40e87e9e C |
5 | import { ResultList } from '../../../../../shared' |
6 | import { RestExtractor, RestService } from '../rest' | |
7 | import { VideoCaption } from '../../../../../shared/models/videos/video-caption.model' | |
8 | import { VideoService } from '@app/shared/video/video.service' | |
ad774752 | 9 | import { objectToFormData, sortBy } from '@app/shared/misc/utils' |
40e87e9e C |
10 | import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.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') | |
ad774752 C |
22 | .pipe(map(res => { |
23 | sortBy(res.data, 'language', 'label') | |
24 | ||
25 | return res | |
26 | })) | |
40e87e9e C |
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 | ||
0f7fedc3 C |
64 | if (observables.length === 0) return of(true) |
65 | ||
40e87e9e C |
66 | return forkJoin(observables) |
67 | } | |
68 | } |