aboutsummaryrefslogblamecommitdiffhomepage
path: root/client/src/app/shared/shared-main/account/actor-avatar-info.component.ts
blob: 3a86e5b21a482fd239fd75a1d80fbdd161e9bd2c (plain) (tree)
1
2
3
4
5
6
7
8
9
10

                                                                                                     
                                                                          
                                             





                                                     
                                                         
                                                                             




                                                       

                     
                                    
                              
 
               
                                         
                              

                                    
                                          
   
 





                                                         


                                                                    
                                                              









                                             
                                                 

   



                                                       
                           
                                                              

   
import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
import { Notifier, ServerService } from '@app/core'
import { Account, BytesPipe, VideoChannel } from '@app/shared/shared-main'
import { ServerConfig } from '@shared/models'

@Component({
  selector: 'my-actor-avatar-info',
  templateUrl: './actor-avatar-info.component.html',
  styleUrls: [ './actor-avatar-info.component.scss' ]
})
export class ActorAvatarInfoComponent implements OnInit {
  @ViewChild('avatarfileInput') avatarfileInput: ElementRef<HTMLInputElement>

  @Input() actor: VideoChannel | Account

  @Output() avatarChange = new EventEmitter<FormData>()

  maxSizeText: string

  private serverConfig: ServerConfig
  private bytesPipe: BytesPipe

  constructor (
    private serverService: ServerService,
    private notifier: Notifier
  ) {
    this.bytesPipe = new BytesPipe()
    this.maxSizeText = $localize`max size`
  }

  ngOnInit (): void {
    this.serverConfig = this.serverService.getTmpConfig()
    this.serverService.getConfig()
        .subscribe(config => this.serverConfig = config)
  }

  onAvatarChange () {
    const avatarfile = this.avatarfileInput.nativeElement.files[ 0 ]
    if (avatarfile.size > this.maxAvatarSize) {
      this.notifier.error('Error', 'This image is too large.')
      return
    }

    const formData = new FormData()
    formData.append('avatarfile', avatarfile)

    this.avatarChange.emit(formData)
  }

  get maxAvatarSize () {
    return this.serverConfig.avatar.file.size.max
  }

  get maxAvatarSizeInBytes () {
    return this.bytesPipe.transform(this.maxAvatarSize)
  }

  get avatarExtensions () {
    return this.serverConfig.avatar.file.extensions.join(', ')
  }
}