PeerTube/client/src/app/shared/shared-actor-image-edit/actor-avatar-edit.component.ts

96 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-04-07 17:01:29 +02:00
import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser'
2020-06-23 14:10:17 +02:00
import { Notifier, ServerService } from '@app/core'
2021-04-07 17:01:29 +02:00
import { Account, VideoChannel } from '@app/shared/shared-main'
2021-01-13 10:52:17 +01:00
import { NgbPopover } from '@ng-bootstrap/ng-bootstrap'
2020-08-14 17:28:54 +02:00
import { getBytes } from '@root-helpers/bytes'
@Component({
2021-04-07 17:01:29 +02:00
selector: 'my-actor-avatar-edit',
templateUrl: './actor-avatar-edit.component.html',
styleUrls: [
'./actor-image-edit.scss',
'./actor-avatar-edit.component.scss'
]
})
2021-04-07 17:01:29 +02:00
export class ActorAvatarEditComponent implements OnInit {
2020-02-07 10:00:34 +01:00
@ViewChild('avatarfileInput') avatarfileInput: ElementRef<HTMLInputElement>
@ViewChild('avatarPopover') avatarPopover: NgbPopover
@Input() actor: VideoChannel | Account
2021-04-07 17:01:29 +02:00
@Input() editable = true
@Input() displaySubscribers = true
@Input() displayUsername = true
@Input() previewImage = false
@Output() avatarChange = new EventEmitter<FormData>()
@Output() avatarDelete = new EventEmitter<void>()
2021-01-13 10:52:17 +01:00
avatarFormat = ''
maxAvatarSize = 0
avatarExtensions = ''
preview: SafeResourceUrl
constructor (
private sanitizer: DomSanitizer,
private serverService: ServerService,
private notifier: Notifier
) { }
2019-12-18 15:31:54 +01:00
ngOnInit (): void {
this.serverService.getConfig()
2021-01-13 10:52:17 +01:00
.subscribe(config => {
this.maxAvatarSize = config.avatar.file.size.max
this.avatarExtensions = config.avatar.file.extensions.join(', ')
this.avatarFormat = `${$localize`max size`}: 192*192px, ` +
`${getBytes(this.maxAvatarSize)} ${$localize`extensions`}: ${this.avatarExtensions}`
})
2019-12-18 15:31:54 +01:00
}
onAvatarChange (input: HTMLInputElement) {
this.avatarfileInput = new ElementRef(input)
const avatarfile = this.avatarfileInput.nativeElement.files[ 0 ]
if (avatarfile.size > this.maxAvatarSize) {
this.notifier.error('Error', $localize`This image is too large.`)
return
}
const formData = new FormData()
formData.append('avatarfile', avatarfile)
this.avatarPopover?.close()
this.avatarChange.emit(formData)
if (this.previewImage) {
this.preview = this.sanitizer.bypassSecurityTrustResourceUrl(URL.createObjectURL(avatarfile))
}
}
deleteAvatar () {
this.preview = undefined
this.avatarDelete.emit()
}
2021-01-13 10:52:17 +01:00
hasAvatar () {
return !!this.preview || !!this.actor.avatar
}
2021-03-29 16:45:35 +02:00
isChannel () {
return !!(this.actor as VideoChannel).ownerAccount
}
2021-04-28 11:49:34 +02:00
getChannel (): VideoChannel {
if (this.isChannel()) return this.actor as VideoChannel
return undefined
}
getAccount (): Account {
if (this.isChannel()) return undefined
return this.actor as Account
}
}