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