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