]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { SortMeta } from 'primeng/api' |
2 | import { Observable } from 'rxjs' | |
ed31c059 C |
3 | import { catchError, map, switchMap } from 'rxjs/operators' |
4 | import { HttpClient, HttpParams } from '@angular/common/http' | |
fbad87b0 | 5 | import { Injectable } from '@angular/core' |
67ed6552 C |
6 | import { RestExtractor, RestPagination, RestService, ServerService, UserService } from '@app/core' |
7 | import { objectToFormData } from '@app/helpers' | |
bd45d503 C |
8 | import { peertubeTranslate } from '@shared/core-utils/i18n' |
9 | import { ResultList, VideoImport, VideoImportCreate, VideoUpdate } from '@shared/models' | |
67ed6552 | 10 | import { environment } from '../../../../environments/environment' |
fbad87b0 C |
11 | |
12 | @Injectable() | |
13 | export class VideoImportService { | |
14 | private static BASE_VIDEO_IMPORT_URL = environment.apiUrl + '/api/v1/videos/imports/' | |
15 | ||
16 | constructor ( | |
17 | private authHttp: HttpClient, | |
18 | private restService: RestService, | |
ed31c059 C |
19 | private restExtractor: RestExtractor, |
20 | private serverService: ServerService | |
fbad87b0 C |
21 | ) {} |
22 | ||
ce33919c | 23 | importVideoUrl (targetUrl: string, video: VideoUpdate): Observable<VideoImport> { |
fbad87b0 | 24 | const url = VideoImportService.BASE_VIDEO_IMPORT_URL |
ce33919c C |
25 | |
26 | const body = this.buildImportVideoObject(video) | |
27 | body.targetUrl = targetUrl | |
28 | ||
29 | const data = objectToFormData(body) | |
30 | return this.authHttp.post<VideoImport>(url, data) | |
31 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
32 | } | |
33 | ||
34 | importVideoTorrent (target: string | Blob, video: VideoUpdate): Observable<VideoImport> { | |
35 | const url = VideoImportService.BASE_VIDEO_IMPORT_URL | |
36 | const body: VideoImportCreate = this.buildImportVideoObject(video) | |
37 | ||
38 | if (typeof target === 'string') body.magnetUri = target | |
39 | else body.torrentfile = target | |
40 | ||
41 | const data = objectToFormData(body) | |
42 | return this.authHttp.post<VideoImport>(url, data) | |
43 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
44 | } | |
45 | ||
46 | getMyVideoImports (pagination: RestPagination, sort: SortMeta): Observable<ResultList<VideoImport>> { | |
47 | let params = new HttpParams() | |
48 | params = this.restService.addRestGetParams(params, pagination, sort) | |
49 | ||
50 | return this.authHttp | |
51 | .get<ResultList<VideoImport>>(UserService.BASE_USERS_URL + '/me/videos/imports', { params }) | |
52 | .pipe( | |
53 | switchMap(res => this.extractVideoImports(res)), | |
54 | map(res => this.restExtractor.convertResultListDateToHuman(res)), | |
55 | catchError(err => this.restExtractor.handleError(err)) | |
56 | ) | |
57 | } | |
58 | ||
419b520c C |
59 | deleteVideoImport (videoImport: VideoImport) { |
60 | return this.authHttp.delete(VideoImportService.BASE_VIDEO_IMPORT_URL + videoImport.id) | |
61 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
62 | } | |
63 | ||
64 | cancelVideoImport (videoImport: VideoImport) { | |
65 | return this.authHttp.post(VideoImportService.BASE_VIDEO_IMPORT_URL + videoImport.id + '/cancel', {}) | |
66 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
67 | } | |
68 | ||
ce33919c | 69 | private buildImportVideoObject (video: VideoUpdate): VideoImportCreate { |
fbad87b0 C |
70 | const language = video.language || null |
71 | const licence = video.licence || null | |
72 | const category = video.category || null | |
73 | const description = video.description || null | |
74 | const support = video.support || null | |
75 | const scheduleUpdate = video.scheduleUpdate || null | |
1e74f19a | 76 | const originallyPublishedAt = video.originallyPublishedAt || null |
fbad87b0 | 77 | |
ce33919c | 78 | return { |
fbad87b0 C |
79 | name: video.name, |
80 | category, | |
81 | licence, | |
82 | language, | |
83 | support, | |
84 | description, | |
85 | channelId: video.channelId, | |
86 | privacy: video.privacy, | |
87 | tags: video.tags, | |
88 | nsfw: video.nsfw, | |
89 | waitTranscoding: video.waitTranscoding, | |
90 | commentsEnabled: video.commentsEnabled, | |
7f2cfe3a | 91 | downloadEnabled: video.downloadEnabled, |
fbad87b0 C |
92 | thumbnailfile: video.thumbnailfile, |
93 | previewfile: video.previewfile, | |
1e74f19a | 94 | scheduleUpdate, |
95 | originallyPublishedAt | |
fbad87b0 | 96 | } |
ed31c059 C |
97 | } |
98 | ||
99 | private extractVideoImports (result: ResultList<VideoImport>): Observable<ResultList<VideoImport>> { | |
ba430d75 | 100 | return this.serverService.getServerLocale() |
ed31c059 C |
101 | .pipe( |
102 | map(translations => { | |
103 | result.data.forEach(d => | |
104 | d.state.label = peertubeTranslate(d.state.label, translations) | |
105 | ) | |
106 | ||
107 | return result | |
108 | }) | |
109 | ) | |
110 | } | |
fbad87b0 | 111 | } |