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, ResultList, VideoImport, VideoImportCreate, VideoUpdate } from '@shared/models'
9 import { environment } from '../../../../environments/environment'
12 export class VideoImportService {
13 private static BASE_VIDEO_IMPORT_URL = environment.apiUrl + '/api/v1/videos/imports/'
16 private authHttp: HttpClient,
17 private restService: RestService,
18 private restExtractor: RestExtractor,
19 private serverService: ServerService
22 importVideoUrl (targetUrl: string, video: VideoUpdate): Observable<VideoImport> {
23 const url = VideoImportService.BASE_VIDEO_IMPORT_URL
25 const body = this.buildImportVideoObject(video)
26 body.targetUrl = targetUrl
28 const data = objectToFormData(body)
29 return this.authHttp.post<VideoImport>(url, data)
30 .pipe(catchError(res => this.restExtractor.handleError(res)))
33 importVideoTorrent (target: string | Blob, video: VideoUpdate): Observable<VideoImport> {
34 const url = VideoImportService.BASE_VIDEO_IMPORT_URL
35 const body: VideoImportCreate = this.buildImportVideoObject(video)
37 if (typeof target === 'string') body.magnetUri = target
38 else body.torrentfile = target
40 const data = objectToFormData(body)
41 return this.authHttp.post<VideoImport>(url, data)
42 .pipe(catchError(res => this.restExtractor.handleError(res)))
45 getMyVideoImports (pagination: RestPagination, sort: SortMeta): Observable<ResultList<VideoImport>> {
46 let params = new HttpParams()
47 params = this.restService.addRestGetParams(params, pagination, sort)
50 .get<ResultList<VideoImport>>(UserService.BASE_USERS_URL + '/me/videos/imports', { params })
52 switchMap(res => this.extractVideoImports(res)),
53 map(res => this.restExtractor.convertResultListDateToHuman(res)),
54 catchError(err => this.restExtractor.handleError(err))
58 private buildImportVideoObject (video: VideoUpdate): VideoImportCreate {
59 const language = video.language || null
60 const licence = video.licence || null
61 const category = video.category || null
62 const description = video.description || null
63 const support = video.support || null
64 const scheduleUpdate = video.scheduleUpdate || null
65 const originallyPublishedAt = video.originallyPublishedAt || null
74 channelId: video.channelId,
75 privacy: video.privacy,
78 waitTranscoding: video.waitTranscoding,
79 commentsEnabled: video.commentsEnabled,
80 downloadEnabled: video.downloadEnabled,
81 thumbnailfile: video.thumbnailfile,
82 previewfile: video.previewfile,
88 private extractVideoImports (result: ResultList<VideoImport>): Observable<ResultList<VideoImport>> {
89 return this.serverService.getServerLocale()
92 result.data.forEach(d =>
93 d.state.label = peertubeTranslate(d.state.label, translations)