]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/video-add-components/video-import-torrent.component.ts
Import in private, and then set the chosen privacy
[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'
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})
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,
66357162
C
46 private videoImportService: VideoImportService
47 ) {
ce33919c
C
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
990b6a0b 63 fileChange () {
c199c427 64 const torrentfile = this.torrentfileInput.nativeElement.files[0]
990b6a0b
C
65 if (!torrentfile) return
66
67 this.importVideo(torrentfile)
68 }
69
c9ff8a08
RK
70 setTorrentFile (files: FileList) {
71 this.torrentfileInput.nativeElement.files = files
72 this.fileChange()
73 }
74
990b6a0b 75 importVideo (torrentfile?: Blob) {
ce33919c
C
76 this.isImportingVideo = true
77
78 const videoUpdate: VideoUpdate = {
d487a997 79 privacy: VideoPrivacy.PRIVATE,
ce33919c
C
80 waitTranscoding: false,
81 commentsEnabled: true,
7f2cfe3a 82 downloadEnabled: true,
ce33919c
C
83 channelId: this.firstStepChannelId
84 }
85
a02b93ce 86 this.loadingBar.useRef().start()
ce33919c 87
990b6a0b 88 this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate).subscribe(
ce33919c 89 res => {
a02b93ce 90 this.loadingBar.useRef().complete()
ce33919c
C
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,
7f2cfe3a 97 downloadEnabled: videoUpdate.downloadEnabled,
d487a997 98 privacy: { id: this.firstStepPrivacyId },
ce33919c
C
99 support: null,
100 thumbnailUrl: null,
101 previewUrl: null
102 }))
8cd7faaa 103
c6c0fa6c 104 hydrateFormFromVideo(this.form, this.video, false)
ce33919c
C
105 },
106
107 err => {
a02b93ce 108 this.loadingBar.useRef().complete()
ce33919c 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
66357162 130 this.notifier.success($localize`Video to import updated.`)
ce33919c 131
17119e4a 132 this.router.navigate([ '/my-library', 'video-imports' ])
ce33919c
C
133 },
134
135 err => {
7373507f
C
136 this.error = err.message
137 scrollToTop()
ce33919c
C
138 console.error(err)
139 }
140 )
ce33919c
C
141 }
142}