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

94 lines
2.6 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'
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'
2021-09-08 10:10:51 +02:00
import { imageToDataURL } from '@root-helpers/images'
@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 = ''
2021-09-08 10:10:51 +02:00
preview: string
constructor (
private serverService: ServerService,
private notifier: Notifier
) { }
2019-12-18 15:31:54 +01:00
ngOnInit (): void {
2021-06-04 13:31:41 +02:00
const config = this.serverService.getHTMLConfig()
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)
2021-08-17 14:42:53 +02:00
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) {
2021-09-08 10:10:51 +02:00
imageToDataURL(avatarfile).then(result => this.preview = result)
}
}
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
}
}