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 { FormReactiveService } 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 { logger } from '@root-helpers/logger'
10 import { PeerTubeProblemDocument, ServerErrorCode, VideoUpdate } from '@shared/models'
11 import { hydrateFormFromVideo } from '../shared/video-edit-utils'
12 import { VideoSend } from './video-send'
15 selector: 'my-video-import-torrent',
16 templateUrl: './video-import-torrent.component.html',
18 '../shared/video-edit.component.scss',
19 './video-import-torrent.component.scss',
23 export class VideoImportTorrentComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
24 @Output() firstStepDone = new EventEmitter<string>()
25 @Output() firstStepError = new EventEmitter<void>()
26 @ViewChild('torrentfileInput') torrentfileInput: ElementRef<HTMLInputElement>
30 isImportingVideo = false
31 hasImportedVideo = false
32 isUpdatingVideo = false
38 protected formReactiveService: FormReactiveService,
39 protected loadingBar: LoadingBarService,
40 protected notifier: Notifier,
41 protected authService: AuthService,
42 protected serverService: ServerService,
43 protected videoService: VideoService,
44 protected videoCaptionService: VideoCaptionService,
45 private router: Router,
46 private videoImportService: VideoImportService,
47 private hooks: HooksService
57 this.hooks.runAction('action:video-torrent-import.init', 'video-edit')
61 return { canDeactivate: true }
65 return !!this.magnetUri
69 const torrentfile = this.torrentfileInput.nativeElement.files[0]
70 if (!torrentfile) return
72 this.importVideo(torrentfile)
75 setTorrentFile (files: FileList) {
76 this.torrentfileInput.nativeElement.files = files
80 importVideo (torrentfile?: Blob) {
81 this.isImportingVideo = true
83 const videoUpdate: VideoUpdate = {
84 privacy: this.highestPrivacy,
85 waitTranscoding: false,
86 channelId: this.firstStepChannelId
89 this.loadingBar.useRef().start()
91 this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate)
92 .pipe(switchMap(({ video }) => this.videoService.getVideo({ videoId: video.uuid })))
95 this.loadingBar.useRef().complete()
96 this.firstStepDone.emit(video.name)
97 this.isImportingVideo = false
98 this.hasImportedVideo = true
100 this.video = new VideoEdit(video)
101 this.video.patch({ privacy: this.firstStepPrivacyId })
103 hydrateFormFromVideo(this.form, this.video, false)
107 this.loadingBar.useRef().complete()
108 this.isImportingVideo = false
109 this.firstStepError.emit()
111 let message = err.message
113 const error = err.body as PeerTubeProblemDocument
114 if (error?.code === ServerErrorCode.INCORRECT_FILES_IN_TORRENT) {
115 message = $localize`Torrents with only 1 file are supported.`
118 this.notifier.error(message)
123 async updateSecondStep () {
124 if (!await this.isFormValid()) return
126 this.video.patch(this.form.value)
128 this.isUpdatingVideo = true
131 this.updateVideoAndCaptions(this.video)
134 this.isUpdatingVideo = false
135 this.notifier.success($localize`Video to import updated.`)
137 this.router.navigate([ '/my-library', 'video-imports' ])
141 this.error = err.message