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 } from '../../../../../shared/models/videos/video-import-create.model'
9 import { objectToFormData } from '@app/shared/misc/utils'
10 import { VideoUpdate } from '../../../../../shared/models/videos'
11 import { ResultList } from '../../../../../shared/models/result-list.model'
12 import { UserService } from '@app/shared/users/user.service'
13 import { SortMeta } from 'primeng/components/common/sortmeta'
14 import { RestPagination } from '@app/shared/rest'
15 import { ServerService } from '@app/core'
16 import { peertubeTranslate } from '@app/shared/i18n/i18n-utils'
19 export class VideoImportService {
20 private static BASE_VIDEO_IMPORT_URL = environment.apiUrl + '/api/v1/videos/imports/'
23 private authHttp: HttpClient,
24 private restService: RestService,
25 private restExtractor: RestExtractor,
26 private serverService: ServerService
29 importVideo (targetUrl: string, video: VideoUpdate): Observable<VideoImport> {
30 const url = VideoImportService.BASE_VIDEO_IMPORT_URL
31 const language = video.language || null
32 const licence = video.licence || null
33 const category = video.category || null
34 const description = video.description || null
35 const support = video.support || null
36 const scheduleUpdate = video.scheduleUpdate || null
38 const body: VideoImportCreate = {
47 channelId: video.channelId,
48 privacy: video.privacy,
51 waitTranscoding: video.waitTranscoding,
52 commentsEnabled: video.commentsEnabled,
53 thumbnailfile: video.thumbnailfile,
54 previewfile: video.previewfile,
58 const data = objectToFormData(body)
59 return this.authHttp.post<VideoImport>(url, data)
60 .pipe(catchError(res => this.restExtractor.handleError(res)))
63 getMyVideoImports (pagination: RestPagination, sort: SortMeta): Observable<ResultList<VideoImport>> {
64 let params = new HttpParams()
65 params = this.restService.addRestGetParams(params, pagination, sort)
68 .get<ResultList<VideoImport>>(UserService.BASE_USERS_URL + '/me/videos/imports', { params })
70 switchMap(res => this.extractVideoImports(res)),
71 map(res => this.restExtractor.convertResultListDateToHuman(res)),
72 catchError(err => this.restExtractor.handleError(err))
76 private extractVideoImports (result: ResultList<VideoImport>): Observable<ResultList<VideoImport>> {
77 return this.serverService.localeObservable
80 result.data.forEach(d =>
81 d.state.label = peertubeTranslate(d.state.label, translations)