]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
67ed6552
C
1import { SortMeta } from 'primeng/api'
2import { Observable } from 'rxjs'
ed31c059
C
3import { catchError, map, switchMap } from 'rxjs/operators'
4import { HttpClient, HttpParams } from '@angular/common/http'
fbad87b0 5import { Injectable } from '@angular/core'
67ed6552
C
6import { RestExtractor, RestPagination, RestService, ServerService, UserService } from '@app/core'
7import { objectToFormData } from '@app/helpers'
8import { peertubeTranslate, ResultList, VideoImport, VideoImportCreate, VideoUpdate } from '@shared/models'
9import { environment } from '../../../../environments/environment'
fbad87b0
C
10
11@Injectable()
12export 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,
ed31c059
C
18 private restExtractor: RestExtractor,
19 private serverService: ServerService
fbad87b0
C
20 ) {}
21
ce33919c 22 importVideoUrl (targetUrl: string, video: VideoUpdate): Observable<VideoImport> {
fbad87b0 23 const url = VideoImportService.BASE_VIDEO_IMPORT_URL
ce33919c
C
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 {
fbad87b0
C
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
1e74f19a 65 const originallyPublishedAt = video.originallyPublishedAt || null
fbad87b0 66
ce33919c 67 return {
fbad87b0
C
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,
7f2cfe3a 80 downloadEnabled: video.downloadEnabled,
fbad87b0
C
81 thumbnailfile: video.thumbnailfile,
82 previewfile: video.previewfile,
1e74f19a 83 scheduleUpdate,
84 originallyPublishedAt
fbad87b0 85 }
ed31c059
C
86 }
87
88 private extractVideoImports (result: ResultList<VideoImport>): Observable<ResultList<VideoImport>> {
ba430d75 89 return this.serverService.getServerLocale()
ed31c059
C
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 }
fbad87b0 100}