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