]>
Commit | Line | Data |
---|---|---|
7b992a86 | 1 | import { Component, forwardRef, Input, OnInit } from '@angular/core' |
702785a5 C |
2 | import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms' |
3 | import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser' | |
4 | import { ServerService } from '@app/core' | |
2989628b | 5 | import { HTMLServerConfig } from '@shared/models' |
94676e63 | 6 | import { BytesPipe } from '../shared-main' |
702785a5 C |
7 | |
8 | @Component({ | |
7b992a86 C |
9 | selector: 'my-preview-upload', |
10 | styleUrls: [ './preview-upload.component.scss' ], | |
11 | templateUrl: './preview-upload.component.html', | |
702785a5 C |
12 | providers: [ |
13 | { | |
14 | provide: NG_VALUE_ACCESSOR, | |
7b992a86 | 15 | useExisting: forwardRef(() => PreviewUploadComponent), |
702785a5 C |
16 | multi: true |
17 | } | |
18 | ] | |
19 | }) | |
7b992a86 | 20 | export class PreviewUploadComponent implements OnInit, ControlValueAccessor { |
702785a5 C |
21 | @Input() inputLabel: string |
22 | @Input() inputName: string | |
23 | @Input() previewWidth: string | |
24 | @Input() previewHeight: string | |
25 | ||
26 | imageSrc: SafeResourceUrl | |
7b992a86 | 27 | allowedExtensionsMessage = '' |
6f02515e | 28 | maxSizeText: string |
702785a5 | 29 | |
2989628b | 30 | private serverConfig: HTMLServerConfig |
6f02515e | 31 | private bytesPipe: BytesPipe |
be27ef3b | 32 | private file: Blob |
702785a5 C |
33 | |
34 | constructor ( | |
35 | private sanitizer: DomSanitizer, | |
66357162 | 36 | private serverService: ServerService |
6f02515e RK |
37 | ) { |
38 | this.bytesPipe = new BytesPipe() | |
66357162 | 39 | this.maxSizeText = $localize`max size` |
6f02515e | 40 | } |
702785a5 C |
41 | |
42 | get videoImageExtensions () { | |
ba430d75 | 43 | return this.serverConfig.video.image.extensions |
702785a5 C |
44 | } |
45 | ||
46 | get maxVideoImageSize () { | |
ba430d75 | 47 | return this.serverConfig.video.image.size.max |
702785a5 C |
48 | } |
49 | ||
6f02515e RK |
50 | get maxVideoImageSizeInBytes () { |
51 | return this.bytesPipe.transform(this.maxVideoImageSize) | |
52 | } | |
53 | ||
7b992a86 | 54 | ngOnInit () { |
2989628b | 55 | this.serverConfig = this.serverService.getHTMLConfig() |
ba430d75 | 56 | |
7b992a86 C |
57 | this.allowedExtensionsMessage = this.videoImageExtensions.join(', ') |
58 | } | |
59 | ||
be27ef3b | 60 | onFileChanged (file: Blob) { |
40e87e9e | 61 | this.file = file |
0c237b19 | 62 | |
40e87e9e C |
63 | this.propagateChange(this.file) |
64 | this.updatePreview() | |
702785a5 C |
65 | } |
66 | ||
67 | propagateChange = (_: any) => { /* empty */ } | |
68 | ||
69 | writeValue (file: any) { | |
70 | this.file = file | |
71 | this.updatePreview() | |
72 | } | |
73 | ||
74 | registerOnChange (fn: (_: any) => void) { | |
75 | this.propagateChange = fn | |
76 | } | |
77 | ||
78 | registerOnTouched () { | |
79 | // Unused | |
80 | } | |
81 | ||
82 | private updatePreview () { | |
83 | if (this.file) { | |
84 | const url = URL.createObjectURL(this.file) | |
85 | this.imageSrc = this.sanitizer.bypassSecurityTrustResourceUrl(url) | |
86 | } | |
87 | } | |
88 | } |