]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-actor-image/actor-avatar-edit.component.ts
Set channel banner/avatar in creation form
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-actor-image / actor-avatar-edit.component.ts
CommitLineData
cdeddff1 1import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
27ec473f 2import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser'
67ed6552 3import { Notifier, ServerService } from '@app/core'
cdeddff1 4import { Account, VideoChannel } from '@app/shared/shared-main'
0e4ca570 5import { NgbPopover } from '@ng-bootstrap/ng-bootstrap'
b4c3c51d 6import { getBytes } from '@root-helpers/bytes'
52d9f792
C
7
8@Component({
cdeddff1
C
9 selector: 'my-actor-avatar-edit',
10 templateUrl: './actor-avatar-edit.component.html',
11 styleUrls: [
12 './actor-image-edit.scss',
13 './actor-avatar-edit.component.scss'
14 ]
52d9f792 15})
cdeddff1 16export class ActorAvatarEditComponent implements OnInit {
2f5d2ec5 17 @ViewChild('avatarfileInput') avatarfileInput: ElementRef<HTMLInputElement>
1ea7da81 18 @ViewChild('avatarPopover') avatarPopover: NgbPopover
52d9f792
C
19
20 @Input() actor: VideoChannel | Account
cdeddff1
C
21 @Input() editable = true
22 @Input() displaySubscribers = true
23 @Input() displayUsername = true
27ec473f 24 @Input() previewImage = false
52d9f792
C
25
26 @Output() avatarChange = new EventEmitter<FormData>()
1ea7da81 27 @Output() avatarDelete = new EventEmitter<void>()
52d9f792 28
0e4ca570
C
29 avatarFormat = ''
30 maxAvatarSize = 0
31 avatarExtensions = ''
32
27ec473f
C
33 preview: SafeResourceUrl
34
52d9f792 35 constructor (
27ec473f 36 private sanitizer: DomSanitizer,
52d9f792 37 private serverService: ServerService,
66357162 38 private notifier: Notifier
123f6193 39 ) { }
52d9f792 40
ba430d75 41 ngOnInit (): void {
ba430d75 42 this.serverService.getConfig()
0e4ca570
C
43 .subscribe(config => {
44 this.maxAvatarSize = config.avatar.file.size.max
45 this.avatarExtensions = config.avatar.file.extensions.join(', ')
46
47 this.avatarFormat = `${$localize`max size`}: 192*192px, ` +
48 `${getBytes(this.maxAvatarSize)} ${$localize`extensions`}: ${this.avatarExtensions}`
49 })
ba430d75
C
50 }
51
1ea7da81
RK
52 onAvatarChange (input: HTMLInputElement) {
53 this.avatarfileInput = new ElementRef(input)
54
52d9f792
C
55 const avatarfile = this.avatarfileInput.nativeElement.files[ 0 ]
56 if (avatarfile.size > this.maxAvatarSize) {
1ea7da81 57 this.notifier.error('Error', $localize`This image is too large.`)
52d9f792
C
58 return
59 }
60
61 const formData = new FormData()
62 formData.append('avatarfile', avatarfile)
1ea7da81 63 this.avatarPopover?.close()
52d9f792 64 this.avatarChange.emit(formData)
27ec473f
C
65
66 if (this.previewImage) {
67 this.preview = this.sanitizer.bypassSecurityTrustResourceUrl(URL.createObjectURL(avatarfile))
68 }
52d9f792
C
69 }
70
1ea7da81 71 deleteAvatar () {
27ec473f 72 this.preview = undefined
1ea7da81
RK
73 this.avatarDelete.emit()
74 }
75
0e4ca570 76 hasAvatar () {
27ec473f 77 return !!this.preview || !!this.actor.avatar
1ea7da81 78 }
deb8b9cd
C
79
80 isChannel () {
81 return !!(this.actor as VideoChannel).ownerAccount
82 }
52d9f792 83}