]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-add-components/video-import-torrent.component.ts
Making password change erroring more friendly
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-add-components / video-import-torrent.component.ts
CommitLineData
990b6a0b 1import { Component, 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'
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>()
990b6a0b 26 @ViewChild('torrentfileInput') torrentfileInput
ce33919c
C
27
28 videoFileName: string
29 magnetUri = ''
30
31 isImportingVideo = false
32 hasImportedVideo = false
33 isUpdatingVideo = false
34
35 video: VideoEdit
36
990b6a0b 37 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
ce33919c
C
38
39 constructor (
40 protected formValidatorService: FormValidatorService,
41 protected loadingBar: LoadingBarService,
42 protected notificationsService: NotificationsService,
43 protected authService: AuthService,
44 protected serverService: ServerService,
45 protected videoService: VideoService,
46 protected videoCaptionService: VideoCaptionService,
47 private router: Router,
48 private videoImportService: VideoImportService,
49 private i18n: I18n
50 ) {
51 super()
52 }
53
54 ngOnInit () {
55 super.ngOnInit()
56 }
57
58 canDeactivate () {
59 return { canDeactivate: true }
60 }
61
62 isMagnetUrlValid () {
63 return !!this.magnetUri
64 }
65
990b6a0b
C
66 fileChange () {
67 const torrentfile = this.torrentfileInput.nativeElement.files[0] as File
68 if (!torrentfile) return
69
70 this.importVideo(torrentfile)
71 }
72
73 importVideo (torrentfile?: Blob) {
ce33919c
C
74 this.isImportingVideo = true
75
76 const videoUpdate: VideoUpdate = {
77 privacy: this.firstStepPrivacyId,
78 waitTranscoding: false,
79 commentsEnabled: true,
80 channelId: this.firstStepChannelId
81 }
82
83 this.loadingBar.start()
84
990b6a0b 85 this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate).subscribe(
ce33919c
C
86 res => {
87 this.loadingBar.complete()
88 this.firstStepDone.emit(res.video.name)
89 this.isImportingVideo = false
90 this.hasImportedVideo = true
91
92 this.video = new VideoEdit(Object.assign(res.video, {
93 commentsEnabled: videoUpdate.commentsEnabled,
94 support: null,
95 thumbnailUrl: null,
96 previewUrl: null
97 }))
98 this.hydrateFormFromVideo()
99 },
100
101 err => {
102 this.loadingBar.complete()
103 this.isImportingVideo = false
104 this.notificationsService.error(this.i18n('Error'), err.message)
105 }
106 )
107 }
108
109 updateSecondStep () {
110 if (this.checkForm() === false) {
111 return
112 }
113
114 this.video.patch(this.form.value)
115
116 this.isUpdatingVideo = true
117
118 // Update the video
119 this.updateVideoAndCaptions(this.video)
120 .subscribe(
121 () => {
122 this.isUpdatingVideo = false
123 this.notificationsService.success(this.i18n('Success'), this.i18n('Video to import updated.'))
124
125 this.router.navigate([ '/my-account', 'video-imports' ])
126 },
127
128 err => {
129 this.isUpdatingVideo = false
130 this.notificationsService.error(this.i18n('Error'), err.message)
131 console.error(err)
132 }
133 )
134
135 }
136
137 private hydrateFormFromVideo () {
138 this.form.patchValue(this.video.toFormPatch())
139 }
140}