]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/video-add-components/video-import-torrent.component.ts
Add client action hooks for pubish pages
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / video-add-components / video-import-torrent.component.ts
CommitLineData
2e257e36 1import { AfterViewInit, Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
ce33919c 2import { Router } from '@angular/router'
2e257e36 3import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core'
67ed6552
C
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'
c6c0fa6c 9import { hydrateFormFromVideo } from '../shared/video-edit-utils'
66357162 10import { VideoSend } from './video-send'
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})
2e257e36 21export class VideoImportTorrentComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
ce33919c 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,
2e257e36
C
46 private videoImportService: VideoImportService,
47 private hooks: HooksService
66357162 48 ) {
ce33919c
C
49 super()
50 }
51
52 ngOnInit () {
53 super.ngOnInit()
54 }
55
2e257e36
C
56 ngAfterViewInit () {
57 this.hooks.runAction('action:video-torrent-import.init', 'video-edit')
58 }
59
ce33919c
C
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 = {
d487a997 84 privacy: VideoPrivacy.PRIVATE,
ce33919c
C
85 waitTranscoding: false,
86 commentsEnabled: true,
7f2cfe3a 87 downloadEnabled: true,
ce33919c
C
88 channelId: this.firstStepChannelId
89 }
90
a02b93ce 91 this.loadingBar.useRef().start()
ce33919c 92
990b6a0b 93 this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate).subscribe(
ce33919c 94 res => {
a02b93ce 95 this.loadingBar.useRef().complete()
ce33919c
C
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,
d487a997 103 privacy: { id: this.firstStepPrivacyId },
ce33919c
C
104 support: null,
105 thumbnailUrl: null,
106 previewUrl: null
107 }))
8cd7faaa 108
c6c0fa6c 109 hydrateFormFromVideo(this.form, this.video, false)
ce33919c
C
110 },
111
112 err => {
a02b93ce 113 this.loadingBar.useRef().complete()
ce33919c 114 this.isImportingVideo = false
7373507f 115 this.firstStepError.emit()
f8b2c1b4 116 this.notifier.error(err.message)
ce33919c
C
117 }
118 )
119 }
120
121 updateSecondStep () {
122 if (this.checkForm() === false) {
123 return
124 }
125
126 this.video.patch(this.form.value)
127
128 this.isUpdatingVideo = true
129
130 // Update the video
131 this.updateVideoAndCaptions(this.video)
132 .subscribe(
133 () => {
134 this.isUpdatingVideo = false
66357162 135 this.notifier.success($localize`Video to import updated.`)
ce33919c 136
17119e4a 137 this.router.navigate([ '/my-library', 'video-imports' ])
ce33919c
C
138 },
139
140 err => {
7373507f
C
141 this.error = err.message
142 scrollToTop()
ce33919c
C
143 console.error(err)
144 }
145 )
ce33919c
C
146 }
147}