]>
Commit | Line | Data |
---|---|---|
1 | import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core' | |
2 | import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser' | |
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' | |
7 | ||
8 | @Component({ | |
9 | selector: 'my-actor-banner-edit', | |
10 | templateUrl: './actor-banner-edit.component.html', | |
11 | styleUrls: [ | |
12 | './actor-image-edit.scss', | |
13 | './actor-banner-edit.component.scss' | |
14 | ] | |
15 | }) | |
16 | export class ActorBannerEditComponent implements OnInit { | |
17 | @ViewChild('bannerfileInput') bannerfileInput: ElementRef<HTMLInputElement> | |
18 | @ViewChild('bannerPopover') bannerPopover: NgbPopover | |
19 | ||
20 | @Input() actor: VideoChannel | |
21 | @Input() previewImage = false | |
22 | ||
23 | @Output() bannerChange = new EventEmitter<FormData>() | |
24 | @Output() bannerDelete = new EventEmitter<void>() | |
25 | ||
26 | bannerFormat = '' | |
27 | maxBannerSize = 0 | |
28 | bannerExtensions = '' | |
29 | ||
30 | preview: SafeResourceUrl | |
31 | ||
32 | constructor ( | |
33 | private sanitizer: DomSanitizer, | |
34 | private serverService: ServerService, | |
35 | private notifier: Notifier | |
36 | ) { } | |
37 | ||
38 | ngOnInit (): void { | |
39 | const config = this.serverService.getHTMLConfig() | |
40 | this.maxBannerSize = config.banner.file.size.max | |
41 | this.bannerExtensions = config.banner.file.extensions.join(', ') | |
42 | ||
43 | /* eslint-disable max-len */ | |
44 | this.bannerFormat = $localize`ratio 6/1, recommended size: 1920x317, max size: ${getBytes(this.maxBannerSize)}, extensions: ${this.bannerExtensions}` | |
45 | } | |
46 | ||
47 | onBannerChange (input: HTMLInputElement) { | |
48 | this.bannerfileInput = new ElementRef(input) | |
49 | ||
50 | const bannerfile = this.bannerfileInput.nativeElement.files[0] | |
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) | |
60 | ||
61 | if (this.previewImage) { | |
62 | this.preview = this.sanitizer.bypassSecurityTrustResourceUrl(URL.createObjectURL(bannerfile)) | |
63 | } | |
64 | } | |
65 | ||
66 | deleteBanner () { | |
67 | this.preview = undefined | |
68 | this.bannerDelete.emit() | |
69 | } | |
70 | ||
71 | hasBanner () { | |
72 | return !!this.preview || !!this.actor.bannerUrl | |
73 | } | |
74 | } |