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'
18 export class VideoImportService {
19 private static BASE_VIDEO_IMPORT_URL = environment.apiUrl + '/api/v1/videos/imports/'
22 private authHttp: HttpClient,
23 private restService: RestService,
24 private restExtractor: RestExtractor,
25 private serverService: ServerService
28 importVideoUrl (targetUrl: string, video: VideoUpdate): Observable<VideoImport> {
29 const url = VideoImportService.BASE_VIDEO_IMPORT_URL
31 const body = this.buildImportVideoObject(video)
32 body.targetUrl = targetUrl
34 const data = objectToFormData(body)
35 return this.authHttp.post<VideoImport>(url, data)
36 .pipe(catchError(res => this.restExtractor.handleError(res)))
39 importVideoTorrent (target: string | Blob, video: VideoUpdate): Observable<VideoImport> {
40 const url = VideoImportService.BASE_VIDEO_IMPORT_URL
41 const body: VideoImportCreate = this.buildImportVideoObject(video)
43 if (typeof target === 'string') body.magnetUri = target
44 else body.torrentfile = target
46 const data = objectToFormData(body)
47 return this.authHttp.post<VideoImport>(url, data)
48 .pipe(catchError(res => this.restExtractor.handleError(res)))
51 getMyVideoImports (pagination: RestPagination, sort: SortMeta): Observable<ResultList<VideoImport>> {
52 let params = new HttpParams()
53 params = this.restService.addRestGetParams(params, pagination, sort)
56 .get<ResultList<VideoImport>>(UserService.BASE_USERS_URL + '/me/videos/imports', { params })
58 switchMap(res => this.extractVideoImports(res)),
59 map(res => this.restExtractor.convertResultListDateToHuman(res)),
60 catchError(err => this.restExtractor.handleError(err))
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
79 channelId: video.channelId,
80 privacy: video.privacy,
83 waitTranscoding: video.waitTranscoding,
84 commentsEnabled: video.commentsEnabled,
85 thumbnailfile: video.thumbnailfile,
86 previewfile: video.previewfile,
91 private extractVideoImports (result: ResultList<VideoImport>): Observable<ResultList<VideoImport>> {
92 return this.serverService.localeObservable
95 result.data.forEach(d =>
96 d.state.label = peertubeTranslate(d.state.label, translations)