1 import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { NotificationsService } from 'angular2-notifications'
4 import { VideoPrivacy, VideoUpdate } from '../../../../../../shared/models/videos'
5 import { AuthService, ServerService } from '../../../core'
6 import { VideoService } from '../../../shared/video/video.service'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { LoadingBarService } from '@ngx-loading-bar/core'
9 import { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send'
10 import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
11 import { VideoEdit } from '@app/shared/video/video-edit.model'
12 import { FormValidatorService } from '@app/shared'
13 import { VideoCaptionService } from '@app/shared/video-caption'
14 import { VideoImportService } from '@app/shared/video-import'
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'
24 export class VideoImportTorrentComponent extends VideoSend implements OnInit, CanComponentDeactivate {
25 @Output() firstStepDone = new EventEmitter<string>()
26 @ViewChild('torrentfileInput') torrentfileInput
31 isImportingVideo = false
32 hasImportedVideo = false
33 isUpdatingVideo = false
37 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
40 protected formValidatorService: FormValidatorService,
41 protected loadingBar: LoadingBarService,
42 protected notificationsService: NotificationsService,
43 protected authService: AuthService,
44 protected serverService: ServerService,
45 protected videoService: VideoService,
46 protected videoCaptionService: VideoCaptionService,
47 private router: Router,
48 private videoImportService: VideoImportService,
59 return { canDeactivate: true }
63 return !!this.magnetUri
67 const torrentfile = this.torrentfileInput.nativeElement.files[0] as File
68 if (!torrentfile) return
70 this.importVideo(torrentfile)
73 importVideo (torrentfile?: Blob) {
74 this.isImportingVideo = true
76 const videoUpdate: VideoUpdate = {
77 privacy: this.firstStepPrivacyId,
78 waitTranscoding: false,
79 commentsEnabled: true,
80 channelId: this.firstStepChannelId
83 this.loadingBar.start()
85 this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate).subscribe(
87 this.loadingBar.complete()
88 this.firstStepDone.emit(res.video.name)
89 this.isImportingVideo = false
90 this.hasImportedVideo = true
92 this.video = new VideoEdit(Object.assign(res.video, {
93 commentsEnabled: videoUpdate.commentsEnabled,
98 this.hydrateFormFromVideo()
102 this.loadingBar.complete()
103 this.isImportingVideo = false
104 this.notificationsService.error(this.i18n('Error'), err.message)
109 updateSecondStep () {
110 if (this.checkForm() === false) {
114 this.video.patch(this.form.value)
116 this.isUpdatingVideo = true
119 this.updateVideoAndCaptions(this.video)
122 this.isUpdatingVideo = false
123 this.notificationsService.success(this.i18n('Success'), this.i18n('Video to import updated.'))
125 this.router.navigate([ '/my-account', 'video-imports' ])
129 this.isUpdatingVideo = false
130 this.notificationsService.error(this.i18n('Error'), err.message)
137 private hydrateFormFromVideo () {
138 this.form.patchValue(this.video.toFormPatch())