1 import { Component, ElementRef, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core'
2 import { Notifier, ServerService } from '@app/core'
3 import { getBytes } from '@root-helpers/bytes'
4 import { ServerConfig } from '@shared/models'
5 import { VideoChannel } from '../video-channel/video-channel.model'
6 import { Account } from '../account/account.model'
7 import { NgbPopover } from '@ng-bootstrap/ng-bootstrap'
8 import { Actor } from './actor.model'
11 selector: 'my-actor-avatar-info',
12 templateUrl: './actor-avatar-info.component.html',
13 styleUrls: [ './actor-avatar-info.component.scss' ]
15 export class ActorAvatarInfoComponent implements OnInit, OnChanges {
16 @ViewChild('avatarfileInput') avatarfileInput: ElementRef<HTMLInputElement>
17 @ViewChild('avatarPopover') avatarPopover: NgbPopover
19 @Input() actor: VideoChannel | Account
21 @Output() avatarChange = new EventEmitter<FormData>()
22 @Output() avatarDelete = new EventEmitter<void>()
24 private avatarUrl: string
25 private serverConfig: ServerConfig
28 private serverService: ServerService,
29 private notifier: Notifier
33 this.serverConfig = this.serverService.getTmpConfig()
34 this.serverService.getConfig()
35 .subscribe(config => this.serverConfig = config)
38 ngOnChanges (changes: SimpleChanges) {
39 if (changes['actor']) {
40 this.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.actor)
44 onAvatarChange (input: HTMLInputElement) {
45 this.avatarfileInput = new ElementRef(input)
47 const avatarfile = this.avatarfileInput.nativeElement.files[ 0 ]
48 if (avatarfile.size > this.maxAvatarSize) {
49 this.notifier.error('Error', $localize`This image is too large.`)
53 const formData = new FormData()
54 formData.append('avatarfile', avatarfile)
55 this.avatarPopover?.close()
56 this.avatarChange.emit(formData)
60 this.avatarDelete.emit()
63 get maxAvatarSize () {
64 return this.serverConfig.avatar.file.size.max
67 get maxAvatarSizeInBytes () {
68 return getBytes(this.maxAvatarSize)
71 get avatarExtensions () {
72 return this.serverConfig.avatar.file.extensions.join(', ')
76 return `${$localize`max size`}: 192*192px, ${this.maxAvatarSizeInBytes} ${$localize`extensions`}: ${this.avatarExtensions}`
80 return !!this.avatarUrl