]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video-import/video-import.service.ts
Merge branch 'release/2.1.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video-import / video-import.service.ts
1 import { catchError, map, switchMap } from 'rxjs/operators'
2 import { HttpClient, HttpParams } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { Observable } from 'rxjs'
5 import { peertubeTranslate, VideoImport } from '../../../../../shared'
6 import { environment } from '../../../environments/environment'
7 import { RestExtractor, RestService } from '../rest'
8 import { VideoImportCreate, VideoUpdate } from '../../../../../shared/models/videos'
9 import { objectToFormData } from '@app/shared/misc/utils'
10 import { ResultList } from '../../../../../shared/models/result-list.model'
11 import { UserService } from '@app/shared/users/user.service'
12 import { SortMeta } from 'primeng/api'
13 import { RestPagination } from '@app/shared/rest'
14 import { ServerService } from '@app/core'
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,
23 private restExtractor: RestExtractor,
24 private serverService: ServerService
25 ) {}
26
27 importVideoUrl (targetUrl: string, video: VideoUpdate): Observable<VideoImport> {
28 const url = VideoImportService.BASE_VIDEO_IMPORT_URL
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 {
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
70 const originallyPublishedAt = video.originallyPublishedAt || null
71
72 return {
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,
85 downloadEnabled: video.downloadEnabled,
86 thumbnailfile: video.thumbnailfile,
87 previewfile: video.previewfile,
88 scheduleUpdate,
89 originallyPublishedAt
90 }
91 }
92
93 private extractVideoImports (result: ResultList<VideoImport>): Observable<ResultList<VideoImport>> {
94 return this.serverService.getServerLocale()
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 }
105 }