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