]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-add-components/video-upload.component.ts
Remove hard-coded 8GB upload limit in client (#1293)
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-add-components / video-upload.component.ts
1 import { HttpEventType, HttpResponse } from '@angular/common/http'
2 import { Component, ElementRef, EventEmitter, OnDestroy, OnInit, Output, ViewChild } from '@angular/core'
3 import { Router } from '@angular/router'
4 import { LoadingBarService } from '@ngx-loading-bar/core'
5 import { NotificationsService } from 'angular2-notifications'
6 import { BytesPipe } from 'ngx-pipes'
7 import { Subscription } from 'rxjs'
8 import { VideoPrivacy } from '../../../../../../shared/models/videos'
9 import { AuthService, ServerService } from '../../../core'
10 import { VideoEdit } from '../../../shared/video/video-edit.model'
11 import { VideoService } from '../../../shared/video/video.service'
12 import { I18n } from '@ngx-translate/i18n-polyfill'
13 import { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send'
14 import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
15 import { FormValidatorService, UserService } from '@app/shared'
16 import { VideoCaptionService } from '@app/shared/video-caption'
17 import { scrollToTop } from '@app/shared/misc/utils'
18
19 @Component({
20 selector: 'my-video-upload',
21 templateUrl: './video-upload.component.html',
22 styleUrls: [
23 '../shared/video-edit.component.scss',
24 './video-upload.component.scss'
25 ]
26 })
27 export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy, CanComponentDeactivate {
28 @Output() firstStepDone = new EventEmitter<string>()
29 @Output() firstStepError = new EventEmitter<void>()
30 @ViewChild('videofileInput') videofileInput: ElementRef<HTMLInputElement>
31
32 // So that it can be accessed in the template
33 readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY
34
35 userVideoQuotaUsed = 0
36 userVideoQuotaUsedDaily = 0
37
38 isUploadingVideo = false
39 isUpdatingVideo = false
40 videoUploaded = false
41 videoUploadObservable: Subscription = null
42 videoUploadPercents = 0
43 videoUploadedIds = {
44 id: 0,
45 uuid: ''
46 }
47
48 error: string
49
50 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
51
52 constructor (
53 protected formValidatorService: FormValidatorService,
54 protected loadingBar: LoadingBarService,
55 protected notificationsService: NotificationsService,
56 protected authService: AuthService,
57 protected serverService: ServerService,
58 protected videoService: VideoService,
59 protected videoCaptionService: VideoCaptionService,
60 private userService: UserService,
61 private router: Router,
62 private i18n: I18n
63 ) {
64 super()
65 }
66
67 get videoExtensions () {
68 return this.serverService.getConfig().video.file.extensions.join(',')
69 }
70
71 ngOnInit () {
72 super.ngOnInit()
73
74 this.userService.getMyVideoQuotaUsed()
75 .subscribe(data => {
76 this.userVideoQuotaUsed = data.videoQuotaUsed
77 this.userVideoQuotaUsedDaily = data.videoQuotaUsedDaily
78 })
79 }
80
81 ngOnDestroy () {
82 if (this.videoUploadObservable) this.videoUploadObservable.unsubscribe()
83 }
84
85 canDeactivate () {
86 let text = ''
87
88 if (this.videoUploaded === true) {
89 // FIXME: cannot concatenate strings inside i18n service :/
90 text = this.i18n('Your video was uploaded to your account and is private.') + ' ' +
91 this.i18n('But associated data (tags, description...) will be lost, are you sure you want to leave this page?')
92 } else {
93 text = this.i18n('Your video is not uploaded yet, are you sure you want to leave this page?')
94 }
95
96 return {
97 canDeactivate: !this.isUploadingVideo,
98 text
99 }
100 }
101
102 fileChange () {
103 this.uploadFirstStep()
104 }
105
106 cancelUpload () {
107 if (this.videoUploadObservable !== null) {
108 this.videoUploadObservable.unsubscribe()
109 this.isUploadingVideo = false
110 this.videoUploadPercents = 0
111 this.videoUploadObservable = null
112 this.notificationsService.info(this.i18n('Info'), this.i18n('Upload cancelled'))
113 }
114 }
115
116 uploadFirstStep () {
117 const videofile = this.videofileInput.nativeElement.files[0]
118 if (!videofile) return
119
120 const bytePipes = new BytesPipe()
121 const videoQuota = this.authService.getUser().videoQuota
122 if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
123 const msg = this.i18n(
124 'Your video quota is exceeded with this video (video size: {{videoSize}}, used: {{videoQuotaUsed}}, quota: {{videoQuota}})',
125 {
126 videoSize: bytePipes.transform(videofile.size, 0),
127 videoQuotaUsed: bytePipes.transform(this.userVideoQuotaUsed, 0),
128 videoQuota: bytePipes.transform(videoQuota, 0)
129 }
130 )
131 this.notificationsService.error(this.i18n('Error'), msg)
132 return
133 }
134
135 const videoQuotaDaily = this.authService.getUser().videoQuotaDaily
136 if (videoQuotaDaily !== -1 && (this.userVideoQuotaUsedDaily + videofile.size) > videoQuotaDaily) {
137 const msg = this.i18n(
138 'Your daily video quota is exceeded with this video (video size: {{videoSize}}, used: {{quotaUsedDaily}}, quota: {{quotaDaily}})',
139 {
140 videoSize: bytePipes.transform(videofile.size, 0),
141 quotaUsedDaily: bytePipes.transform(this.userVideoQuotaUsedDaily, 0),
142 quotaDaily: bytePipes.transform(videoQuotaDaily, 0)
143 }
144 )
145 this.notificationsService.error(this.i18n('Error'), msg)
146 return
147 }
148
149 const nameWithoutExtension = videofile.name.replace(/\.[^/.]+$/, '')
150 let name: string
151
152 // If the name of the file is very small, keep the extension
153 if (nameWithoutExtension.length < 3) name = videofile.name
154 else name = nameWithoutExtension
155
156 const privacy = this.firstStepPrivacyId.toString()
157 const nsfw = false
158 const waitTranscoding = true
159 const commentsEnabled = true
160 const channelId = this.firstStepChannelId.toString()
161
162 const formData = new FormData()
163 formData.append('name', name)
164 // Put the video "private" -> we are waiting the user validation of the second step
165 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
166 formData.append('nsfw', '' + nsfw)
167 formData.append('commentsEnabled', '' + commentsEnabled)
168 formData.append('waitTranscoding', '' + waitTranscoding)
169 formData.append('channelId', '' + channelId)
170 formData.append('videofile', videofile)
171
172 this.isUploadingVideo = true
173 this.firstStepDone.emit(name)
174
175 this.form.patchValue({
176 name,
177 privacy,
178 nsfw,
179 channelId
180 })
181
182 this.videoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies)
183
184 this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe(
185 event => {
186 if (event.type === HttpEventType.UploadProgress) {
187 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
188 } else if (event instanceof HttpResponse) {
189 this.videoUploaded = true
190
191 this.videoUploadedIds = event.body.video
192
193 this.videoUploadObservable = null
194 }
195 },
196
197 err => {
198 // Reset progress
199 this.isUploadingVideo = false
200 this.videoUploadPercents = 0
201 this.videoUploadObservable = null
202 this.firstStepError.emit()
203 this.notificationsService.error(this.i18n('Error'), err.message)
204 }
205 )
206 }
207
208 isPublishingButtonDisabled () {
209 return !this.form.valid ||
210 this.isUpdatingVideo === true ||
211 this.videoUploaded !== true
212 }
213
214 updateSecondStep () {
215 if (this.checkForm() === false) {
216 return
217 }
218
219 const video = new VideoEdit()
220 video.patch(this.form.value)
221 video.id = this.videoUploadedIds.id
222 video.uuid = this.videoUploadedIds.uuid
223
224 this.isUpdatingVideo = true
225
226 this.updateVideoAndCaptions(video)
227 .subscribe(
228 () => {
229 this.isUpdatingVideo = false
230 this.isUploadingVideo = false
231
232 this.notificationsService.success(this.i18n('Success'), this.i18n('Video published.'))
233 this.router.navigate([ '/videos/watch', video.uuid ])
234 },
235
236 err => {
237 this.error = err.message
238 scrollToTop()
239 console.error(err)
240 }
241 )
242 }
243 }