]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-add-components/video-import-torrent.component.ts
Prepare Dislike/Flag/View fixes
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-add-components / video-import-torrent.component.ts
1 import { Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { VideoPrivacy, VideoUpdate } from '../../../../../../shared/models/videos'
4 import { AuthService, Notifier, ServerService } from '../../../core'
5 import { VideoService } from '../../../shared/video/video.service'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { LoadingBarService } from '@ngx-loading-bar/core'
8 import { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send'
9 import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
10 import { VideoEdit } from '@app/shared/video/video-edit.model'
11 import { FormValidatorService } from '@app/shared'
12 import { VideoCaptionService } from '@app/shared/video-caption'
13 import { VideoImportService } from '@app/shared/video-import'
14 import { scrollToTop } from '@app/shared/misc/utils'
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 })
24 export class VideoImportTorrentComponent extends VideoSend implements OnInit, CanComponentDeactivate {
25 @Output() firstStepDone = new EventEmitter<string>()
26 @Output() firstStepError = new EventEmitter<void>()
27 @ViewChild('torrentfileInput') torrentfileInput: ElementRef<HTMLInputElement>
28
29 magnetUri = ''
30
31 isImportingVideo = false
32 hasImportedVideo = false
33 isUpdatingVideo = false
34
35 video: VideoEdit
36 error: string
37
38 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
39
40 constructor (
41 protected formValidatorService: FormValidatorService,
42 protected loadingBar: LoadingBarService,
43 protected notifier: Notifier,
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
67 fileChange () {
68 const torrentfile = this.torrentfileInput.nativeElement.files[0]
69 if (!torrentfile) return
70
71 this.importVideo(torrentfile)
72 }
73
74 importVideo (torrentfile?: Blob) {
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
86 this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate).subscribe(
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 }))
99
100 this.videoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies)
101
102 this.hydrateFormFromVideo()
103 },
104
105 err => {
106 this.loadingBar.complete()
107 this.isImportingVideo = false
108 this.firstStepError.emit()
109 this.notifier.error(err.message)
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
128 this.notifier.success(this.i18n('Video to import updated.'))
129
130 this.router.navigate([ '/my-account', 'video-imports' ])
131 },
132
133 err => {
134 this.error = err.message
135 scrollToTop()
136 console.error(err)
137 }
138 )
139
140 }
141
142 private hydrateFormFromVideo () {
143 this.form.patchValue(this.video.toFormPatch())
144 }
145 }