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') 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 setTorrentFile (files: FileList) {
76 this.torrentfileInput.nativeElement.files = files
80 importVideo (torrentfile?: Blob) {
81 this.isImportingVideo = true
83 const videoUpdate: VideoUpdate = {
84 privacy: this.firstStepPrivacyId,
85 waitTranscoding: false,
86 commentsEnabled: true,
87 downloadEnabled: true,
88 channelId: this.firstStepChannelId
91 this.loadingBar.start()
93 this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate).subscribe(
95 this.loadingBar.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,
108 this.hydrateFormFromVideo()
112 this.loadingBar.complete()
113 this.isImportingVideo = false
114 this.firstStepError.emit()
115 this.notifier.error(err.message)
120 updateSecondStep () {
121 if (this.checkForm() === false) {
125 this.video.patch(this.form.value)
127 this.isUpdatingVideo = true
130 this.updateVideoAndCaptions(this.video)
133 this.isUpdatingVideo = false
134 this.notifier.success(this.i18n('Video to import updated.'))
136 this.router.navigate([ '/my-account', 'video-imports' ])
140 this.error = err.message
148 private hydrateFormFromVideo () {
149 this.form.patchValue(this.video.toFormPatch())