]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-add.component.ts
Remove unnecessary image check in video upload
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-add.component.ts
CommitLineData
202f6b6c 1import { HttpEventType, HttpResponse } from '@angular/common/http'
f6a043df 2import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
df98563e 3import { Router } from '@angular/router'
ce5496d6 4import { UserService } from '@app/shared'
f6a043df 5import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
68e24d72 6import { LoadingBarService } from '@ngx-loading-bar/core'
df98563e 7import { NotificationsService } from 'angular2-notifications'
ce5496d6 8import { BytesPipe } from 'ngx-pipes'
db400f44 9import { Subscription } from 'rxjs'
cadb46d8 10import { VideoPrivacy } from '../../../../../shared/models/videos'
202f6b6c 11import { AuthService, ServerService } from '../../core'
27e1a06c 12import { FormReactive } from '../../shared'
15a7387d 13import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
27e1a06c 14import { VideoEdit } from '../../shared/video/video-edit.model'
63c4db6d 15import { VideoService } from '../../shared/video/video.service'
b1d40cff 16import { I18n } from '@ngx-translate/i18n-polyfill'
d18d6478 17import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
40e87e9e
C
18import { switchMap } from 'rxjs/operators'
19import { VideoCaptionService } from '@app/shared/video-caption'
1553e15d 20
dc8bc31b
C
21@Component({
22 selector: 'my-videos-add',
27e1a06c
C
23 templateUrl: './video-add.component.html',
24 styleUrls: [
25 './shared/video-edit.component.scss',
26 './video-add.component.scss'
27 ]
dc8bc31b 28})
f6a043df 29export class VideoAddComponent extends FormReactive implements OnInit, OnDestroy, CanComponentDeactivate {
bfb3a98f
C
30 @ViewChild('videofileInput') videofileInput
31
bbe0f064
C
32 // So that it can be accessed in the template
33 readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY
34
27e1a06c 35 isUploadingVideo = false
68e24d72 36 isUpdatingVideo = false
baeefe22 37 videoUploaded = false
f6a043df 38 videoUploadObservable: Subscription = null
c182778e 39 videoUploadPercents = 0
a4b8a4dd
C
40 videoUploadedIds = {
41 id: 0,
42 uuid: ''
43 }
2f315e2f 44 videoFileName: string
3758da94 45
74af5145 46 userVideoChannels: { id: number, label: string, support: string }[] = []
ce5496d6 47 userVideoQuotaUsed = 0
27e1a06c 48 videoPrivacies = []
cadb46d8
C
49 firstStepPrivacyId = 0
50 firstStepChannelId = 0
40e87e9e 51 videoCaptions = []
dc8bc31b 52
df98563e 53 constructor (
d18d6478 54 protected formValidatorService: FormValidatorService,
7ddd02c9 55 private router: Router,
6e07c3de 56 private notificationsService: NotificationsService,
bcd9f81e 57 private authService: AuthService,
ce5496d6 58 private userService: UserService,
db7af09b 59 private serverService: ServerService,
68e24d72 60 private videoService: VideoService,
b1d40cff 61 private loadingBar: LoadingBarService,
40e87e9e
C
62 private i18n: I18n,
63 private videoCaptionService: VideoCaptionService
4b2f33f3 64 ) {
df98563e 65 super()
4b2f33f3 66 }
dc8bc31b 67
108af661
C
68 get videoExtensions () {
69 return this.serverService.getConfig().video.file.extensions.join(',')
70 }
71
df98563e 72 ngOnInit () {
d18d6478 73 this.buildForm({})
2de96f4d 74
15a7387d
C
75 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
76 .then(() => this.firstStepChannelId = this.userVideoChannels[0].id)
77
ce5496d6
C
78 this.userService.getMyVideoQuotaUsed()
79 .subscribe(data => this.userVideoQuotaUsed = data.videoQuotaUsed)
80
c182778e 81 this.serverService.videoPrivaciesLoaded
baeefe22
C
82 .subscribe(
83 () => {
84 this.videoPrivacies = this.serverService.getVideoPrivacies()
cadb46d8
C
85
86 // Public by default
87 this.firstStepPrivacyId = VideoPrivacy.PUBLIC
baeefe22 88 })
e822fdae
C
89 }
90
f6a043df
C
91 ngOnDestroy () {
92 if (this.videoUploadObservable) {
93 this.videoUploadObservable.unsubscribe()
94 }
95 }
96
529479f9 97 canDeactivate () {
f6a043df
C
98 let text = ''
99
100 if (this.videoUploaded === true) {
b1d40cff
C
101 // FIXME: cannot concatenate strings inside i18n service :/
102 text = this.i18n('Your video was uploaded in your account and is private.') +
103 this.i18n('But associated data (tags, description...) will be lost, are you sure you want to leave this page?')
f6a043df 104 } else {
b1d40cff 105 text = this.i18n('Your video is not uploaded yet, are you sure you want to leave this page?')
f6a043df
C
106 }
107
108 return {
109 canDeactivate: !this.isUploadingVideo,
110 text
111 }
529479f9
DG
112 }
113
baeefe22
C
114 fileChange () {
115 this.uploadFirstStep()
bf57d5ee
C
116 }
117
bfb3a98f
C
118 checkForm () {
119 this.forceCheck()
120
121 return this.form.valid
e822fdae
C
122 }
123
8c4890cb
DG
124 cancelUpload () {
125 if (this.videoUploadObservable !== null) {
126 this.videoUploadObservable.unsubscribe()
127 this.isUploadingVideo = false
128 this.videoUploadPercents = 0
8c4890cb 129 this.videoUploadObservable = null
b1d40cff 130 this.notificationsService.info(this.i18n('Info'), this.i18n('Upload cancelled'))
8c4890cb
DG
131 }
132 }
133
27e1a06c 134 uploadFirstStep () {
81c263c8 135 const videofile = this.videofileInput.nativeElement.files[0] as File
a22bfc3e
C
136 if (!videofile) return
137
34b19192
C
138 // Cannot upload videos > 8GB for now
139 if (videofile.size > 8 * 1024 * 1024 * 1024) {
140 this.notificationsService.error(this.i18n('Error'), this.i18n('We are sorry but PeerTube cannot handle videos > 8GB'))
141 return
142 }
81c263c8 143
ce5496d6 144 const videoQuota = this.authService.getUser().videoQuota
a22bfc3e 145 if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
ce5496d6
C
146 const bytePipes = new BytesPipe()
147
b1d40cff
C
148 const msg = this.i18n(
149 'Your video quota is exceeded with this video (video size: {{ videoSize }}, used: {{ videoQuotaUsed }}, quota: {{ videoQuota }})',
150 {
151 videoSize: bytePipes.transform(videofile.size, 0),
152 videoQuotaUsed: bytePipes.transform(this.userVideoQuotaUsed, 0),
153 videoQuota: bytePipes.transform(videoQuota, 0)
154 }
155 )
156 this.notificationsService.error(this.i18n('Error'), msg)
ce5496d6
C
157 return
158 }
159
2f315e2f
C
160 this.videoFileName = videofile.name
161
f2c3f7cd
C
162 const nameWithoutExtension = videofile.name.replace(/\.[^/.]+$/, '')
163 let name: string
164
165 // If the name of the file is very small, keep the extension
40e87e9e
C
166 if (nameWithoutExtension.length < 3) name = videofile.name
167 else name = nameWithoutExtension
f2c3f7cd 168
cadb46d8 169 const privacy = this.firstStepPrivacyId.toString()
baeefe22 170 const nsfw = false
2186386c 171 const waitTranscoding = true
47564bbe 172 const commentsEnabled = true
cadb46d8 173 const channelId = this.firstStepChannelId.toString()
bfb3a98f
C
174
175 const formData = new FormData()
176 formData.append('name', name)
fed95155 177 // Put the video "private" -> we are waiting the user validation of the second step
cadb46d8 178 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
bfb3a98f 179 formData.append('nsfw', '' + nsfw)
47564bbe 180 formData.append('commentsEnabled', '' + commentsEnabled)
2186386c 181 formData.append('waitTranscoding', '' + waitTranscoding)
bcd9f81e 182 formData.append('channelId', '' + channelId)
bfb3a98f
C
183 formData.append('videofile', videofile)
184
baeefe22
C
185 this.isUploadingVideo = true
186 this.form.patchValue({
187 name,
188 privacy,
189 nsfw,
190 channelId
191 })
e822fdae 192
8c4890cb 193 this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe(
bfb3a98f
C
194 event => {
195 if (event.type === HttpEventType.UploadProgress) {
c182778e 196 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
bfb3a98f 197 } else if (event instanceof HttpResponse) {
baeefe22 198 this.videoUploaded = true
cadb46d8 199
a4b8a4dd 200 this.videoUploadedIds = event.body.video
8c4890cb
DG
201
202 this.videoUploadObservable = null
bfb3a98f
C
203 }
204 },
205
315cc0cc
C
206 err => {
207 // Reset progress
ce5496d6 208 this.isUploadingVideo = false
c182778e 209 this.videoUploadPercents = 0
8c4890cb 210 this.videoUploadObservable = null
b1d40cff 211 this.notificationsService.error(this.i18n('Error'), err.message)
315cc0cc 212 }
bfb3a98f 213 )
dc8bc31b 214 }
27e1a06c
C
215
216 updateSecondStep () {
217 if (this.checkForm() === false) {
218 return
219 }
220
cadb46d8
C
221 const video = new VideoEdit()
222 video.patch(this.form.value)
a4b8a4dd
C
223 video.id = this.videoUploadedIds.id
224 video.uuid = this.videoUploadedIds.uuid
27e1a06c 225
68e24d72
C
226 this.isUpdatingVideo = true
227 this.loadingBar.start()
27e1a06c 228 this.videoService.updateVideo(video)
40e87e9e
C
229 .pipe(
230 // Then update captions
231 switchMap(() => this.videoCaptionService.updateCaptions(video.id, this.videoCaptions))
232 )
233 .subscribe(
234 () => {
235 this.isUpdatingVideo = false
236 this.isUploadingVideo = false
237 this.loadingBar.complete()
238
239 this.notificationsService.success(this.i18n('Success'), this.i18n('Video published.'))
240 this.router.navigate([ '/videos/watch', video.uuid ])
241 },
242
243 err => {
244 this.isUpdatingVideo = false
245 this.notificationsService.error(this.i18n('Error'), err.message)
246 console.error(err)
247 }
248 )
27e1a06c 249 }
dc8bc31b 250}