PeerTube/client/src/app/+videos/+video-edit/video-add-components/video-import-torrent.compon...

150 lines
4.5 KiB
TypeScript
Raw Normal View History

import { switchMap } from 'rxjs'
import { AfterViewInit, Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
2018-08-06 17:13:39 +02:00
import { Router } from '@angular/router'
import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core'
2020-06-23 14:10:17 +02:00
import { scrollToTop } from '@app/helpers'
2022-10-07 15:26:53 +02:00
import { FormReactiveService } from '@app/shared/shared-forms'
2023-08-28 10:55:04 +02:00
import { VideoCaptionService, VideoChapterService, VideoEdit, VideoImportService, VideoService } from '@app/shared/shared-main'
2020-06-23 14:10:17 +02:00
import { LoadingBarService } from '@ngx-loading-bar/core'
import { logger } from '@root-helpers/logger'
import { PeerTubeProblemDocument, ServerErrorCode, VideoUpdate } from '@peertube/peertube-models'
import { hydrateFormFromVideo } from '../shared/video-edit-utils'
import { VideoSend } from './video-send'
2018-08-06 17:13:39 +02:00
@Component({
selector: 'my-video-import-torrent',
templateUrl: './video-import-torrent.component.html',
styleUrls: [
'../shared/video-edit.component.scss',
'./video-import-torrent.component.scss',
'./video-send.scss'
2018-08-06 17:13:39 +02:00
]
})
export class VideoImportTorrentComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
2018-08-06 17:13:39 +02:00
@Output() firstStepDone = new EventEmitter<string>()
2018-11-16 10:05:25 +01:00
@Output() firstStepError = new EventEmitter<void>()
2020-02-07 10:00:34 +01:00
@ViewChild('torrentfileInput') torrentfileInput: ElementRef<HTMLInputElement>
2018-08-06 17:13:39 +02:00
magnetUri = ''
isImportingVideo = false
hasImportedVideo = false
isUpdatingVideo = false
video: VideoEdit
2018-11-16 10:05:25 +01:00
error: string
2018-08-06 17:13:39 +02:00
constructor (
2022-10-07 15:26:53 +02:00
protected formReactiveService: FormReactiveService,
2018-08-06 17:13:39 +02:00
protected loadingBar: LoadingBarService,
protected notifier: Notifier,
2018-08-06 17:13:39 +02:00
protected authService: AuthService,
protected serverService: ServerService,
protected videoService: VideoService,
protected videoCaptionService: VideoCaptionService,
2023-08-28 10:55:04 +02:00
protected videoChapterService: VideoChapterService,
2018-08-06 17:13:39 +02:00
private router: Router,
private videoImportService: VideoImportService,
private hooks: HooksService
2021-08-17 14:42:53 +02:00
) {
2018-08-06 17:13:39 +02:00
super()
}
ngOnInit () {
super.ngOnInit()
}
ngAfterViewInit () {
this.hooks.runAction('action:video-torrent-import.init', 'video-edit')
}
2018-08-06 17:13:39 +02:00
canDeactivate () {
return { canDeactivate: true }
}
isMagnetUrlValid () {
return !!this.magnetUri
}
2018-08-07 09:54:36 +02:00
fileChange () {
2018-10-18 14:35:31 +02:00
const torrentfile = this.torrentfileInput.nativeElement.files[0]
2018-08-07 09:54:36 +02:00
if (!torrentfile) return
this.importVideo(torrentfile)
}
setTorrentFile (files: FileList) {
this.torrentfileInput.nativeElement.files = files
this.fileChange()
}
2018-08-07 09:54:36 +02:00
importVideo (torrentfile?: Blob) {
2018-08-06 17:13:39 +02:00
this.isImportingVideo = true
const videoUpdate: VideoUpdate = {
privacy: this.highestPrivacy,
2018-08-06 17:13:39 +02:00
waitTranscoding: false,
channelId: this.firstStepChannelId
}
2020-08-06 16:14:58 +02:00
this.loadingBar.useRef().start()
2018-08-06 17:13:39 +02:00
2021-08-17 11:27:47 +02:00
this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate)
.pipe(switchMap(({ video }) => this.videoService.getVideo({ videoId: video.uuid })))
2021-08-17 11:27:47 +02:00
.subscribe({
next: video => {
2021-08-17 11:27:47 +02:00
this.loadingBar.useRef().complete()
this.firstStepDone.emit(video.name)
2021-08-17 11:27:47 +02:00
this.isImportingVideo = false
this.hasImportedVideo = true
this.video = new VideoEdit(video)
this.video.patch({ privacy: this.firstStepPrivacyId })
2021-08-17 11:27:47 +02:00
hydrateFormFromVideo(this.form, this.video, false)
},
error: err => {
this.loadingBar.useRef().complete()
this.isImportingVideo = false
this.firstStepError.emit()
let message = err.message
const error = err.body as PeerTubeProblemDocument
if (error?.code === ServerErrorCode.INCORRECT_FILES_IN_TORRENT) {
message = $localize`Torrents with only 1 file are supported.`
}
2021-08-17 11:27:47 +02:00
this.notifier.error(message)
}
})
2018-08-06 17:13:39 +02:00
}
2021-12-29 15:33:24 +01:00
async updateSecondStep () {
if (!await this.isFormValid()) return
2018-08-06 17:13:39 +02:00
this.video.patch(this.form.value)
2023-08-28 10:55:04 +02:00
this.chaptersEdit.patch(this.form.value)
2018-08-06 17:13:39 +02:00
this.isUpdatingVideo = true
// Update the video
2023-08-28 10:55:04 +02:00
this.updateVideoAndCaptionsAndChapters({ video: this.video, captions: this.videoCaptions, chapters: this.chaptersEdit })
.subscribe({
next: () => {
this.isUpdatingVideo = false
this.notifier.success($localize`Video to import updated.`)
this.router.navigate([ '/my-library', 'video-imports' ])
},
error: err => {
this.error = err.message
scrollToTop()
logger.error(err)
}
})
2018-08-06 17:13:39 +02:00
}
}