]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/video-caption/video-caption.service.ts
Merge branch 'release/4.1.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / video-caption / video-caption.service.ts
1 import { Observable, of } from 'rxjs'
2 import { catchError, map, switchMap } from 'rxjs/operators'
3 import { HttpClient } from '@angular/common/http'
4 import { Injectable } from '@angular/core'
5 import { RestExtractor, ServerService } from '@app/core'
6 import { objectToFormData, sortBy } from '@app/helpers'
7 import { VideoService } from '@app/shared/shared-main/video'
8 import { peertubeTranslate } from '@shared/core-utils/i18n'
9 import { ResultList, VideoCaption } from '@shared/models'
10 import { environment } from '../../../../environments/environment'
11 import { VideoCaptionEdit } from './video-caption-edit.model'
12
13 @Injectable()
14 export class VideoCaptionService {
15 constructor (
16 private authHttp: HttpClient,
17 private serverService: ServerService,
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`)
23 .pipe(
24 switchMap(captionsResult => {
25 return this.serverService.getServerLocale()
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')
37
38 return captionsResult
39 })
40 )
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(catchError(res => this.restExtractor.handleError(res)))
47 }
48
49 addCaption (videoId: number | string, language: string, captionfile: File) {
50 const body = { captionfile }
51 const data = objectToFormData(body)
52
53 return this.authHttp.put(`${VideoService.BASE_VIDEO_URL}/${videoId}/captions/${language}`, data)
54 .pipe(catchError(res => this.restExtractor.handleError(res)))
55 }
56
57 updateCaptions (videoId: number | string, videoCaptions: VideoCaptionEdit[]) {
58 let obs: Observable<any> = of(undefined)
59
60 for (const videoCaption of videoCaptions) {
61 if (videoCaption.action === 'CREATE' || videoCaption.action === 'UPDATE') {
62 obs = obs.pipe(switchMap(() => this.addCaption(videoId, videoCaption.language.id, videoCaption.captionfile)))
63 } else if (videoCaption.action === 'REMOVE') {
64 obs = obs.pipe(switchMap(() => this.removeCaption(videoId, videoCaption.language.id)))
65 }
66 }
67
68 return obs
69 }
70
71 getCaptionContent ({ captionPath }: Pick<VideoCaption, 'captionPath'>) {
72 return this.authHttp.get(environment.originServerUrl + captionPath, { responseType: 'text' })
73 }
74 }