]>
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' | |
9 | import { objectToFormData } from '@app/shared/misc/utils' | |
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') | |
22 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
23 | } | |
24 | ||
25 | removeCaption (videoId: number | string, language: string) { | |
26 | return this.authHttp.delete(VideoService.BASE_VIDEO_URL + videoId + '/captions/' + language) | |
27 | .pipe( | |
28 | map(this.restExtractor.extractDataBool), | |
29 | catchError(res => this.restExtractor.handleError(res)) | |
30 | ) | |
31 | } | |
32 | ||
33 | addCaption (videoId: number | string, language: string, captionfile: File) { | |
34 | const body = { captionfile } | |
35 | const data = objectToFormData(body) | |
36 | ||
37 | return this.authHttp.put(VideoService.BASE_VIDEO_URL + videoId + '/captions/' + language, data) | |
38 | .pipe( | |
39 | map(this.restExtractor.extractDataBool), | |
40 | catchError(res => this.restExtractor.handleError(res)) | |
41 | ) | |
42 | } | |
43 | ||
44 | updateCaptions (videoId: number | string, videoCaptions: VideoCaptionEdit[]) { | |
329d9086 C |
45 | if (videoCaptions.length === 0) return of(true) |
46 | ||
40e87e9e C |
47 | const observables: Observable<any>[] = [] |
48 | ||
49 | for (const videoCaption of videoCaptions) { | |
50 | if (videoCaption.action === 'CREATE') { | |
51 | observables.push( | |
52 | this.addCaption(videoId, videoCaption.language.id, videoCaption.captionfile) | |
53 | ) | |
54 | } else if (videoCaption.action === 'REMOVE') { | |
55 | observables.push( | |
56 | this.removeCaption(videoId, videoCaption.language.id) | |
57 | ) | |
58 | } | |
59 | } | |
60 | ||
61 | return forkJoin(observables) | |
62 | } | |
63 | } |