1 import { catchError, map } from 'rxjs/operators'
2 import { HttpClient } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { forkJoin, Observable, of } from 'rxjs'
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, sortBy } from '@app/shared/misc/utils'
10 import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model'
13 export class VideoCaptionService {
15 private authHttp: HttpClient,
16 private restService: RestService,
17 private restExtractor: RestExtractor
20 listCaptions (videoId: number | string): Observable<ResultList<VideoCaption>> {
21 return this.authHttp.get<ResultList<VideoCaption>>(VideoService.BASE_VIDEO_URL + videoId + '/captions')
23 sortBy(res.data, 'language', 'label')
27 .pipe(catchError(res => this.restExtractor.handleError(res)))
30 removeCaption (videoId: number | string, language: string) {
31 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + videoId + '/captions/' + language)
33 map(this.restExtractor.extractDataBool),
34 catchError(res => this.restExtractor.handleError(res))
38 addCaption (videoId: number | string, language: string, captionfile: File) {
39 const body = { captionfile }
40 const data = objectToFormData(body)
42 return this.authHttp.put(VideoService.BASE_VIDEO_URL + videoId + '/captions/' + language, data)
44 map(this.restExtractor.extractDataBool),
45 catchError(res => this.restExtractor.handleError(res))
49 updateCaptions (videoId: number | string, videoCaptions: VideoCaptionEdit[]) {
50 const observables: Observable<any>[] = []
52 for (const videoCaption of videoCaptions) {
53 if (videoCaption.action === 'CREATE') {
55 this.addCaption(videoId, videoCaption.language.id, videoCaption.captionfile)
57 } else if (videoCaption.action === 'REMOVE') {
59 this.removeCaption(videoId, videoCaption.language.id)
64 if (observables.length === 0) return of(true)
66 return forkJoin(observables)