1 import { switchMap } from 'rxjs'
2 import { AfterViewInit, Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
3 import { Router } from '@angular/router'
4 import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core'
5 import { scrollToTop } from '@app/helpers'
6 import { FormValidatorService } from '@app/shared/shared-forms'
7 import { VideoCaptionService, VideoEdit, VideoImportService, VideoService } from '@app/shared/shared-main'
8 import { LoadingBarService } from '@ngx-loading-bar/core'
9 import { PeerTubeProblemDocument, ServerErrorCode, VideoUpdate } from '@shared/models'
10 import { hydrateFormFromVideo } from '../shared/video-edit-utils'
11 import { VideoSend } from './video-send'
14 selector: 'my-video-import-torrent',
15 templateUrl: './video-import-torrent.component.html',
17 '../shared/video-edit.component.scss',
18 './video-import-torrent.component.scss',
22 export class VideoImportTorrentComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
23 @Output() firstStepDone = new EventEmitter<string>()
24 @Output() firstStepError = new EventEmitter<void>()
25 @ViewChild('torrentfileInput') torrentfileInput: ElementRef<HTMLInputElement>
29 isImportingVideo = false
30 hasImportedVideo = false
31 isUpdatingVideo = false
37 protected formValidatorService: FormValidatorService,
38 protected loadingBar: LoadingBarService,
39 protected notifier: Notifier,
40 protected authService: AuthService,
41 protected serverService: ServerService,
42 protected videoService: VideoService,
43 protected videoCaptionService: VideoCaptionService,
44 private router: Router,
45 private videoImportService: VideoImportService,
46 private hooks: HooksService
56 this.hooks.runAction('action:video-torrent-import.init', 'video-edit')
60 return { canDeactivate: true }
64 return !!this.magnetUri
68 const torrentfile = this.torrentfileInput.nativeElement.files[0]
69 if (!torrentfile) return
71 this.importVideo(torrentfile)
74 setTorrentFile (files: FileList) {
75 this.torrentfileInput.nativeElement.files = files
79 importVideo (torrentfile?: Blob) {
80 this.isImportingVideo = true
82 const videoUpdate: VideoUpdate = {
83 privacy: this.highestPrivacy,
84 waitTranscoding: false,
85 channelId: this.firstStepChannelId
88 this.loadingBar.useRef().start()
90 this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate)
91 .pipe(switchMap(({ video }) => this.videoService.getVideo({ videoId: video.uuid })))
94 this.loadingBar.useRef().complete()
95 this.firstStepDone.emit(video.name)
96 this.isImportingVideo = false
97 this.hasImportedVideo = true
99 this.video = new VideoEdit(video)
100 this.video.patch({ privacy: this.firstStepPrivacyId })
102 hydrateFormFromVideo(this.form, this.video, false)
106 this.loadingBar.useRef().complete()
107 this.isImportingVideo = false
108 this.firstStepError.emit()
110 let message = err.message
112 const error = err.body as PeerTubeProblemDocument
113 if (error?.code === ServerErrorCode.INCORRECT_FILES_IN_TORRENT) {
114 message = $localize`Torrents with only 1 file are supported.`
117 this.notifier.error(message)
122 async updateSecondStep () {
123 if (!await this.isFormValid()) return
125 this.video.patch(this.form.value)
127 this.isUpdatingVideo = true
130 this.updateVideoAndCaptions(this.video)
133 this.isUpdatingVideo = false
134 this.notifier.success($localize`Video to import updated.`)
136 this.router.navigate([ '/my-library', 'video-imports' ])
140 this.error = err.message