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