]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/video-add-components/video-upload.component.ts
Try to improve tools doc
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / video-add-components / video-upload.component.ts
CommitLineData
67ed6552 1import { Subscription } from 'rxjs'
d4132d3f 2import { HttpErrorResponse, HttpEventType, HttpResponse } from '@angular/common/http'
c199c427 3import { Component, ElementRef, EventEmitter, OnDestroy, OnInit, Output, ViewChild } from '@angular/core'
fbad87b0 4import { Router } from '@angular/router'
67ed6552 5import { AuthService, CanComponentDeactivate, Notifier, ServerService, UserService } from '@app/core'
d4132d3f 6import { scrollToTop, uploadErrorHandler } from '@app/helpers'
67ed6552 7import { FormValidatorService } from '@app/shared/shared-forms'
94676e63 8import { BytesPipe, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main'
fbad87b0 9import { LoadingBarService } from '@ngx-loading-bar/core'
67ed6552 10import { VideoPrivacy } from '@shared/models'
1942f11d 11import { VideoSend } from './video-send'
fbad87b0
C
12
13@Component({
14 selector: 'my-video-upload',
15 templateUrl: './video-upload.component.html',
16 styleUrls: [
78848714 17 '../shared/video-edit.component.scss',
457bb213
C
18 './video-upload.component.scss',
19 './video-send.scss'
fbad87b0
C
20 ]
21})
43620009 22export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy, CanComponentDeactivate {
fbad87b0 23 @Output() firstStepDone = new EventEmitter<string>()
7373507f 24 @Output() firstStepError = new EventEmitter<void>()
2f5d2ec5 25 @ViewChild('videofileInput') videofileInput: ElementRef<HTMLInputElement>
fbad87b0
C
26
27 // So that it can be accessed in the template
28 readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY
29
43620009 30 userVideoQuotaUsed = 0
bee0abff 31 userVideoQuotaUsedDaily = 0
43620009 32
7b992a86 33 isUploadingAudioFile = false
fbad87b0
C
34 isUploadingVideo = false
35 isUpdatingVideo = false
7b992a86 36
fbad87b0
C
37 videoUploaded = false
38 videoUploadObservable: Subscription = null
39 videoUploadPercents = 0
40 videoUploadedIds = {
41 id: 0,
42 uuid: ''
43 }
d4132d3f 44 formData: FormData
7b992a86 45
14e2014a 46 waitTranscodingEnabled = true
7b992a86 47 previewfileUpload: File
fbad87b0 48
7373507f 49 error: string
d4132d3f 50 enableRetryAfterError: boolean
fbad87b0 51
43620009 52 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
fbad87b0
C
53
54 constructor (
55 protected formValidatorService: FormValidatorService,
43620009 56 protected loadingBar: LoadingBarService,
f8b2c1b4 57 protected notifier: Notifier,
43620009
C
58 protected authService: AuthService,
59 protected serverService: ServerService,
60 protected videoService: VideoService,
61 protected videoCaptionService: VideoCaptionService,
fbad87b0 62 private userService: UserService,
66357162
C
63 private router: Router
64 ) {
fbad87b0
C
65 super()
66 }
67
68 get videoExtensions () {
758f0d19 69 return this.serverConfig.video.file.extensions.join(', ')
fbad87b0
C
70 }
71
72 ngOnInit () {
43620009 73 super.ngOnInit()
fbad87b0
C
74
75 this.userService.getMyVideoQuotaUsed()
348106f2
C
76 .subscribe(data => {
77 this.userVideoQuotaUsed = data.videoQuotaUsed
78 this.userVideoQuotaUsedDaily = data.videoQuotaUsedDaily
79 })
fbad87b0
C
80 }
81
82 ngOnDestroy () {
43620009 83 if (this.videoUploadObservable) this.videoUploadObservable.unsubscribe()
fbad87b0
C
84 }
85
86 canDeactivate () {
87 let text = ''
88
89 if (this.videoUploaded === true) {
5bfc33b6 90 // FIXME: cannot concatenate strings using $localize
66357162
C
91 text = $localize`Your video was uploaded to your account and is private.` + ' ' +
92 $localize`But associated data (tags, description...) will be lost, are you sure you want to leave this page?`
fbad87b0 93 } else {
66357162 94 text = $localize`Your video is not uploaded yet, are you sure you want to leave this page?`
fbad87b0
C
95 }
96
97 return {
98 canDeactivate: !this.isUploadingVideo,
99 text
100 }
101 }
102
7b992a86
C
103 getVideoFile () {
104 return this.videofileInput.nativeElement.files[0]
105 }
106
c9ff8a08
RK
107 setVideoFile (files: FileList) {
108 this.videofileInput.nativeElement.files = files
109 this.fileChange()
110 }
111
7b992a86
C
112 getAudioUploadLabel () {
113 const videofile = this.getVideoFile()
66357162 114 if (!videofile) return $localize`Upload`
7b992a86 115
66357162 116 return $localize`Upload ${videofile.name}`
7b992a86
C
117 }
118
fbad87b0
C
119 fileChange () {
120 this.uploadFirstStep()
121 }
122
d4132d3f
RK
123 retryUpload () {
124 this.enableRetryAfterError = false
125 this.error = ''
126 this.uploadVideo()
127 }
128
fbad87b0
C
129 cancelUpload () {
130 if (this.videoUploadObservable !== null) {
131 this.videoUploadObservable.unsubscribe()
e713698f 132
fbad87b0
C
133 this.isUploadingVideo = false
134 this.videoUploadPercents = 0
135 this.videoUploadObservable = null
e713698f
C
136
137 this.firstStepError.emit()
d4132d3f
RK
138 this.enableRetryAfterError = false
139 this.error = ''
e713698f 140
66357162 141 this.notifier.info($localize`Upload cancelled`)
fbad87b0
C
142 }
143 }
144
7b992a86
C
145 uploadFirstStep (clickedOnButton = false) {
146 const videofile = this.getVideoFile()
fbad87b0
C
147 if (!videofile) return
148
7b992a86
C
149 if (!this.checkGlobalUserQuota(videofile)) return
150 if (!this.checkDailyUserQuota(videofile)) return
fbad87b0 151
7b992a86
C
152 if (clickedOnButton === false && this.isAudioFile(videofile.name)) {
153 this.isUploadingAudioFile = true
bee0abff
FA
154 return
155 }
156
14e2014a 157 // Build name field
fbad87b0
C
158 const nameWithoutExtension = videofile.name.replace(/\.[^/.]+$/, '')
159 let name: string
160
161 // If the name of the file is very small, keep the extension
162 if (nameWithoutExtension.length < 3) name = videofile.name
163 else name = nameWithoutExtension
164
14e2014a
C
165 // Force user to wait transcoding for unsupported video types in web browsers
166 if (!videofile.name.endsWith('.mp4') && !videofile.name.endsWith('.webm') && !videofile.name.endsWith('.ogv')) {
167 this.waitTranscodingEnabled = false
168 }
169
ba430d75 170 const nsfw = this.serverConfig.instance.isNSFW
fbad87b0
C
171 const waitTranscoding = true
172 const commentsEnabled = true
7f2cfe3a 173 const downloadEnabled = true
fbad87b0
C
174 const channelId = this.firstStepChannelId.toString()
175
d4132d3f
RK
176 this.formData = new FormData()
177 this.formData.append('name', name)
fbad87b0 178 // Put the video "private" -> we are waiting the user validation of the second step
d4132d3f
RK
179 this.formData.append('privacy', VideoPrivacy.PRIVATE.toString())
180 this.formData.append('nsfw', '' + nsfw)
181 this.formData.append('commentsEnabled', '' + commentsEnabled)
182 this.formData.append('downloadEnabled', '' + downloadEnabled)
183 this.formData.append('waitTranscoding', '' + waitTranscoding)
184 this.formData.append('channelId', '' + channelId)
185 this.formData.append('videofile', videofile)
fbad87b0 186
7b992a86 187 if (this.previewfileUpload) {
d4132d3f
RK
188 this.formData.append('previewfile', this.previewfileUpload)
189 this.formData.append('thumbnailfile', this.previewfileUpload)
7b992a86
C
190 }
191
fbad87b0
C
192 this.isUploadingVideo = true
193 this.firstStepDone.emit(name)
194
195 this.form.patchValue({
196 name,
30b1e106 197 privacy: this.firstStepPrivacyId,
fbad87b0 198 nsfw,
30b1e106 199 channelId: this.firstStepChannelId,
7b992a86 200 previewfile: this.previewfileUpload
fbad87b0
C
201 })
202
d4132d3f
RK
203 this.uploadVideo()
204 }
205
206 uploadVideo () {
207 this.videoUploadObservable = this.videoService.uploadVideo(this.formData).subscribe(
fbad87b0
C
208 event => {
209 if (event.type === HttpEventType.UploadProgress) {
210 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
211 } else if (event instanceof HttpResponse) {
212 this.videoUploaded = true
213
214 this.videoUploadedIds = event.body.video
215
216 this.videoUploadObservable = null
217 }
218 },
219
d4132d3f
RK
220 (err: HttpErrorResponse) => {
221 // Reset progress (but keep isUploadingVideo true)
fbad87b0
C
222 this.videoUploadPercents = 0
223 this.videoUploadObservable = null
d4132d3f
RK
224 this.enableRetryAfterError = true
225
226 this.error = uploadErrorHandler({
227 err,
228 name: $localize`video`,
229 notifier: this.notifier,
230 sticky: false
231 })
fbad87b0
C
232 }
233 )
234 }
235
59c9c5d9
C
236 isPublishingButtonDisabled () {
237 return !this.form.valid ||
238 this.isUpdatingVideo === true ||
c7a53f61
C
239 this.videoUploaded !== true ||
240 !this.videoUploadedIds.id
59c9c5d9
C
241 }
242
fbad87b0 243 updateSecondStep () {
c7a53f61 244 if (this.isPublishingButtonDisabled() || !this.checkForm()) {
fbad87b0
C
245 return
246 }
247
248 const video = new VideoEdit()
249 video.patch(this.form.value)
250 video.id = this.videoUploadedIds.id
251 video.uuid = this.videoUploadedIds.uuid
252
253 this.isUpdatingVideo = true
43620009
C
254
255 this.updateVideoAndCaptions(video)
fbad87b0
C
256 .subscribe(
257 () => {
258 this.isUpdatingVideo = false
259 this.isUploadingVideo = false
fbad87b0 260
66357162 261 this.notifier.success($localize`Video published.`)
fbad87b0
C
262 this.router.navigate([ '/videos/watch', video.uuid ])
263 },
264
265 err => {
7373507f
C
266 this.error = err.message
267 scrollToTop()
fbad87b0
C
268 console.error(err)
269 }
270 )
271 }
7b992a86
C
272
273 private checkGlobalUserQuota (videofile: File) {
274 const bytePipes = new BytesPipe()
275
276 // Check global user quota
277 const videoQuota = this.authService.getUser().videoQuota
278 if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
66357162
C
279 const videoSizeBytes = bytePipes.transform(videofile.size, 0)
280 const videoQuotaUsedBytes = bytePipes.transform(this.userVideoQuotaUsed, 0)
281 const videoQuotaBytes = bytePipes.transform(videoQuota, 0)
282
283 const msg = $localize`Your video quota is exceeded with this video (
284video size: ${videoSizeBytes}, used: ${videoQuotaUsedBytes}, quota: ${videoQuotaBytes})`
7b992a86
C
285 this.notifier.error(msg)
286
287 return false
288 }
289
290 return true
291 }
292
293 private checkDailyUserQuota (videofile: File) {
294 const bytePipes = new BytesPipe()
295
296 // Check daily user quota
297 const videoQuotaDaily = this.authService.getUser().videoQuotaDaily
298 if (videoQuotaDaily !== -1 && (this.userVideoQuotaUsedDaily + videofile.size) > videoQuotaDaily) {
66357162
C
299 const videoSizeBytes = bytePipes.transform(videofile.size, 0)
300 const quotaUsedDailyBytes = bytePipes.transform(this.userVideoQuotaUsedDaily, 0)
301 const quotaDailyBytes = bytePipes.transform(videoQuotaDaily, 0)
302
303 const msg = $localize`Your daily video quota is exceeded with this video (
304video size: ${videoSizeBytes}, used: ${quotaUsedDailyBytes}, quota: ${quotaDailyBytes})`
7b992a86
C
305 this.notifier.error(msg)
306
307 return false
308 }
309
310 return true
311 }
312
313 private isAudioFile (filename: string) {
99d362de
C
314 const extensions = [ '.mp3', '.flac', '.ogg', '.wma', '.wav' ]
315
316 return extensions.some(e => filename.endsWith(e))
7b992a86 317 }
fbad87b0 318}