]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/video-add-components/video-import-torrent.component.ts
Merge branch 'constant-registry' into develop
[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'
e030bfb5 8import { PeerTubeProblemDocument, ServerErrorCode, 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
ce33919c
C
35 constructor (
36 protected formValidatorService: FormValidatorService,
37 protected loadingBar: LoadingBarService,
f8b2c1b4 38 protected notifier: Notifier,
ce33919c
C
39 protected authService: AuthService,
40 protected serverService: ServerService,
41 protected videoService: VideoService,
42 protected videoCaptionService: VideoCaptionService,
43 private router: Router,
2e257e36
C
44 private videoImportService: VideoImportService,
45 private hooks: HooksService
66357162 46 ) {
ce33919c
C
47 super()
48 }
49
50 ngOnInit () {
51 super.ngOnInit()
52 }
53
2e257e36
C
54 ngAfterViewInit () {
55 this.hooks.runAction('action:video-torrent-import.init', 'video-edit')
56 }
57
ce33919c
C
58 canDeactivate () {
59 return { canDeactivate: true }
60 }
61
62 isMagnetUrlValid () {
63 return !!this.magnetUri
64 }
65
990b6a0b 66 fileChange () {
c199c427 67 const torrentfile = this.torrentfileInput.nativeElement.files[0]
990b6a0b
C
68 if (!torrentfile) return
69
70 this.importVideo(torrentfile)
71 }
72
c9ff8a08
RK
73 setTorrentFile (files: FileList) {
74 this.torrentfileInput.nativeElement.files = files
75 this.fileChange()
76 }
77
990b6a0b 78 importVideo (torrentfile?: Blob) {
ce33919c
C
79 this.isImportingVideo = true
80
81 const videoUpdate: VideoUpdate = {
a3f45a2a 82 privacy: this.highestPrivacy,
ce33919c
C
83 waitTranscoding: false,
84 commentsEnabled: true,
7f2cfe3a 85 downloadEnabled: true,
ce33919c
C
86 channelId: this.firstStepChannelId
87 }
88
a02b93ce 89 this.loadingBar.useRef().start()
ce33919c 90
990b6a0b 91 this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate).subscribe(
ce33919c 92 res => {
a02b93ce 93 this.loadingBar.useRef().complete()
ce33919c
C
94 this.firstStepDone.emit(res.video.name)
95 this.isImportingVideo = false
96 this.hasImportedVideo = true
97
98 this.video = new VideoEdit(Object.assign(res.video, {
99 commentsEnabled: videoUpdate.commentsEnabled,
7f2cfe3a 100 downloadEnabled: videoUpdate.downloadEnabled,
d487a997 101 privacy: { id: this.firstStepPrivacyId },
ce33919c
C
102 support: null,
103 thumbnailUrl: null,
104 previewUrl: null
105 }))
8cd7faaa 106
c6c0fa6c 107 hydrateFormFromVideo(this.form, this.video, false)
ce33919c
C
108 },
109
110 err => {
a02b93ce 111 this.loadingBar.useRef().complete()
ce33919c 112 this.isImportingVideo = false
7373507f 113 this.firstStepError.emit()
32985a0a
C
114
115 let message = err.message
e030bfb5
C
116
117 const error = err.body as PeerTubeProblemDocument
118 if (error?.code === ServerErrorCode.INCORRECT_FILES_IN_TORRENT) {
32985a0a
C
119 message = $localize`Torrents with only 1 file are supported.`
120 }
121
122 this.notifier.error(message)
ce33919c
C
123 }
124 )
125 }
126
127 updateSecondStep () {
128 if (this.checkForm() === false) {
129 return
130 }
131
132 this.video.patch(this.form.value)
133
134 this.isUpdatingVideo = true
135
136 // Update the video
137 this.updateVideoAndCaptions(this.video)
138 .subscribe(
139 () => {
140 this.isUpdatingVideo = false
66357162 141 this.notifier.success($localize`Video to import updated.`)
ce33919c 142
17119e4a 143 this.router.navigate([ '/my-library', 'video-imports' ])
ce33919c
C
144 },
145
146 err => {
7373507f
C
147 this.error = err.message
148 scrollToTop()
ce33919c
C
149 console.error(err)
150 }
151 )
ce33919c
C
152 }
153}