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