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 { VideoPrivacy, 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
35 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
38 protected formValidatorService: FormValidatorService,
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: VideoPrivacy.PRIVATE,
85 waitTranscoding: false,
86 commentsEnabled: true,
87 downloadEnabled: true,
88 channelId: this.firstStepChannelId
91 this.loadingBar.useRef().start()
93 this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate).subscribe(
95 this.loadingBar.useRef().complete()
96 this.firstStepDone.emit(res.video.name)
97 this.isImportingVideo = false
98 this.hasImportedVideo = true
100 this.video = new VideoEdit(Object.assign(res.video, {
101 commentsEnabled: videoUpdate.commentsEnabled,
102 downloadEnabled: videoUpdate.downloadEnabled,
103 privacy: { id: this.firstStepPrivacyId },
109 hydrateFormFromVideo(this.form, this.video, false)
113 this.loadingBar.useRef().complete()
114 this.isImportingVideo = false
115 this.firstStepError.emit()
116 this.notifier.error(err.message)
121 updateSecondStep () {
122 if (this.checkForm() === false) {
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