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