]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video-import/video-import.service.ts
Merge branch 'release/beta-10' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video-import / video-import.service.ts
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'
17
18 @Injectable()
19 export class VideoImportService {
20 private static BASE_VIDEO_IMPORT_URL = environment.apiUrl + '/api/v1/videos/imports/'
21
22 constructor (
23 private authHttp: HttpClient,
24 private restService: RestService,
25 private restExtractor: RestExtractor,
26 private serverService: ServerService
27 ) {}
28
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
37
38 const body: VideoImportCreate = {
39 targetUrl,
40
41 name: video.name,
42 category,
43 licence,
44 language,
45 support,
46 description,
47 channelId: video.channelId,
48 privacy: video.privacy,
49 tags: video.tags,
50 nsfw: video.nsfw,
51 waitTranscoding: video.waitTranscoding,
52 commentsEnabled: video.commentsEnabled,
53 thumbnailfile: video.thumbnailfile,
54 previewfile: video.previewfile,
55 scheduleUpdate
56 }
57
58 const data = objectToFormData(body)
59 return this.authHttp.post<VideoImport>(url, data)
60 .pipe(catchError(res => this.restExtractor.handleError(res)))
61 }
62
63 getMyVideoImports (pagination: RestPagination, sort: SortMeta): Observable<ResultList<VideoImport>> {
64 let params = new HttpParams()
65 params = this.restService.addRestGetParams(params, pagination, sort)
66
67 return this.authHttp
68 .get<ResultList<VideoImport>>(UserService.BASE_USERS_URL + '/me/videos/imports', { params })
69 .pipe(
70 switchMap(res => this.extractVideoImports(res)),
71 map(res => this.restExtractor.convertResultListDateToHuman(res)),
72 catchError(err => this.restExtractor.handleError(err))
73 )
74 }
75
76 private extractVideoImports (result: ResultList<VideoImport>): Observable<ResultList<VideoImport>> {
77 return this.serverService.localeObservable
78 .pipe(
79 map(translations => {
80 result.data.forEach(d =>
81 d.state.label = peertubeTranslate(d.state.label, translations)
82 )
83
84 return result
85 })
86 )
87 }
88 }