From 67ed6552b831df66713bac9e672738796128d33f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 23 Jun 2020 14:10:17 +0200 Subject: Reorganize client shared modules --- .../app/shared/shared-main/video-caption/index.ts | 2 + .../video-caption/video-caption-edit.model.ts | 9 +++ .../video-caption/video-caption.service.ts | 74 ++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 client/src/app/shared/shared-main/video-caption/index.ts create mode 100644 client/src/app/shared/shared-main/video-caption/video-caption-edit.model.ts create mode 100644 client/src/app/shared/shared-main/video-caption/video-caption.service.ts (limited to 'client/src/app/shared/shared-main/video-caption') diff --git a/client/src/app/shared/shared-main/video-caption/index.ts b/client/src/app/shared/shared-main/video-caption/index.ts new file mode 100644 index 000000000..308200f27 --- /dev/null +++ b/client/src/app/shared/shared-main/video-caption/index.ts @@ -0,0 +1,2 @@ +export * from './video-caption-edit.model' +export * from './video-caption.service' diff --git a/client/src/app/shared/shared-main/video-caption/video-caption-edit.model.ts b/client/src/app/shared/shared-main/video-caption/video-caption-edit.model.ts new file mode 100644 index 000000000..732f20158 --- /dev/null +++ b/client/src/app/shared/shared-main/video-caption/video-caption-edit.model.ts @@ -0,0 +1,9 @@ +export interface VideoCaptionEdit { + language: { + id: string + label?: string + } + + action?: 'CREATE' | 'REMOVE' + captionfile?: any +} diff --git a/client/src/app/shared/shared-main/video-caption/video-caption.service.ts b/client/src/app/shared/shared-main/video-caption/video-caption.service.ts new file mode 100644 index 000000000..d45fb837a --- /dev/null +++ b/client/src/app/shared/shared-main/video-caption/video-caption.service.ts @@ -0,0 +1,74 @@ +import { Observable, of } from 'rxjs' +import { catchError, map, switchMap } from 'rxjs/operators' +import { HttpClient } from '@angular/common/http' +import { Injectable } from '@angular/core' +import { RestExtractor, ServerService } from '@app/core' +import { objectToFormData, sortBy } from '@app/helpers' +import { VideoService } from '@app/shared/shared-main/video' +import { peertubeTranslate, ResultList, VideoCaption } from '@shared/models' +import { VideoCaptionEdit } from './video-caption-edit.model' + +@Injectable() +export class VideoCaptionService { + constructor ( + private authHttp: HttpClient, + private serverService: ServerService, + private restExtractor: RestExtractor + ) {} + + listCaptions (videoId: number | string): Observable> { + return this.authHttp.get>(VideoService.BASE_VIDEO_URL + videoId + '/captions') + .pipe( + switchMap(captionsResult => { + return this.serverService.getServerLocale() + .pipe(map(translations => ({ captionsResult, translations }))) + }), + map(({ captionsResult, translations }) => { + for (const c of captionsResult.data) { + c.language.label = peertubeTranslate(c.language.label, translations) + } + + return captionsResult + }), + map(captionsResult => { + sortBy(captionsResult.data, 'language', 'label') + + return captionsResult + }) + ) + .pipe(catchError(res => this.restExtractor.handleError(res))) + } + + removeCaption (videoId: number | string, language: string) { + return this.authHttp.delete(VideoService.BASE_VIDEO_URL + videoId + '/captions/' + language) + .pipe( + map(this.restExtractor.extractDataBool), + catchError(res => this.restExtractor.handleError(res)) + ) + } + + addCaption (videoId: number | string, language: string, captionfile: File) { + const body = { captionfile } + const data = objectToFormData(body) + + return this.authHttp.put(VideoService.BASE_VIDEO_URL + videoId + '/captions/' + language, data) + .pipe( + map(this.restExtractor.extractDataBool), + catchError(res => this.restExtractor.handleError(res)) + ) + } + + updateCaptions (videoId: number | string, videoCaptions: VideoCaptionEdit[]) { + let obs = of(true) + + for (const videoCaption of videoCaptions) { + if (videoCaption.action === 'CREATE') { + obs = obs.pipe(switchMap(() => this.addCaption(videoId, videoCaption.language.id, videoCaption.captionfile))) + } else if (videoCaption.action === 'REMOVE') { + obs = obs.pipe(switchMap(() => this.removeCaption(videoId, videoCaption.language.id))) + } + } + + return obs + } +} -- cgit v1.2.3