]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video-import/video-import.service.ts
Ensure youtubedl binary exists in ydl helper
[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 { 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/components/common/sortmeta'
13 import { RestPagination } from '@app/shared/rest'
14 import { ServerService } from '@app/core'
15 import { peertubeTranslate } from '@app/shared/i18n/i18n-utils'
16
17 @Injectable()
18 export class VideoImportService {
19 private static BASE_VIDEO_IMPORT_URL = environment.apiUrl + '/api/v1/videos/imports/'
20
21 constructor (
22 private authHttp: HttpClient,
23 private restService: RestService,
24 private restExtractor: RestExtractor,
25 private serverService: ServerService
26 ) {}
27
28 importVideoUrl (targetUrl: string, video: VideoUpdate): Observable<VideoImport> {
29 const url = VideoImportService.BASE_VIDEO_IMPORT_URL
30
31 const body = this.buildImportVideoObject(video)
32 body.targetUrl = targetUrl
33
34 const data = objectToFormData(body)
35 return this.authHttp.post<VideoImport>(url, data)
36 .pipe(catchError(res => this.restExtractor.handleError(res)))
37 }
38
39 importVideoTorrent (target: string | Blob, video: VideoUpdate): Observable<VideoImport> {
40 const url = VideoImportService.BASE_VIDEO_IMPORT_URL
41 const body: VideoImportCreate = this.buildImportVideoObject(video)
42
43 if (typeof target === 'string') body.magnetUri = target
44 else body.torrentfile = target
45
46 const data = objectToFormData(body)
47 return this.authHttp.post<VideoImport>(url, data)
48 .pipe(catchError(res => this.restExtractor.handleError(res)))
49 }
50
51 getMyVideoImports (pagination: RestPagination, sort: SortMeta): Observable<ResultList<VideoImport>> {
52 let params = new HttpParams()
53 params = this.restService.addRestGetParams(params, pagination, sort)
54
55 return this.authHttp
56 .get<ResultList<VideoImport>>(UserService.BASE_USERS_URL + '/me/videos/imports', { params })
57 .pipe(
58 switchMap(res => this.extractVideoImports(res)),
59 map(res => this.restExtractor.convertResultListDateToHuman(res)),
60 catchError(err => this.restExtractor.handleError(err))
61 )
62 }
63
64 private buildImportVideoObject (video: VideoUpdate): VideoImportCreate {
65 const language = video.language || null
66 const licence = video.licence || null
67 const category = video.category || null
68 const description = video.description || null
69 const support = video.support || null
70 const scheduleUpdate = video.scheduleUpdate || 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 thumbnailfile: video.thumbnailfile,
86 previewfile: video.previewfile,
87 scheduleUpdate
88 }
89 }
90
91 private extractVideoImports (result: ResultList<VideoImport>): Observable<ResultList<VideoImport>> {
92 return this.serverService.localeObservable
93 .pipe(
94 map(translations => {
95 result.data.forEach(d =>
96 d.state.label = peertubeTranslate(d.state.label, translations)
97 )
98
99 return result
100 })
101 )
102 }
103 }