1 import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
2 import { Notifier, ServerService } from '@app/core'
3 import { Account, VideoChannel } from '@app/shared/shared-main'
4 import { NgbPopover } from '@ng-bootstrap/ng-bootstrap'
5 import { getBytes } from '@root-helpers/bytes'
6 import { imageToDataURL } from '@root-helpers/images'
9 selector: 'my-actor-avatar-edit',
10 templateUrl: './actor-avatar-edit.component.html',
12 './actor-image-edit.scss',
13 './actor-avatar-edit.component.scss'
16 export class ActorAvatarEditComponent implements OnInit {
17 @ViewChild('avatarfileInput') avatarfileInput: ElementRef<HTMLInputElement>
18 @ViewChild('avatarPopover') avatarPopover: NgbPopover
20 @Input() actor: VideoChannel | Account
21 @Input() editable = true
22 @Input() displaySubscribers = true
23 @Input() displayUsername = true
24 @Input() previewImage = false
26 @Output() avatarChange = new EventEmitter<FormData>()
27 @Output() avatarDelete = new EventEmitter<void>()
36 private serverService: ServerService,
37 private notifier: Notifier
41 const config = this.serverService.getHTMLConfig()
43 this.maxAvatarSize = config.avatar.file.size.max
44 this.avatarExtensions = config.avatar.file.extensions.join(', ')
46 this.avatarFormat = `${$localize`max size`}: 192*192px, ` +
47 `${getBytes(this.maxAvatarSize)} ${$localize`extensions`}: ${this.avatarExtensions}`
50 onAvatarChange (input: HTMLInputElement) {
51 this.avatarfileInput = new ElementRef(input)
53 const avatarfile = this.avatarfileInput.nativeElement.files[0]
54 if (avatarfile.size > this.maxAvatarSize) {
55 this.notifier.error('Error', $localize`This image is too large.`)
59 const formData = new FormData()
60 formData.append('avatarfile', avatarfile)
61 this.avatarPopover?.close()
62 this.avatarChange.emit(formData)
64 if (this.previewImage) {
65 imageToDataURL(avatarfile).then(result => this.preview = result)
70 this.preview = undefined
71 this.avatarDelete.emit()
75 return !!this.preview || this.actor.avatars.length !== 0
79 return !!(this.actor as VideoChannel).ownerAccount
82 getChannel (): VideoChannel {
83 if (this.isChannel()) return this.actor as VideoChannel
88 getAccount (): Account {
89 if (this.isChannel()) return undefined
91 return this.actor as Account