]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-add-components/video-import-torrent.component.ts
provide specific engine boundaries for nodejs and yarn
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-add-components / video-import-torrent.component.ts
CommitLineData
c199c427 1import { Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
ce33919c 2import { Router } from '@angular/router'
ce33919c 3import { VideoPrivacy, VideoUpdate } from '../../../../../../shared/models/videos'
f8b2c1b4 4import { AuthService, Notifier, ServerService } from '../../../core'
ce33919c
C
5import { VideoService } from '../../../shared/video/video.service'
6import { I18n } from '@ngx-translate/i18n-polyfill'
7import { LoadingBarService } from '@ngx-loading-bar/core'
8import { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send'
9import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
10import { VideoEdit } from '@app/shared/video/video-edit.model'
11import { FormValidatorService } from '@app/shared'
12import { VideoCaptionService } from '@app/shared/video-caption'
13import { VideoImportService } from '@app/shared/video-import'
7373507f 14import { scrollToTop } from '@app/shared/misc/utils'
ce33919c
C
15
16@Component({
17 selector: 'my-video-import-torrent',
18 templateUrl: './video-import-torrent.component.html',
19 styleUrls: [
20 '../shared/video-edit.component.scss',
457bb213
C
21 './video-import-torrent.component.scss',
22 './video-send.scss'
ce33919c
C
23 ]
24})
25export class VideoImportTorrentComponent extends VideoSend implements OnInit, CanComponentDeactivate {
26 @Output() firstStepDone = new EventEmitter<string>()
7373507f 27 @Output() firstStepError = new EventEmitter<void>()
2f5d2ec5 28 @ViewChild('torrentfileInput') torrentfileInput: ElementRef<HTMLInputElement>
ce33919c 29
ce33919c
C
30 magnetUri = ''
31
32 isImportingVideo = false
33 hasImportedVideo = false
34 isUpdatingVideo = false
35
36 video: VideoEdit
7373507f 37 error: string
ce33919c 38
990b6a0b 39 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
ce33919c
C
40
41 constructor (
42 protected formValidatorService: FormValidatorService,
43 protected loadingBar: LoadingBarService,
f8b2c1b4 44 protected notifier: Notifier,
ce33919c
C
45 protected authService: AuthService,
46 protected serverService: ServerService,
47 protected videoService: VideoService,
48 protected videoCaptionService: VideoCaptionService,
49 private router: Router,
50 private videoImportService: VideoImportService,
51 private i18n: I18n
52 ) {
53 super()
54 }
55
56 ngOnInit () {
57 super.ngOnInit()
58 }
59
60 canDeactivate () {
61 return { canDeactivate: true }
62 }
63
64 isMagnetUrlValid () {
65 return !!this.magnetUri
66 }
67
990b6a0b 68 fileChange () {
c199c427 69 const torrentfile = this.torrentfileInput.nativeElement.files[0]
990b6a0b
C
70 if (!torrentfile) return
71
72 this.importVideo(torrentfile)
73 }
74
c9ff8a08
RK
75 setTorrentFile (files: FileList) {
76 this.torrentfileInput.nativeElement.files = files
77 this.fileChange()
78 }
79
990b6a0b 80 importVideo (torrentfile?: Blob) {
ce33919c
C
81 this.isImportingVideo = true
82
83 const videoUpdate: VideoUpdate = {
84 privacy: this.firstStepPrivacyId,
85 waitTranscoding: false,
86 commentsEnabled: true,
7f2cfe3a 87 downloadEnabled: true,
ce33919c
C
88 channelId: this.firstStepChannelId
89 }
90
91 this.loadingBar.start()
92
990b6a0b 93 this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate).subscribe(
ce33919c
C
94 res => {
95 this.loadingBar.complete()
96 this.firstStepDone.emit(res.video.name)
97 this.isImportingVideo = false
98 this.hasImportedVideo = true
99
100 this.video = new VideoEdit(Object.assign(res.video, {
101 commentsEnabled: videoUpdate.commentsEnabled,
7f2cfe3a 102 downloadEnabled: videoUpdate.downloadEnabled,
ce33919c
C
103 support: null,
104 thumbnailUrl: null,
105 previewUrl: null
106 }))
8cd7faaa 107
ce33919c
C
108 this.hydrateFormFromVideo()
109 },
110
111 err => {
112 this.loadingBar.complete()
113 this.isImportingVideo = false
7373507f 114 this.firstStepError.emit()
f8b2c1b4 115 this.notifier.error(err.message)
ce33919c
C
116 }
117 )
118 }
119
120 updateSecondStep () {
121 if (this.checkForm() === false) {
122 return
123 }
124
125 this.video.patch(this.form.value)
126
127 this.isUpdatingVideo = true
128
129 // Update the video
130 this.updateVideoAndCaptions(this.video)
131 .subscribe(
132 () => {
133 this.isUpdatingVideo = false
f8b2c1b4 134 this.notifier.success(this.i18n('Video to import updated.'))
ce33919c
C
135
136 this.router.navigate([ '/my-account', 'video-imports' ])
137 },
138
139 err => {
7373507f
C
140 this.error = err.message
141 scrollToTop()
ce33919c
C
142 console.error(err)
143 }
144 )
145
146 }
147
148 private hydrateFormFromVideo () {
149 this.form.patchValue(this.video.toFormPatch())
150 }
151}