]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/video-import/video-import.service.ts
Import magnets with webtorrent
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video-import / video-import.service.ts
index b4709866a811d89bcf8d70a2e99237a87f09c075..002412bd717e1c331f32732a5a659c90baa4a657 100644 (file)
@@ -1,5 +1,5 @@
-import { catchError } from 'rxjs/operators'
-import { HttpClient } from '@angular/common/http'
+import { catchError, map, switchMap } from 'rxjs/operators'
+import { HttpClient, HttpParams } from '@angular/common/http'
 import { Injectable } from '@angular/core'
 import { Observable } from 'rxjs'
 import { VideoImport } from '../../../../../shared'
@@ -8,6 +8,12 @@ import { RestExtractor, RestService } from '../rest'
 import { VideoImportCreate } from '../../../../../shared/models/videos/video-import-create.model'
 import { objectToFormData } from '@app/shared/misc/utils'
 import { VideoUpdate } from '../../../../../shared/models/videos'
+import { ResultList } from '../../../../../shared/models/result-list.model'
+import { UserService } from '@app/shared/users/user.service'
+import { SortMeta } from 'primeng/components/common/sortmeta'
+import { RestPagination } from '@app/shared/rest'
+import { ServerService } from '@app/core'
+import { peertubeTranslate } from '@app/shared/i18n/i18n-utils'
 
 @Injectable()
 export class VideoImportService {
@@ -16,11 +22,47 @@ export class VideoImportService {
   constructor (
     private authHttp: HttpClient,
     private restService: RestService,
-    private restExtractor: RestExtractor
+    private restExtractor: RestExtractor,
+    private serverService: ServerService
   ) {}
 
-  importVideo (targetUrl: string, video: VideoUpdate): Observable<VideoImport> {
+  importVideoUrl (targetUrl: string, video: VideoUpdate): Observable<VideoImport> {
     const url = VideoImportService.BASE_VIDEO_IMPORT_URL
+
+    const body = this.buildImportVideoObject(video)
+    body.targetUrl = targetUrl
+
+    const data = objectToFormData(body)
+    return this.authHttp.post<VideoImport>(url, data)
+               .pipe(catchError(res => this.restExtractor.handleError(res)))
+  }
+
+  importVideoTorrent (target: string | Blob, video: VideoUpdate): Observable<VideoImport> {
+    const url = VideoImportService.BASE_VIDEO_IMPORT_URL
+    const body: VideoImportCreate = this.buildImportVideoObject(video)
+
+    if (typeof target === 'string') body.magnetUri = target
+    else body.torrentfile = target
+
+    const data = objectToFormData(body)
+    return this.authHttp.post<VideoImport>(url, data)
+               .pipe(catchError(res => this.restExtractor.handleError(res)))
+  }
+
+  getMyVideoImports (pagination: RestPagination, sort: SortMeta): Observable<ResultList<VideoImport>> {
+    let params = new HttpParams()
+    params = this.restService.addRestGetParams(params, pagination, sort)
+
+    return this.authHttp
+               .get<ResultList<VideoImport>>(UserService.BASE_USERS_URL + '/me/videos/imports', { params })
+               .pipe(
+                 switchMap(res => this.extractVideoImports(res)),
+                 map(res => this.restExtractor.convertResultListDateToHuman(res)),
+                 catchError(err => this.restExtractor.handleError(err))
+               )
+  }
+
+  private buildImportVideoObject (video: VideoUpdate): VideoImportCreate {
     const language = video.language || null
     const licence = video.licence || null
     const category = video.category || null
@@ -28,9 +70,7 @@ export class VideoImportService {
     const support = video.support || null
     const scheduleUpdate = video.scheduleUpdate || null
 
-    const body: VideoImportCreate = {
-      targetUrl,
-
+    return {
       name: video.name,
       category,
       licence,
@@ -47,10 +87,18 @@ export class VideoImportService {
       previewfile: video.previewfile,
       scheduleUpdate
     }
-
-    const data = objectToFormData(body)
-    return this.authHttp.post<VideoImport>(url, data)
-               .pipe(catchError(res => this.restExtractor.handleError(res)))
   }
 
+  private extractVideoImports (result: ResultList<VideoImport>): Observable<ResultList<VideoImport>> {
+    return this.serverService.localeObservable
+               .pipe(
+                 map(translations => {
+                   result.data.forEach(d =>
+                     d.state.label = peertubeTranslate(d.state.label, translations)
+                   )
+
+                   return result
+                 })
+               )
+  }
 }