]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
ed31c059
C
1import { catchError, map, switchMap } from 'rxjs/operators'
2import { HttpClient, HttpParams } from '@angular/common/http'
fbad87b0
C
3import { Injectable } from '@angular/core'
4import { Observable } from 'rxjs'
5import { VideoImport } from '../../../../../shared'
6import { environment } from '../../../environments/environment'
7import { RestExtractor, RestService } from '../rest'
a76138ff 8import { VideoImportCreate, VideoUpdate } from '../../../../../shared/models/videos'
fbad87b0 9import { objectToFormData } from '@app/shared/misc/utils'
ed31c059
C
10import { ResultList } from '../../../../../shared/models/result-list.model'
11import { UserService } from '@app/shared/users/user.service'
12import { SortMeta } from 'primeng/components/common/sortmeta'
13import { RestPagination } from '@app/shared/rest'
14import { ServerService } from '@app/core'
15import { peertubeTranslate } from '@app/shared/i18n/i18n-utils'
fbad87b0
C
16
17@Injectable()
18export 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,
ed31c059
C
24 private restExtractor: RestExtractor,
25 private serverService: ServerService
fbad87b0
C
26 ) {}
27
ce33919c 28 importVideoUrl (targetUrl: string, video: VideoUpdate): Observable<VideoImport> {
fbad87b0 29 const url = VideoImportService.BASE_VIDEO_IMPORT_URL
ce33919c
C
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 {
fbad87b0
C
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
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,
85 thumbnailfile: video.thumbnailfile,
86 previewfile: video.previewfile,
87 scheduleUpdate
88 }
ed31c059
C
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 }
fbad87b0 103}