]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-add-components/video-import-torrent.component.ts
Fix deleting not found remote actors
[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'
ce33919c 3import { VideoPrivacy, VideoUpdate } from '../../../../../../shared/models/videos'
f8b2c1b4 4import { AuthService, Notifier, ServerService } from '../../../core'
ce33919c
C
5import { VideoService } from '../../../shared/video/video.service'
6import { I18n } from '@ngx-translate/i18n-polyfill'
7import { LoadingBarService } from '@ngx-loading-bar/core'
8import { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send'
9import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
10import { VideoEdit } from '@app/shared/video/video-edit.model'
11import { FormValidatorService } from '@app/shared'
12import { VideoCaptionService } from '@app/shared/video-caption'
13import { VideoImportService } from '@app/shared/video-import'
7373507f 14import { scrollToTop } from '@app/shared/misc/utils'
ce33919c
C
15
16@Component({
17 selector: 'my-video-import-torrent',
18 templateUrl: './video-import-torrent.component.html',
19 styleUrls: [
20 '../shared/video-edit.component.scss',
21 './video-import-torrent.component.scss'
22 ]
23})
24export class VideoImportTorrentComponent extends VideoSend implements OnInit, CanComponentDeactivate {
25 @Output() firstStepDone = new EventEmitter<string>()
7373507f 26 @Output() firstStepError = new EventEmitter<void>()
c199c427 27 @ViewChild('torrentfileInput') torrentfileInput: ElementRef<HTMLInputElement>
ce33919c 28
ce33919c
C
29 magnetUri = ''
30
31 isImportingVideo = false
32 hasImportedVideo = false
33 isUpdatingVideo = false
34
35 video: VideoEdit
7373507f 36 error: string
ce33919c 37
990b6a0b 38 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
ce33919c
C
39
40 constructor (
41 protected formValidatorService: FormValidatorService,
42 protected loadingBar: LoadingBarService,
f8b2c1b4 43 protected notifier: Notifier,
ce33919c
C
44 protected authService: AuthService,
45 protected serverService: ServerService,
46 protected videoService: VideoService,
47 protected videoCaptionService: VideoCaptionService,
48 private router: Router,
49 private videoImportService: VideoImportService,
50 private i18n: I18n
51 ) {
52 super()
53 }
54
55 ngOnInit () {
56 super.ngOnInit()
57 }
58
59 canDeactivate () {
60 return { canDeactivate: true }
61 }
62
63 isMagnetUrlValid () {
64 return !!this.magnetUri
65 }
66
990b6a0b 67 fileChange () {
c199c427 68 const torrentfile = this.torrentfileInput.nativeElement.files[0]
990b6a0b
C
69 if (!torrentfile) return
70
71 this.importVideo(torrentfile)
72 }
73
74 importVideo (torrentfile?: Blob) {
ce33919c
C
75 this.isImportingVideo = true
76
77 const videoUpdate: VideoUpdate = {
78 privacy: this.firstStepPrivacyId,
79 waitTranscoding: false,
80 commentsEnabled: true,
81 channelId: this.firstStepChannelId
82 }
83
84 this.loadingBar.start()
85
990b6a0b 86 this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate).subscribe(
ce33919c
C
87 res => {
88 this.loadingBar.complete()
89 this.firstStepDone.emit(res.video.name)
90 this.isImportingVideo = false
91 this.hasImportedVideo = true
92
93 this.video = new VideoEdit(Object.assign(res.video, {
94 commentsEnabled: videoUpdate.commentsEnabled,
95 support: null,
96 thumbnailUrl: null,
97 previewUrl: null
98 }))
8cd7faaa
C
99
100 this.videoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies)
101
ce33919c
C
102 this.hydrateFormFromVideo()
103 },
104
105 err => {
106 this.loadingBar.complete()
107 this.isImportingVideo = false
7373507f 108 this.firstStepError.emit()
f8b2c1b4 109 this.notifier.error(err.message)
ce33919c
C
110 }
111 )
112 }
113
114 updateSecondStep () {
115 if (this.checkForm() === false) {
116 return
117 }
118
119 this.video.patch(this.form.value)
120
121 this.isUpdatingVideo = true
122
123 // Update the video
124 this.updateVideoAndCaptions(this.video)
125 .subscribe(
126 () => {
127 this.isUpdatingVideo = false
f8b2c1b4 128 this.notifier.success(this.i18n('Video to import updated.'))
ce33919c
C
129
130 this.router.navigate([ '/my-account', 'video-imports' ])
131 },
132
133 err => {
7373507f
C
134 this.error = err.message
135 scrollToTop()
ce33919c
C
136 console.error(err)
137 }
138 )
139
140 }
141
142 private hydrateFormFromVideo () {
143 this.form.patchValue(this.video.toFormPatch())
144 }
145}