]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/video/video-import.service.ts
Fix embed url
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / video / video-import.service.ts
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'
10
11 @Injectable()
12 export class VideoImportService {
13 private static BASE_VIDEO_IMPORT_URL = environment.apiUrl + '/api/v1/videos/imports/'
14
15 constructor (
16 private authHttp: HttpClient,
17 private restService: RestService,
18 private restExtractor: RestExtractor,
19 private serverService: ServerService
20 ) {}
21
22 importVideoUrl (targetUrl: string, video: VideoUpdate): Observable<VideoImport> {
23 const url = VideoImportService.BASE_VIDEO_IMPORT_URL
24
25 const body = this.buildImportVideoObject(video)
26 body.targetUrl = targetUrl
27
28 const data = objectToFormData(body)
29 return this.authHttp.post<VideoImport>(url, data)
30 .pipe(catchError(res => this.restExtractor.handleError(res)))
31 }
32
33 importVideoTorrent (target: string | Blob, video: VideoUpdate): Observable<VideoImport> {
34 const url = VideoImportService.BASE_VIDEO_IMPORT_URL
35 const body: VideoImportCreate = this.buildImportVideoObject(video)
36
37 if (typeof target === 'string') body.magnetUri = target
38 else body.torrentfile = target
39
40 const data = objectToFormData(body)
41 return this.authHttp.post<VideoImport>(url, data)
42 .pipe(catchError(res => this.restExtractor.handleError(res)))
43 }
44
45 getMyVideoImports (pagination: RestPagination, sort: SortMeta): Observable<ResultList<VideoImport>> {
46 let params = new HttpParams()
47 params = this.restService.addRestGetParams(params, pagination, sort)
48
49 return this.authHttp
50 .get<ResultList<VideoImport>>(UserService.BASE_USERS_URL + '/me/videos/imports', { params })
51 .pipe(
52 switchMap(res => this.extractVideoImports(res)),
53 map(res => this.restExtractor.convertResultListDateToHuman(res)),
54 catchError(err => this.restExtractor.handleError(err))
55 )
56 }
57
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
66
67 return {
68 name: video.name,
69 category,
70 licence,
71 language,
72 support,
73 description,
74 channelId: video.channelId,
75 privacy: video.privacy,
76 tags: video.tags,
77 nsfw: video.nsfw,
78 waitTranscoding: video.waitTranscoding,
79 commentsEnabled: video.commentsEnabled,
80 downloadEnabled: video.downloadEnabled,
81 thumbnailfile: video.thumbnailfile,
82 previewfile: video.previewfile,
83 scheduleUpdate,
84 originallyPublishedAt
85 }
86 }
87
88 private extractVideoImports (result: ResultList<VideoImport>): Observable<ResultList<VideoImport>> {
89 return this.serverService.getServerLocale()
90 .pipe(
91 map(translations => {
92 result.data.forEach(d =>
93 d.state.label = peertubeTranslate(d.state.label, translations)
94 )
95
96 return result
97 })
98 )
99 }
100 }