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