]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-add.component.ts
Add ability for uploaders to schedule video update
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-add.component.ts
CommitLineData
202f6b6c 1import { HttpEventType, HttpResponse } from '@angular/common/http'
f6a043df 2import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
df98563e 3import { Router } from '@angular/router'
ce5496d6 4import { UserService } from '@app/shared'
f6a043df 5import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
68e24d72 6import { LoadingBarService } from '@ngx-loading-bar/core'
df98563e 7import { NotificationsService } from 'angular2-notifications'
ce5496d6 8import { BytesPipe } from 'ngx-pipes'
db400f44 9import { Subscription } from 'rxjs'
cadb46d8 10import { VideoPrivacy } from '../../../../../shared/models/videos'
202f6b6c 11import { AuthService, ServerService } from '../../core'
27e1a06c 12import { FormReactive } from '../../shared'
15a7387d 13import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
27e1a06c 14import { VideoEdit } from '../../shared/video/video-edit.model'
63c4db6d 15import { VideoService } from '../../shared/video/video.service'
b1d40cff 16import { I18n } from '@ngx-translate/i18n-polyfill'
d18d6478 17import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
1553e15d 18
dc8bc31b
C
19@Component({
20 selector: 'my-videos-add',
27e1a06c
C
21 templateUrl: './video-add.component.html',
22 styleUrls: [
23 './shared/video-edit.component.scss',
24 './video-add.component.scss'
25 ]
dc8bc31b 26})
f6a043df 27export class VideoAddComponent extends FormReactive implements OnInit, OnDestroy, CanComponentDeactivate {
bfb3a98f
C
28 @ViewChild('videofileInput') videofileInput
29
27e1a06c 30 isUploadingVideo = false
68e24d72 31 isUpdatingVideo = false
baeefe22 32 videoUploaded = false
f6a043df 33 videoUploadObservable: Subscription = null
c182778e 34 videoUploadPercents = 0
a4b8a4dd
C
35 videoUploadedIds = {
36 id: 0,
37 uuid: ''
38 }
2f315e2f 39 videoFileName: string
3758da94 40
74af5145 41 userVideoChannels: { id: number, label: string, support: string }[] = []
ce5496d6 42 userVideoQuotaUsed = 0
27e1a06c 43 videoPrivacies = []
cadb46d8
C
44 firstStepPrivacyId = 0
45 firstStepChannelId = 0
dc8bc31b 46
df98563e 47 constructor (
d18d6478 48 protected formValidatorService: FormValidatorService,
7ddd02c9 49 private router: Router,
6e07c3de 50 private notificationsService: NotificationsService,
bcd9f81e 51 private authService: AuthService,
ce5496d6 52 private userService: UserService,
db7af09b 53 private serverService: ServerService,
68e24d72 54 private videoService: VideoService,
b1d40cff
C
55 private loadingBar: LoadingBarService,
56 private i18n: I18n
4b2f33f3 57 ) {
df98563e 58 super()
4b2f33f3 59 }
dc8bc31b 60
108af661
C
61 get videoExtensions () {
62 return this.serverService.getConfig().video.file.extensions.join(',')
63 }
64
df98563e 65 ngOnInit () {
d18d6478 66 this.buildForm({})
2de96f4d 67
15a7387d
C
68 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
69 .then(() => this.firstStepChannelId = this.userVideoChannels[0].id)
70
ce5496d6
C
71 this.userService.getMyVideoQuotaUsed()
72 .subscribe(data => this.userVideoQuotaUsed = data.videoQuotaUsed)
73
c182778e 74 this.serverService.videoPrivaciesLoaded
baeefe22
C
75 .subscribe(
76 () => {
77 this.videoPrivacies = this.serverService.getVideoPrivacies()
cadb46d8
C
78
79 // Public by default
80 this.firstStepPrivacyId = VideoPrivacy.PUBLIC
baeefe22 81 })
e822fdae
C
82 }
83
f6a043df
C
84 ngOnDestroy () {
85 if (this.videoUploadObservable) {
86 this.videoUploadObservable.unsubscribe()
87 }
88 }
89
529479f9 90 canDeactivate () {
f6a043df
C
91 let text = ''
92
93 if (this.videoUploaded === true) {
b1d40cff
C
94 // FIXME: cannot concatenate strings inside i18n service :/
95 text = this.i18n('Your video was uploaded in your account and is private.') +
96 this.i18n('But associated data (tags, description...) will be lost, are you sure you want to leave this page?')
f6a043df 97 } else {
b1d40cff 98 text = this.i18n('Your video is not uploaded yet, are you sure you want to leave this page?')
f6a043df
C
99 }
100
101 return {
102 canDeactivate: !this.isUploadingVideo,
103 text
104 }
529479f9
DG
105 }
106
baeefe22
C
107 fileChange () {
108 this.uploadFirstStep()
bf57d5ee
C
109 }
110
bfb3a98f
C
111 checkForm () {
112 this.forceCheck()
113
114 return this.form.valid
e822fdae
C
115 }
116
8c4890cb
DG
117 cancelUpload () {
118 if (this.videoUploadObservable !== null) {
119 this.videoUploadObservable.unsubscribe()
120 this.isUploadingVideo = false
121 this.videoUploadPercents = 0
8c4890cb 122 this.videoUploadObservable = null
b1d40cff 123 this.notificationsService.info(this.i18n('Info'), this.i18n('Upload cancelled'))
8c4890cb
DG
124 }
125 }
126
27e1a06c 127 uploadFirstStep () {
81c263c8 128 const videofile = this.videofileInput.nativeElement.files[0] as File
a22bfc3e
C
129 if (!videofile) return
130
81c263c8
C
131 // Cannot upload videos > 4GB for now
132 if (videofile.size > 4 * 1024 * 1024 * 1024) {
b1d40cff 133 this.notificationsService.error(this.i18n('Error'), this.i18n('We are sorry but PeerTube cannot handle videos > 4GB'))
81c263c8
C
134 return
135 }
136
ce5496d6 137 const videoQuota = this.authService.getUser().videoQuota
a22bfc3e 138 if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
ce5496d6
C
139 const bytePipes = new BytesPipe()
140
b1d40cff
C
141 const msg = this.i18n(
142 'Your video quota is exceeded with this video (video size: {{ videoSize }}, used: {{ videoQuotaUsed }}, quota: {{ videoQuota }})',
143 {
144 videoSize: bytePipes.transform(videofile.size, 0),
145 videoQuotaUsed: bytePipes.transform(this.userVideoQuotaUsed, 0),
146 videoQuota: bytePipes.transform(videoQuota, 0)
147 }
148 )
149 this.notificationsService.error(this.i18n('Error'), msg)
ce5496d6
C
150 return
151 }
152
2f315e2f
C
153 this.videoFileName = videofile.name
154
f2c3f7cd
C
155 const nameWithoutExtension = videofile.name.replace(/\.[^/.]+$/, '')
156 let name: string
157
158 // If the name of the file is very small, keep the extension
159 if (nameWithoutExtension.length < 3) {
160 name = videofile.name
161 } else {
162 name = nameWithoutExtension
163 }
164
cadb46d8 165 const privacy = this.firstStepPrivacyId.toString()
baeefe22 166 const nsfw = false
2186386c 167 const waitTranscoding = true
47564bbe 168 const commentsEnabled = true
cadb46d8 169 const channelId = this.firstStepChannelId.toString()
bfb3a98f
C
170
171 const formData = new FormData()
172 formData.append('name', name)
fed95155 173 // Put the video "private" -> we are waiting the user validation of the second step
cadb46d8 174 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
bfb3a98f 175 formData.append('nsfw', '' + nsfw)
47564bbe 176 formData.append('commentsEnabled', '' + commentsEnabled)
2186386c 177 formData.append('waitTranscoding', '' + waitTranscoding)
bcd9f81e 178 formData.append('channelId', '' + channelId)
bfb3a98f
C
179 formData.append('videofile', videofile)
180
baeefe22
C
181 this.isUploadingVideo = true
182 this.form.patchValue({
183 name,
184 privacy,
185 nsfw,
186 channelId
187 })
e822fdae 188
8c4890cb 189 this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe(
bfb3a98f
C
190 event => {
191 if (event.type === HttpEventType.UploadProgress) {
c182778e 192 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
bfb3a98f 193 } else if (event instanceof HttpResponse) {
baeefe22 194 this.videoUploaded = true
cadb46d8 195
a4b8a4dd 196 this.videoUploadedIds = event.body.video
8c4890cb
DG
197
198 this.videoUploadObservable = null
bfb3a98f
C
199 }
200 },
201
315cc0cc
C
202 err => {
203 // Reset progress
ce5496d6 204 this.isUploadingVideo = false
c182778e 205 this.videoUploadPercents = 0
8c4890cb 206 this.videoUploadObservable = null
b1d40cff 207 this.notificationsService.error(this.i18n('Error'), err.message)
315cc0cc 208 }
bfb3a98f 209 )
dc8bc31b 210 }
27e1a06c
C
211
212 updateSecondStep () {
213 if (this.checkForm() === false) {
214 return
215 }
216
cadb46d8
C
217 const video = new VideoEdit()
218 video.patch(this.form.value)
a4b8a4dd
C
219 video.id = this.videoUploadedIds.id
220 video.uuid = this.videoUploadedIds.uuid
27e1a06c 221
68e24d72
C
222 this.isUpdatingVideo = true
223 this.loadingBar.start()
27e1a06c
C
224 this.videoService.updateVideo(video)
225 .subscribe(
226 () => {
68e24d72 227 this.isUpdatingVideo = false
66ee325f 228 this.isUploadingVideo = false
68e24d72
C
229 this.loadingBar.complete()
230
b1d40cff 231 this.notificationsService.success(this.i18n('Success'), this.i18n('Video published.'))
a4b8a4dd 232 this.router.navigate([ '/videos/watch', video.uuid ])
27e1a06c
C
233 },
234
235 err => {
68e24d72 236 this.isUpdatingVideo = false
b1d40cff 237 this.notificationsService.error(this.i18n('Error'), err.message)
27e1a06c
C
238 console.error(err)
239 }
240 )
241
242 }
dc8bc31b 243}