]>
Commit | Line | Data |
---|---|---|
1ea7da81 | 1 | import { Component, ElementRef, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core' |
67ed6552 | 2 | import { Notifier, ServerService } from '@app/core' |
b4c3c51d | 3 | import { getBytes } from '@root-helpers/bytes' |
67ed6552 | 4 | import { ServerConfig } from '@shared/models' |
b4c3c51d C |
5 | import { VideoChannel } from '../video-channel/video-channel.model' |
6 | import { Account } from '../account/account.model' | |
1ea7da81 RK |
7 | import { NgbPopover } from '@ng-bootstrap/ng-bootstrap' |
8 | import { Actor } from './actor.model' | |
52d9f792 C |
9 | |
10 | @Component({ | |
11 | selector: 'my-actor-avatar-info', | |
12 | templateUrl: './actor-avatar-info.component.html', | |
13 | styleUrls: [ './actor-avatar-info.component.scss' ] | |
14 | }) | |
1ea7da81 | 15 | export class ActorAvatarInfoComponent implements OnInit, OnChanges { |
2f5d2ec5 | 16 | @ViewChild('avatarfileInput') avatarfileInput: ElementRef<HTMLInputElement> |
1ea7da81 | 17 | @ViewChild('avatarPopover') avatarPopover: NgbPopover |
52d9f792 C |
18 | |
19 | @Input() actor: VideoChannel | Account | |
20 | ||
21 | @Output() avatarChange = new EventEmitter<FormData>() | |
1ea7da81 | 22 | @Output() avatarDelete = new EventEmitter<void>() |
52d9f792 | 23 | |
1ea7da81 | 24 | private avatarUrl: string |
ba430d75 C |
25 | private serverConfig: ServerConfig |
26 | ||
52d9f792 | 27 | constructor ( |
52d9f792 | 28 | private serverService: ServerService, |
66357162 | 29 | private notifier: Notifier |
123f6193 | 30 | ) { } |
52d9f792 | 31 | |
ba430d75 C |
32 | ngOnInit (): void { |
33 | this.serverConfig = this.serverService.getTmpConfig() | |
34 | this.serverService.getConfig() | |
35 | .subscribe(config => this.serverConfig = config) | |
36 | } | |
37 | ||
1ea7da81 RK |
38 | ngOnChanges (changes: SimpleChanges) { |
39 | if (changes['actor']) { | |
40 | this.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.actor) | |
41 | } | |
42 | } | |
43 | ||
44 | onAvatarChange (input: HTMLInputElement) { | |
45 | this.avatarfileInput = new ElementRef(input) | |
46 | ||
52d9f792 C |
47 | const avatarfile = this.avatarfileInput.nativeElement.files[ 0 ] |
48 | if (avatarfile.size > this.maxAvatarSize) { | |
1ea7da81 | 49 | this.notifier.error('Error', $localize`This image is too large.`) |
52d9f792 C |
50 | return |
51 | } | |
52 | ||
53 | const formData = new FormData() | |
54 | formData.append('avatarfile', avatarfile) | |
1ea7da81 | 55 | this.avatarPopover?.close() |
52d9f792 C |
56 | this.avatarChange.emit(formData) |
57 | } | |
58 | ||
1ea7da81 RK |
59 | deleteAvatar () { |
60 | this.avatarDelete.emit() | |
61 | } | |
62 | ||
52d9f792 | 63 | get maxAvatarSize () { |
ba430d75 | 64 | return this.serverConfig.avatar.file.size.max |
52d9f792 C |
65 | } |
66 | ||
e61151b0 | 67 | get maxAvatarSizeInBytes () { |
b4c3c51d | 68 | return getBytes(this.maxAvatarSize) |
e61151b0 RK |
69 | } |
70 | ||
52d9f792 | 71 | get avatarExtensions () { |
482fa503 | 72 | return this.serverConfig.avatar.file.extensions.join(', ') |
52d9f792 | 73 | } |
123f6193 K |
74 | |
75 | get avatarFormat () { | |
76 | return `${$localize`max size`}: 192*192px, ${this.maxAvatarSizeInBytes} ${$localize`extensions`}: ${this.avatarExtensions}` | |
77 | } | |
1ea7da81 RK |
78 | |
79 | get hasAvatar () { | |
80 | return !!this.avatarUrl | |
81 | } | |
52d9f792 | 82 | } |