]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-add-components/video-import-torrent.component.ts
57cf0e395639202b245cf41c849c4b2047602098
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-add-components / video-import-torrent.component.ts
1 import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { NotificationsService } from 'angular2-notifications'
4 import { VideoPrivacy, VideoUpdate } from '../../../../../../shared/models/videos'
5 import { AuthService, ServerService } from '../../../core'
6 import { VideoService } from '../../../shared/video/video.service'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { LoadingBarService } from '@ngx-loading-bar/core'
9 import { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send'
10 import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
11 import { VideoEdit } from '@app/shared/video/video-edit.model'
12 import { FormValidatorService } from '@app/shared'
13 import { VideoCaptionService } from '@app/shared/video-caption'
14 import { 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 })
24 export class VideoImportTorrentComponent extends VideoSend implements OnInit, CanComponentDeactivate {
25 @Output() firstStepDone = new EventEmitter<string>()
26 @ViewChild('torrentfileInput') torrentfileInput
27
28 videoFileName: string
29 magnetUri = ''
30
31 isImportingVideo = false
32 hasImportedVideo = false
33 isUpdatingVideo = false
34
35 video: VideoEdit
36
37 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
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
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) {
74 this.isImportingVideo = true
75
76 const videoUpdate: VideoUpdate = {
77 privacy: this.firstStepPrivacyId,
78 waitTranscoding: false,
79 commentsEnabled: true,
80 downloadingEnabled: 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 downloadingEnabled: videoUpdate.downloadingEnabled,
96 support: null,
97 thumbnailUrl: null,
98 previewUrl: null
99 }))
100
101 this.videoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies)
102
103 this.hydrateFormFromVideo()
104 },
105
106 err => {
107 this.loadingBar.complete()
108 this.isImportingVideo = false
109 this.notificationsService.error(this.i18n('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.notificationsService.success(this.i18n('Success'), this.i18n('Video to import updated.'))
129
130 this.router.navigate([ '/my-account', 'video-imports' ])
131 },
132
133 err => {
134 this.isUpdatingVideo = false
135 this.notificationsService.error(this.i18n('Error'), err.message)
136 console.error(err)
137 }
138 )
139
140 }
141
142 private hydrateFormFromVideo () {
143 this.form.patchValue(this.video.toFormPatch())
144 }
145 }