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