1 import { AfterViewInit, Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core'
4 import { scrollToTop } from '@app/helpers'
5 import { FormValidatorService } from '@app/shared/shared-forms'
6 import { VideoCaptionService, VideoEdit, VideoImportService, VideoService } from '@app/shared/shared-main'
7 import { LoadingBarService } from '@ngx-loading-bar/core'
8 import { PeerTubeProblemDocument, ServerErrorCode, VideoUpdate } from '@shared/models'
9 import { hydrateFormFromVideo } from '../shared/video-edit-utils'
10 import { VideoSend } from './video-send'
13 selector: 'my-video-import-torrent',
14 templateUrl: './video-import-torrent.component.html',
16 '../shared/video-edit.component.scss',
17 './video-import-torrent.component.scss',
21 export class VideoImportTorrentComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
22 @Output() firstStepDone = new EventEmitter<string>()
23 @Output() firstStepError = new EventEmitter<void>()
24 @ViewChild('torrentfileInput') torrentfileInput: ElementRef<HTMLInputElement>
28 isImportingVideo = false
29 hasImportedVideo = false
30 isUpdatingVideo = false
36 protected formValidatorService: FormValidatorService,
37 protected loadingBar: LoadingBarService,
38 protected notifier: Notifier,
39 protected authService: AuthService,
40 protected serverService: ServerService,
41 protected videoService: VideoService,
42 protected videoCaptionService: VideoCaptionService,
43 private router: Router,
44 private videoImportService: VideoImportService,
45 private hooks: HooksService
55 this.hooks.runAction('action:video-torrent-import.init', 'video-edit')
59 return { canDeactivate: true }
63 return !!this.magnetUri
67 const torrentfile = this.torrentfileInput.nativeElement.files[0]
68 if (!torrentfile) return
70 this.importVideo(torrentfile)
73 setTorrentFile (files: FileList) {
74 this.torrentfileInput.nativeElement.files = files
78 importVideo (torrentfile?: Blob) {
79 this.isImportingVideo = true
81 const videoUpdate: VideoUpdate = {
82 privacy: this.highestPrivacy,
83 waitTranscoding: false,
84 commentsEnabled: true,
85 downloadEnabled: true,
86 channelId: this.firstStepChannelId
89 this.loadingBar.useRef().start()
91 this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate)
94 this.loadingBar.useRef().complete()
95 this.firstStepDone.emit(res.video.name)
96 this.isImportingVideo = false
97 this.hasImportedVideo = true
99 this.video = new VideoEdit(Object.assign(res.video, {
100 commentsEnabled: videoUpdate.commentsEnabled,
101 downloadEnabled: videoUpdate.downloadEnabled,
102 privacy: { id: this.firstStepPrivacyId },
108 hydrateFormFromVideo(this.form, this.video, false)
112 this.loadingBar.useRef().complete()
113 this.isImportingVideo = false
114 this.firstStepError.emit()
116 let message = err.message
118 const error = err.body as PeerTubeProblemDocument
119 if (error?.code === ServerErrorCode.INCORRECT_FILES_IN_TORRENT) {
120 message = $localize`Torrents with only 1 file are supported.`
123 this.notifier.error(message)
128 updateSecondStep () {
129 if (this.checkForm() === false) {
133 this.video.patch(this.form.value)
135 this.isUpdatingVideo = true
138 this.updateVideoAndCaptions(this.video)
141 this.isUpdatingVideo = false
142 this.notifier.success($localize`Video to import updated.`)
144 this.router.navigate([ '/my-library', 'video-imports' ])
148 this.error = err.message