]>
Commit | Line | Data |
---|---|---|
2989628b | 1 | import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core' |
0ea2f79d | 2 | import { SafeResourceUrl } from '@angular/platform-browser' |
cdeddff1 C |
3 | import { Notifier, ServerService } from '@app/core' |
4 | import { VideoChannel } from '@app/shared/shared-main' | |
5 | import { NgbPopover } from '@ng-bootstrap/ng-bootstrap' | |
6 | import { getBytes } from '@root-helpers/bytes' | |
0ea2f79d | 7 | import { imageToDataURL } from '@root-helpers/images' |
cdeddff1 C |
8 | |
9 | @Component({ | |
10 | selector: 'my-actor-banner-edit', | |
11 | templateUrl: './actor-banner-edit.component.html', | |
12 | styleUrls: [ | |
13 | './actor-image-edit.scss', | |
14 | './actor-banner-edit.component.scss' | |
15 | ] | |
16 | }) | |
17 | export class ActorBannerEditComponent implements OnInit { | |
18 | @ViewChild('bannerfileInput') bannerfileInput: ElementRef<HTMLInputElement> | |
19 | @ViewChild('bannerPopover') bannerPopover: NgbPopover | |
20 | ||
21 | @Input() actor: VideoChannel | |
27ec473f | 22 | @Input() previewImage = false |
cdeddff1 C |
23 | |
24 | @Output() bannerChange = new EventEmitter<FormData>() | |
25 | @Output() bannerDelete = new EventEmitter<void>() | |
26 | ||
27 | bannerFormat = '' | |
28 | maxBannerSize = 0 | |
29 | bannerExtensions = '' | |
30 | ||
27ec473f C |
31 | preview: SafeResourceUrl |
32 | ||
cdeddff1 C |
33 | constructor ( |
34 | private serverService: ServerService, | |
35 | private notifier: Notifier | |
36 | ) { } | |
37 | ||
38 | ngOnInit (): void { | |
2989628b C |
39 | const config = this.serverService.getHTMLConfig() |
40 | this.maxBannerSize = config.banner.file.size.max | |
41 | this.bannerExtensions = config.banner.file.extensions.join(', ') | |
cdeddff1 | 42 | |
9df52d66 | 43 | /* eslint-disable max-len */ |
2989628b | 44 | this.bannerFormat = $localize`ratio 6/1, recommended size: 1920x317, max size: ${getBytes(this.maxBannerSize)}, extensions: ${this.bannerExtensions}` |
cdeddff1 C |
45 | } |
46 | ||
47 | onBannerChange (input: HTMLInputElement) { | |
48 | this.bannerfileInput = new ElementRef(input) | |
49 | ||
9df52d66 | 50 | const bannerfile = this.bannerfileInput.nativeElement.files[0] |
cdeddff1 C |
51 | if (bannerfile.size > this.maxBannerSize) { |
52 | this.notifier.error('Error', $localize`This image is too large.`) | |
53 | return | |
54 | } | |
55 | ||
56 | const formData = new FormData() | |
57 | formData.append('bannerfile', bannerfile) | |
58 | this.bannerPopover?.close() | |
59 | this.bannerChange.emit(formData) | |
27ec473f C |
60 | |
61 | if (this.previewImage) { | |
0ea2f79d | 62 | imageToDataURL(bannerfile).then(result => this.preview = result) |
27ec473f | 63 | } |
cdeddff1 C |
64 | } |
65 | ||
66 | deleteBanner () { | |
27ec473f | 67 | this.preview = undefined |
cdeddff1 C |
68 | this.bannerDelete.emit() |
69 | } | |
70 | ||
71 | hasBanner () { | |
27ec473f | 72 | return !!this.preview || !!this.actor.bannerUrl |
cdeddff1 C |
73 | } |
74 | } |