]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-edit/video-add-components/video-import-torrent.component.ts
Fix comments/download attributes on import
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / video-add-components / video-import-torrent.component.ts
1 import { switchMap } from 'rxjs'
2 import { AfterViewInit, Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
3 import { Router } from '@angular/router'
4 import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core'
5 import { scrollToTop } from '@app/helpers'
6 import { FormValidatorService } from '@app/shared/shared-forms'
7 import { VideoCaptionService, VideoEdit, VideoImportService, VideoService } from '@app/shared/shared-main'
8 import { LoadingBarService } from '@ngx-loading-bar/core'
9 import { PeerTubeProblemDocument, ServerErrorCode, VideoUpdate } from '@shared/models'
10 import { hydrateFormFromVideo } from '../shared/video-edit-utils'
11 import { VideoSend } from './video-send'
12
13 @Component({
14 selector: 'my-video-import-torrent',
15 templateUrl: './video-import-torrent.component.html',
16 styleUrls: [
17 '../shared/video-edit.component.scss',
18 './video-import-torrent.component.scss',
19 './video-send.scss'
20 ]
21 })
22 export class VideoImportTorrentComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
23 @Output() firstStepDone = new EventEmitter<string>()
24 @Output() firstStepError = new EventEmitter<void>()
25 @ViewChild('torrentfileInput') torrentfileInput: ElementRef<HTMLInputElement>
26
27 magnetUri = ''
28
29 isImportingVideo = false
30 hasImportedVideo = false
31 isUpdatingVideo = false
32
33 video: VideoEdit
34 error: string
35
36 constructor (
37 protected formValidatorService: FormValidatorService,
38 protected loadingBar: LoadingBarService,
39 protected notifier: Notifier,
40 protected authService: AuthService,
41 protected serverService: ServerService,
42 protected videoService: VideoService,
43 protected videoCaptionService: VideoCaptionService,
44 private router: Router,
45 private videoImportService: VideoImportService,
46 private hooks: HooksService
47 ) {
48 super()
49 }
50
51 ngOnInit () {
52 super.ngOnInit()
53 }
54
55 ngAfterViewInit () {
56 this.hooks.runAction('action:video-torrent-import.init', 'video-edit')
57 }
58
59 canDeactivate () {
60 return { canDeactivate: true }
61 }
62
63 isMagnetUrlValid () {
64 return !!this.magnetUri
65 }
66
67 fileChange () {
68 const torrentfile = this.torrentfileInput.nativeElement.files[0]
69 if (!torrentfile) return
70
71 this.importVideo(torrentfile)
72 }
73
74 setTorrentFile (files: FileList) {
75 this.torrentfileInput.nativeElement.files = files
76 this.fileChange()
77 }
78
79 importVideo (torrentfile?: Blob) {
80 this.isImportingVideo = true
81
82 const videoUpdate: VideoUpdate = {
83 privacy: this.highestPrivacy,
84 waitTranscoding: false,
85 channelId: this.firstStepChannelId
86 }
87
88 this.loadingBar.useRef().start()
89
90 this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate)
91 .pipe(switchMap(({ video }) => this.videoService.getVideo({ videoId: video.uuid })))
92 .subscribe({
93 next: video => {
94 this.loadingBar.useRef().complete()
95 this.firstStepDone.emit(video.name)
96 this.isImportingVideo = false
97 this.hasImportedVideo = true
98
99 this.video = new VideoEdit(video)
100 this.video.patch({ privacy: this.firstStepPrivacyId })
101
102 hydrateFormFromVideo(this.form, this.video, false)
103 },
104
105 error: err => {
106 this.loadingBar.useRef().complete()
107 this.isImportingVideo = false
108 this.firstStepError.emit()
109
110 let message = err.message
111
112 const error = err.body as PeerTubeProblemDocument
113 if (error?.code === ServerErrorCode.INCORRECT_FILES_IN_TORRENT) {
114 message = $localize`Torrents with only 1 file are supported.`
115 }
116
117 this.notifier.error(message)
118 }
119 })
120 }
121
122 async updateSecondStep () {
123 if (!await this.isFormValid()) return
124
125 this.video.patch(this.form.value)
126
127 this.isUpdatingVideo = true
128
129 // Update the video
130 this.updateVideoAndCaptions(this.video)
131 .subscribe({
132 next: () => {
133 this.isUpdatingVideo = false
134 this.notifier.success($localize`Video to import updated.`)
135
136 this.router.navigate([ '/my-library', 'video-imports' ])
137 },
138
139 error: err => {
140 this.error = err.message
141 scrollToTop()
142 console.error(err)
143 }
144 })
145 }
146 }