diff options
author | Chocobozzz <me@florianbigard.com> | 2020-06-23 14:10:17 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-06-23 16:00:49 +0200 |
commit | 67ed6552b831df66713bac9e672738796128d33f (patch) | |
tree | 59c97d41e0b49d75a90aa3de987968ab9b1ff447 /client/src/app/shared/shared-main/video-caption | |
parent | 0c4bacbff53bc732f5a2677d62a6ead7752e2405 (diff) | |
download | PeerTube-67ed6552b831df66713bac9e672738796128d33f.tar.gz PeerTube-67ed6552b831df66713bac9e672738796128d33f.tar.zst PeerTube-67ed6552b831df66713bac9e672738796128d33f.zip |
Reorganize client shared modules
Diffstat (limited to 'client/src/app/shared/shared-main/video-caption')
3 files changed, 85 insertions, 0 deletions
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 @@ | |||
1 | export * from './video-caption-edit.model' | ||
2 | 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 @@ | |||
1 | export interface VideoCaptionEdit { | ||
2 | language: { | ||
3 | id: string | ||
4 | label?: string | ||
5 | } | ||
6 | |||
7 | action?: 'CREATE' | 'REMOVE' | ||
8 | captionfile?: any | ||
9 | } | ||
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 @@ | |||
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, ResultList, VideoCaption } from '@shared/models' | ||
9 | import { VideoCaptionEdit } from './video-caption-edit.model' | ||
10 | |||
11 | @Injectable() | ||
12 | export class VideoCaptionService { | ||
13 | constructor ( | ||
14 | private authHttp: HttpClient, | ||
15 | private serverService: ServerService, | ||
16 | private restExtractor: RestExtractor | ||
17 | ) {} | ||
18 | |||
19 | listCaptions (videoId: number | string): Observable<ResultList<VideoCaption>> { | ||
20 | return this.authHttp.get<ResultList<VideoCaption>>(VideoService.BASE_VIDEO_URL + videoId + '/captions') | ||
21 | .pipe( | ||
22 | switchMap(captionsResult => { | ||
23 | return this.serverService.getServerLocale() | ||
24 | .pipe(map(translations => ({ captionsResult, translations }))) | ||
25 | }), | ||
26 | map(({ captionsResult, translations }) => { | ||
27 | for (const c of captionsResult.data) { | ||
28 | c.language.label = peertubeTranslate(c.language.label, translations) | ||
29 | } | ||
30 | |||
31 | return captionsResult | ||
32 | }), | ||
33 | map(captionsResult => { | ||
34 | sortBy(captionsResult.data, 'language', 'label') | ||
35 | |||
36 | return captionsResult | ||
37 | }) | ||
38 | ) | ||
39 | .pipe(catchError(res => this.restExtractor.handleError(res))) | ||
40 | } | ||
41 | |||
42 | removeCaption (videoId: number | string, language: string) { | ||
43 | return this.authHttp.delete(VideoService.BASE_VIDEO_URL + videoId + '/captions/' + language) | ||
44 | .pipe( | ||
45 | map(this.restExtractor.extractDataBool), | ||
46 | catchError(res => this.restExtractor.handleError(res)) | ||
47 | ) | ||
48 | } | ||
49 | |||
50 | addCaption (videoId: number | string, language: string, captionfile: File) { | ||
51 | const body = { captionfile } | ||
52 | const data = objectToFormData(body) | ||
53 | |||
54 | return this.authHttp.put(VideoService.BASE_VIDEO_URL + videoId + '/captions/' + language, data) | ||
55 | .pipe( | ||
56 | map(this.restExtractor.extractDataBool), | ||
57 | catchError(res => this.restExtractor.handleError(res)) | ||
58 | ) | ||
59 | } | ||
60 | |||
61 | updateCaptions (videoId: number | string, videoCaptions: VideoCaptionEdit[]) { | ||
62 | let obs = of(true) | ||
63 | |||
64 | for (const videoCaption of videoCaptions) { | ||
65 | if (videoCaption.action === 'CREATE') { | ||
66 | obs = obs.pipe(switchMap(() => this.addCaption(videoId, videoCaption.language.id, videoCaption.captionfile))) | ||
67 | } else if (videoCaption.action === 'REMOVE') { | ||
68 | obs = obs.pipe(switchMap(() => this.removeCaption(videoId, videoCaption.language.id))) | ||
69 | } | ||
70 | } | ||
71 | |||
72 | return obs | ||
73 | } | ||
74 | } | ||