]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-add-components/video-import-torrent.component.ts
Add downloadingEnabled property to video model
[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,
156c50af 80 downloadingEnabled: true,
ce33919c
C
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,
156c50af 95 downloadingEnabled: videoUpdate.downloadingEnabled,
ce33919c
C
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
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}