aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/video-caption/video-caption.service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/shared/video-caption/video-caption.service.ts')
-rw-r--r--client/src/app/shared/video-caption/video-caption.service.ts61
1 files changed, 61 insertions, 0 deletions
diff --git a/client/src/app/shared/video-caption/video-caption.service.ts b/client/src/app/shared/video-caption/video-caption.service.ts
new file mode 100644
index 000000000..4ae8ebd0a
--- /dev/null
+++ b/client/src/app/shared/video-caption/video-caption.service.ts
@@ -0,0 +1,61 @@
1import { catchError, map } from 'rxjs/operators'
2import { HttpClient } from '@angular/common/http'
3import { Injectable } from '@angular/core'
4import { forkJoin, Observable } from 'rxjs'
5import { ResultList } from '../../../../../shared'
6import { RestExtractor, RestService } from '../rest'
7import { VideoCaption } from '../../../../../shared/models/videos/video-caption.model'
8import { VideoService } from '@app/shared/video/video.service'
9import { objectToFormData } from '@app/shared/misc/utils'
10import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model'
11
12@Injectable()
13export 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[]) {
45 const observables: Observable<any>[] = []
46
47 for (const videoCaption of videoCaptions) {
48 if (videoCaption.action === 'CREATE') {
49 observables.push(
50 this.addCaption(videoId, videoCaption.language.id, videoCaption.captionfile)
51 )
52 } else if (videoCaption.action === 'REMOVE') {
53 observables.push(
54 this.removeCaption(videoId, videoCaption.language.id)
55 )
56 }
57 }
58
59 return forkJoin(observables)
60 }
61}