Fix images size limit
[oweals/peertube.git] / client / src / app / +my-account / my-account-settings / my-account-settings.component.ts
1 import { Component, OnInit, ViewChild } from '@angular/core'
2 import { NotificationsService } from 'angular2-notifications'
3 import { BytesPipe } from 'ngx-pipes'
4 import { AuthService } from '../../core'
5 import { ServerService } from '../../core/server'
6 import { User } from '../../shared'
7 import { UserService } from '../../shared/users'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9
10 @Component({
11   selector: 'my-account-settings',
12   templateUrl: './my-account-settings.component.html',
13   styleUrls: [ './my-account-settings.component.scss' ]
14 })
15 export class MyAccountSettingsComponent implements OnInit {
16   @ViewChild('avatarfileInput') avatarfileInput
17
18   user: User = null
19   userVideoQuota = '0'
20   userVideoQuotaUsed = 0
21
22   constructor (
23     private userService: UserService,
24     private authService: AuthService,
25     private serverService: ServerService,
26     private notificationsService: NotificationsService,
27     private i18n: I18n
28   ) {}
29
30   get userInformationLoaded () {
31     return this.authService.userInformationLoaded
32   }
33
34   ngOnInit () {
35     this.user = this.authService.getUser()
36
37     this.authService.userInformationLoaded.subscribe(
38       () => {
39         if (this.user.videoQuota !== -1) {
40           this.userVideoQuota = new BytesPipe().transform(this.user.videoQuota, 0).toString()
41         } else {
42           this.userVideoQuota = this.i18n('Unlimited')
43         }
44       }
45     )
46
47     this.userService.getMyVideoQuotaUsed()
48       .subscribe(data => this.userVideoQuotaUsed = data.videoQuotaUsed)
49   }
50
51   changeAvatar () {
52     const avatarfile = this.avatarfileInput.nativeElement.files[ 0 ]
53     if (avatarfile.size > this.maxAvatarSize) {
54       this.notificationsService.error('Error', 'This image is too large.')
55       return
56     }
57
58     const formData = new FormData()
59     formData.append('avatarfile', avatarfile)
60
61     this.userService.changeAvatar(formData)
62       .subscribe(
63         data => {
64           this.notificationsService.success(this.i18n('Success'), this.i18n('Avatar changed.'))
65
66           this.user.updateAccountAvatar(data.avatar)
67         },
68
69         err => this.notificationsService.error(this.i18n('Error'), err.message)
70       )
71   }
72
73   get maxAvatarSize () {
74     return this.serverService.getConfig().avatar.file.size.max
75   }
76
77   get avatarExtensions () {
78     return this.serverService.getConfig().avatar.file.extensions.join(',')
79   }
80 }