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'
13 export class VideoImportService {
14 private static BASE_VIDEO_IMPORT_URL = environment.apiUrl + '/api/v1/videos/imports/'
17 private authHttp: HttpClient,
18 private restService: RestService,
19 private restExtractor: RestExtractor,
20 private serverService: ServerService
23 importVideoUrl (targetUrl: string, video: VideoUpdate): Observable<VideoImport> {
24 const url = VideoImportService.BASE_VIDEO_IMPORT_URL
26 const body = this.buildImportVideoObject(video)
27 body.targetUrl = targetUrl
29 const data = objectToFormData(body)
30 return this.authHttp.post<VideoImport>(url, data)
31 .pipe(catchError(res => this.restExtractor.handleError(res)))
34 importVideoTorrent (target: string | Blob, video: VideoUpdate): Observable<VideoImport> {
35 const url = VideoImportService.BASE_VIDEO_IMPORT_URL
36 const body: VideoImportCreate = this.buildImportVideoObject(video)
38 if (typeof target === 'string') body.magnetUri = target
39 else body.torrentfile = target
41 const data = objectToFormData(body)
42 return this.authHttp.post<VideoImport>(url, data)
43 .pipe(catchError(res => this.restExtractor.handleError(res)))
46 getMyVideoImports (pagination: RestPagination, sort: SortMeta, search?: string): Observable<ResultList<VideoImport>> {
47 let params = new HttpParams()
48 params = this.restService.addRestGetParams(params, pagination, sort)
51 const filters = this.restService.parseQueryStringFilter(search, {
53 prefix: 'videoChannelSyncId:'
60 params = this.restService.addObjectParams(params, filters)
64 .get<ResultList<VideoImport>>(UserService.BASE_USERS_URL + '/me/videos/imports', { params })
66 switchMap(res => this.extractVideoImports(res)),
67 catchError(err => this.restExtractor.handleError(err))
71 deleteVideoImport (videoImport: VideoImport) {
72 return this.authHttp.delete(VideoImportService.BASE_VIDEO_IMPORT_URL + videoImport.id)
73 .pipe(catchError(err => this.restExtractor.handleError(err)))
76 cancelVideoImport (videoImport: VideoImport) {
77 return this.authHttp.post(VideoImportService.BASE_VIDEO_IMPORT_URL + videoImport.id + '/cancel', {})
78 .pipe(catchError(err => this.restExtractor.handleError(err)))
81 private buildImportVideoObject (video: VideoUpdate): VideoImportCreate {
82 const language = video.language || null
83 const licence = video.licence || null
84 const category = video.category || null
85 const description = video.description || null
86 const support = video.support || null
87 const scheduleUpdate = video.scheduleUpdate || null
88 const originallyPublishedAt = video.originallyPublishedAt || null
97 channelId: video.channelId,
98 privacy: video.privacy,
101 waitTranscoding: video.waitTranscoding,
102 commentsEnabled: video.commentsEnabled,
103 downloadEnabled: video.downloadEnabled,
104 thumbnailfile: video.thumbnailfile,
105 previewfile: video.previewfile,
107 originallyPublishedAt
111 private extractVideoImports (result: ResultList<VideoImport>): Observable<ResultList<VideoImport>> {
112 return this.serverService.getServerLocale()
114 map(translations => {
115 result.data.forEach(d =>
116 d.state.label = peertubeTranslate(d.state.label, translations)