]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video-import/video-import.service.ts
002412bd717e1c331f32732a5a659c90baa4a657
[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 importVideoUrl (targetUrl: string, video: VideoUpdate): Observable<VideoImport> {
30 const url = VideoImportService.BASE_VIDEO_IMPORT_URL
31
32 const body = this.buildImportVideoObject(video)
33 body.targetUrl = targetUrl
34
35 const data = objectToFormData(body)
36 return this.authHttp.post<VideoImport>(url, data)
37 .pipe(catchError(res => this.restExtractor.handleError(res)))
38 }
39
40 importVideoTorrent (target: string | Blob, video: VideoUpdate): Observable<VideoImport> {
41 const url = VideoImportService.BASE_VIDEO_IMPORT_URL
42 const body: VideoImportCreate = this.buildImportVideoObject(video)
43
44 if (typeof target === 'string') body.magnetUri = target
45 else body.torrentfile = target
46
47 const data = objectToFormData(body)
48 return this.authHttp.post<VideoImport>(url, data)
49 .pipe(catchError(res => this.restExtractor.handleError(res)))
50 }
51
52 getMyVideoImports (pagination: RestPagination, sort: SortMeta): Observable<ResultList<VideoImport>> {
53 let params = new HttpParams()
54 params = this.restService.addRestGetParams(params, pagination, sort)
55
56 return this.authHttp
57 .get<ResultList<VideoImport>>(UserService.BASE_USERS_URL + '/me/videos/imports', { params })
58 .pipe(
59 switchMap(res => this.extractVideoImports(res)),
60 map(res => this.restExtractor.convertResultListDateToHuman(res)),
61 catchError(err => this.restExtractor.handleError(err))
62 )
63 }
64
65 private buildImportVideoObject (video: VideoUpdate): VideoImportCreate {
66 const language = video.language || null
67 const licence = video.licence || null
68 const category = video.category || null
69 const description = video.description || null
70 const support = video.support || null
71 const scheduleUpdate = video.scheduleUpdate || null
72
73 return {
74 name: video.name,
75 category,
76 licence,
77 language,
78 support,
79 description,
80 channelId: video.channelId,
81 privacy: video.privacy,
82 tags: video.tags,
83 nsfw: video.nsfw,
84 waitTranscoding: video.waitTranscoding,
85 commentsEnabled: video.commentsEnabled,
86 thumbnailfile: video.thumbnailfile,
87 previewfile: video.previewfile,
88 scheduleUpdate
89 }
90 }
91
92 private extractVideoImports (result: ResultList<VideoImport>): Observable<ResultList<VideoImport>> {
93 return this.serverService.localeObservable
94 .pipe(
95 map(translations => {
96 result.data.forEach(d =>
97 d.state.label = peertubeTranslate(d.state.label, translations)
98 )
99
100 return result
101 })
102 )
103 }
104 }