]>
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 C |
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' | |
78848714 C |
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' | |
fbad87b0 | 12 | import { I18n } from '@ngx-translate/i18n-polyfill' |
78848714 C |
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' | |
fbad87b0 | 16 | import { VideoCaptionService } from '@app/shared/video-caption' |
7373507f | 17 | import { scrollToTop } from '@app/shared/misc/utils' |
fbad87b0 C |
18 | |
19 | @Component({ | |
20 | selector: 'my-video-upload', | |
21 | templateUrl: './video-upload.component.html', | |
22 | styleUrls: [ | |
78848714 | 23 | '../shared/video-edit.component.scss', |
fbad87b0 C |
24 | './video-upload.component.scss' |
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 | } | |
47 | ||
7373507f C |
48 | error: string |
49 | ||
43620009 | 50 | protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC |
fbad87b0 C |
51 | |
52 | constructor ( | |
53 | protected formValidatorService: FormValidatorService, | |
43620009 C |
54 | protected loadingBar: LoadingBarService, |
55 | protected notificationsService: NotificationsService, | |
56 | protected authService: AuthService, | |
57 | protected serverService: ServerService, | |
58 | protected videoService: VideoService, | |
59 | protected videoCaptionService: VideoCaptionService, | |
fbad87b0 | 60 | private userService: UserService, |
43620009 C |
61 | private router: Router, |
62 | private i18n: I18n | |
fbad87b0 C |
63 | ) { |
64 | super() | |
65 | } | |
66 | ||
67 | get videoExtensions () { | |
68 | return this.serverService.getConfig().video.file.extensions.join(',') | |
69 | } | |
70 | ||
71 | ngOnInit () { | |
43620009 | 72 | super.ngOnInit() |
fbad87b0 C |
73 | |
74 | this.userService.getMyVideoQuotaUsed() | |
348106f2 C |
75 | .subscribe(data => { |
76 | this.userVideoQuotaUsed = data.videoQuotaUsed | |
77 | this.userVideoQuotaUsedDaily = data.videoQuotaUsedDaily | |
78 | }) | |
fbad87b0 C |
79 | } |
80 | ||
81 | ngOnDestroy () { | |
43620009 | 82 | if (this.videoUploadObservable) this.videoUploadObservable.unsubscribe() |
fbad87b0 C |
83 | } |
84 | ||
85 | canDeactivate () { | |
86 | let text = '' | |
87 | ||
88 | if (this.videoUploaded === true) { | |
89 | // FIXME: cannot concatenate strings inside i18n service :/ | |
8c2b9756 | 90 | text = this.i18n('Your video was uploaded to your account and is private.') + ' ' + |
fbad87b0 C |
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 | ||
fbad87b0 C |
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 () { | |
c199c427 | 117 | const videofile = this.videofileInput.nativeElement.files[0] |
fbad87b0 C |
118 | if (!videofile) return |
119 | ||
120 | // Cannot upload videos > 8GB for now | |
121 | if (videofile.size > 8 * 1024 * 1024 * 1024) { | |
122 | this.notificationsService.error(this.i18n('Error'), this.i18n('We are sorry but PeerTube cannot handle videos > 8GB')) | |
123 | return | |
124 | } | |
125 | ||
bee0abff | 126 | const bytePipes = new BytesPipe() |
fbad87b0 C |
127 | const videoQuota = this.authService.getUser().videoQuota |
128 | if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) { | |
fbad87b0 | 129 | const msg = this.i18n( |
d972dc7f | 130 | 'Your video quota is exceeded with this video (video size: {{videoSize}}, used: {{videoQuotaUsed}}, quota: {{videoQuota}})', |
fbad87b0 C |
131 | { |
132 | videoSize: bytePipes.transform(videofile.size, 0), | |
133 | videoQuotaUsed: bytePipes.transform(this.userVideoQuotaUsed, 0), | |
134 | videoQuota: bytePipes.transform(videoQuota, 0) | |
135 | } | |
136 | ) | |
137 | this.notificationsService.error(this.i18n('Error'), msg) | |
138 | return | |
139 | } | |
140 | ||
bee0abff FA |
141 | const videoQuotaDaily = this.authService.getUser().videoQuotaDaily |
142 | if (videoQuotaDaily !== -1 && (this.userVideoQuotaUsedDaily + videofile.size) > videoQuotaDaily) { | |
143 | const msg = this.i18n( | |
d972dc7f | 144 | 'Your daily video quota is exceeded with this video (video size: {{videoSize}}, used: {{quotaUsedDaily}}, quota: {{quotaDaily}})', |
bee0abff FA |
145 | { |
146 | videoSize: bytePipes.transform(videofile.size, 0), | |
d972dc7f C |
147 | quotaUsedDaily: bytePipes.transform(this.userVideoQuotaUsedDaily, 0), |
148 | quotaDaily: bytePipes.transform(videoQuotaDaily, 0) | |
bee0abff FA |
149 | } |
150 | ) | |
151 | this.notificationsService.error(this.i18n('Error'), msg) | |
152 | return | |
153 | } | |
154 | ||
fbad87b0 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) name = videofile.name | |
160 | else name = nameWithoutExtension | |
161 | ||
162 | const privacy = this.firstStepPrivacyId.toString() | |
163 | const nsfw = false | |
164 | const waitTranscoding = true | |
165 | const commentsEnabled = true | |
166 | const channelId = this.firstStepChannelId.toString() | |
167 | ||
168 | const formData = new FormData() | |
169 | formData.append('name', name) | |
170 | // Put the video "private" -> we are waiting the user validation of the second step | |
171 | formData.append('privacy', VideoPrivacy.PRIVATE.toString()) | |
172 | formData.append('nsfw', '' + nsfw) | |
173 | formData.append('commentsEnabled', '' + commentsEnabled) | |
174 | formData.append('waitTranscoding', '' + waitTranscoding) | |
175 | formData.append('channelId', '' + channelId) | |
176 | formData.append('videofile', videofile) | |
177 | ||
178 | this.isUploadingVideo = true | |
179 | this.firstStepDone.emit(name) | |
180 | ||
181 | this.form.patchValue({ | |
182 | name, | |
183 | privacy, | |
184 | nsfw, | |
185 | channelId | |
186 | }) | |
187 | ||
8cd7faaa C |
188 | this.videoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies) |
189 | ||
fbad87b0 C |
190 | this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe( |
191 | event => { | |
192 | if (event.type === HttpEventType.UploadProgress) { | |
193 | this.videoUploadPercents = Math.round(100 * event.loaded / event.total) | |
194 | } else if (event instanceof HttpResponse) { | |
195 | this.videoUploaded = true | |
196 | ||
197 | this.videoUploadedIds = event.body.video | |
198 | ||
199 | this.videoUploadObservable = null | |
200 | } | |
201 | }, | |
202 | ||
203 | err => { | |
204 | // Reset progress | |
205 | this.isUploadingVideo = false | |
206 | this.videoUploadPercents = 0 | |
207 | this.videoUploadObservable = null | |
7373507f | 208 | this.firstStepError.emit() |
fbad87b0 C |
209 | this.notificationsService.error(this.i18n('Error'), err.message) |
210 | } | |
211 | ) | |
212 | } | |
213 | ||
59c9c5d9 C |
214 | isPublishingButtonDisabled () { |
215 | return !this.form.valid || | |
216 | this.isUpdatingVideo === true || | |
217 | this.videoUploaded !== true | |
218 | } | |
219 | ||
fbad87b0 C |
220 | updateSecondStep () { |
221 | if (this.checkForm() === false) { | |
222 | return | |
223 | } | |
224 | ||
225 | const video = new VideoEdit() | |
226 | video.patch(this.form.value) | |
227 | video.id = this.videoUploadedIds.id | |
228 | video.uuid = this.videoUploadedIds.uuid | |
229 | ||
230 | this.isUpdatingVideo = true | |
43620009 C |
231 | |
232 | this.updateVideoAndCaptions(video) | |
fbad87b0 C |
233 | .subscribe( |
234 | () => { | |
235 | this.isUpdatingVideo = false | |
236 | this.isUploadingVideo = false | |
fbad87b0 C |
237 | |
238 | this.notificationsService.success(this.i18n('Success'), this.i18n('Video published.')) | |
239 | this.router.navigate([ '/videos/watch', video.uuid ]) | |
240 | }, | |
241 | ||
242 | err => { | |
7373507f C |
243 | this.error = err.message |
244 | scrollToTop() | |
fbad87b0 C |
245 | console.error(err) |
246 | } | |
247 | ) | |
248 | } | |
249 | } |