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