1 import { Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { VideoPrivacy, VideoUpdate } from '../../../../../../shared/models/videos'
4 import { AuthService, Notifier, ServerService } from '../../../core'
5 import { VideoService } from '../../../shared/video/video.service'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { LoadingBarService } from '@ngx-loading-bar/core'
8 import { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send'
9 import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
10 import { VideoEdit } from '@app/shared/video/video-edit.model'
11 import { FormValidatorService } from '@app/shared'
12 import { VideoCaptionService } from '@app/shared/video-caption'
13 import { VideoImportService } from '@app/shared/video-import'
14 import { scrollToTop } from '@app/shared/misc/utils'
17 selector: 'my-video-import-torrent',
18 templateUrl: './video-import-torrent.component.html',
20 '../shared/video-edit.component.scss',
21 './video-import-torrent.component.scss',
25 export class VideoImportTorrentComponent extends VideoSend implements OnInit, CanComponentDeactivate {
26 @Output() firstStepDone = new EventEmitter<string>()
27 @Output() firstStepError = new EventEmitter<void>()
28 @ViewChild('torrentfileInput', { static: false }) torrentfileInput: ElementRef<HTMLInputElement>
32 isImportingVideo = false
33 hasImportedVideo = false
34 isUpdatingVideo = false
39 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
42 protected formValidatorService: FormValidatorService,
43 protected loadingBar: LoadingBarService,
44 protected notifier: Notifier,
45 protected authService: AuthService,
46 protected serverService: ServerService,
47 protected videoService: VideoService,
48 protected videoCaptionService: VideoCaptionService,
49 private router: Router,
50 private videoImportService: VideoImportService,
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 importVideo (torrentfile?: Blob) {
76 this.isImportingVideo = true
78 const videoUpdate: VideoUpdate = {
79 privacy: this.firstStepPrivacyId,
80 waitTranscoding: false,
81 commentsEnabled: true,
82 downloadEnabled: true,
83 channelId: this.firstStepChannelId
86 this.loadingBar.start()
88 this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate).subscribe(
90 this.loadingBar.complete()
91 this.firstStepDone.emit(res.video.name)
92 this.isImportingVideo = false
93 this.hasImportedVideo = true
95 this.video = new VideoEdit(Object.assign(res.video, {
96 commentsEnabled: videoUpdate.commentsEnabled,
97 downloadEnabled: videoUpdate.downloadEnabled,
103 this.hydrateFormFromVideo()
107 this.loadingBar.complete()
108 this.isImportingVideo = false
109 this.firstStepError.emit()
110 this.notifier.error(err.message)
115 updateSecondStep () {
116 if (this.checkForm() === false) {
120 this.video.patch(this.form.value)
122 this.isUpdatingVideo = true
125 this.updateVideoAndCaptions(this.video)
128 this.isUpdatingVideo = false
129 this.notifier.success(this.i18n('Video to import updated.'))
131 this.router.navigate([ '/my-account', 'video-imports' ])
135 this.error = err.message
143 private hydrateFormFromVideo () {
144 this.form.patchValue(this.video.toFormPatch())