]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/video/video-import.service.ts
Fix comment in PR template
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / video / video-import.service.ts
1 import { SortMeta } from 'primeng/api'
2 import { Observable } from 'rxjs'
3 import { catchError, map, switchMap } from 'rxjs/operators'
4 import { HttpClient, HttpParams } from '@angular/common/http'
5 import { Injectable } from '@angular/core'
6 import { RestExtractor, RestPagination, RestService, ServerService, UserService } from '@app/core'
7 import { objectToFormData } from '@app/helpers'
8 import { peertubeTranslate } from '@shared/core-utils/i18n'
9 import { ResultList, VideoImport, VideoImportCreate, VideoUpdate } from '@shared/models'
10 import { environment } from '../../../../environments/environment'
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,
19 private restExtractor: RestExtractor,
20 private serverService: ServerService
21 ) {}
22
23 importVideoUrl (targetUrl: string, video: VideoUpdate): Observable<VideoImport> {
24 const url = VideoImportService.BASE_VIDEO_IMPORT_URL
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
59 private buildImportVideoObject (video: VideoUpdate): VideoImportCreate {
60 const language = video.language || null
61 const licence = video.licence || null
62 const category = video.category || null
63 const description = video.description || null
64 const support = video.support || null
65 const scheduleUpdate = video.scheduleUpdate || null
66 const originallyPublishedAt = video.originallyPublishedAt || null
67
68 return {
69 name: video.name,
70 category,
71 licence,
72 language,
73 support,
74 description,
75 channelId: video.channelId,
76 privacy: video.privacy,
77 tags: video.tags,
78 nsfw: video.nsfw,
79 waitTranscoding: video.waitTranscoding,
80 commentsEnabled: video.commentsEnabled,
81 downloadEnabled: video.downloadEnabled,
82 thumbnailfile: video.thumbnailfile,
83 previewfile: video.previewfile,
84 scheduleUpdate,
85 originallyPublishedAt
86 }
87 }
88
89 private extractVideoImports (result: ResultList<VideoImport>): Observable<ResultList<VideoImport>> {
90 return this.serverService.getServerLocale()
91 .pipe(
92 map(translations => {
93 result.data.forEach(d =>
94 d.state.label = peertubeTranslate(d.state.label, translations)
95 )
96
97 return result
98 })
99 )
100 }
101 }