1 import { Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { AuthService, CanComponentDeactivate, 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 { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send'
8 import { LoadingBarService } from '@ngx-loading-bar/core'
9 import { I18n } from '@ngx-translate/i18n-polyfill'
10 import { VideoPrivacy, VideoUpdate } from '@shared/models'
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, 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,
57 return { canDeactivate: true }
61 return !!this.magnetUri
65 const torrentfile = this.torrentfileInput.nativeElement.files[0]
66 if (!torrentfile) return
68 this.importVideo(torrentfile)
71 setTorrentFile (files: FileList) {
72 this.torrentfileInput.nativeElement.files = files
76 importVideo (torrentfile?: Blob) {
77 this.isImportingVideo = true
79 const videoUpdate: VideoUpdate = {
80 privacy: this.firstStepPrivacyId,
81 waitTranscoding: false,
82 commentsEnabled: true,
83 downloadEnabled: true,
84 channelId: this.firstStepChannelId
87 this.loadingBar.start()
89 this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate).subscribe(
91 this.loadingBar.complete()
92 this.firstStepDone.emit(res.video.name)
93 this.isImportingVideo = false
94 this.hasImportedVideo = true
96 this.video = new VideoEdit(Object.assign(res.video, {
97 commentsEnabled: videoUpdate.commentsEnabled,
98 downloadEnabled: videoUpdate.downloadEnabled,
104 this.hydrateFormFromVideo()
108 this.loadingBar.complete()
109 this.isImportingVideo = false
110 this.firstStepError.emit()
111 this.notifier.error(err.message)
116 updateSecondStep () {
117 if (this.checkForm() === false) {
121 this.video.patch(this.form.value)
123 this.isUpdatingVideo = true
126 this.updateVideoAndCaptions(this.video)
129 this.isUpdatingVideo = false
130 this.notifier.success(this.i18n('Video to import updated.'))
132 this.router.navigate([ '/my-account', 'video-imports' ])
136 this.error = err.message
144 private hydrateFormFromVideo () {
145 this.form.patchValue(this.video.toFormPatch())