]>
Commit | Line | Data |
---|---|---|
7b992a86 | 1 | import { Component, forwardRef, Input, OnInit } from '@angular/core' |
702785a5 | 2 | import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms' |
702785a5 | 3 | import { ServerService } from '@app/core' |
0ea2f79d | 4 | import { imageToDataURL } from '@root-helpers/images' |
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 | ||
0ea2f79d | 26 | imageSrc: string |
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 ( | |
66357162 | 35 | private serverService: ServerService |
6f02515e RK |
36 | ) { |
37 | this.bytesPipe = new BytesPipe() | |
66357162 | 38 | this.maxSizeText = $localize`max size` |
6f02515e | 39 | } |
702785a5 C |
40 | |
41 | get videoImageExtensions () { | |
ba430d75 | 42 | return this.serverConfig.video.image.extensions |
702785a5 C |
43 | } |
44 | ||
45 | get maxVideoImageSize () { | |
ba430d75 | 46 | return this.serverConfig.video.image.size.max |
702785a5 C |
47 | } |
48 | ||
6f02515e RK |
49 | get maxVideoImageSizeInBytes () { |
50 | return this.bytesPipe.transform(this.maxVideoImageSize) | |
51 | } | |
52 | ||
7b992a86 | 53 | ngOnInit () { |
2989628b | 54 | this.serverConfig = this.serverService.getHTMLConfig() |
ba430d75 | 55 | |
7b992a86 C |
56 | this.allowedExtensionsMessage = this.videoImageExtensions.join(', ') |
57 | } | |
58 | ||
be27ef3b | 59 | onFileChanged (file: Blob) { |
40e87e9e | 60 | this.file = file |
0c237b19 | 61 | |
40e87e9e C |
62 | this.propagateChange(this.file) |
63 | this.updatePreview() | |
702785a5 C |
64 | } |
65 | ||
66 | propagateChange = (_: any) => { /* empty */ } | |
67 | ||
68 | writeValue (file: any) { | |
69 | this.file = file | |
70 | this.updatePreview() | |
71 | } | |
72 | ||
73 | registerOnChange (fn: (_: any) => void) { | |
74 | this.propagateChange = fn | |
75 | } | |
76 | ||
77 | registerOnTouched () { | |
78 | // Unused | |
79 | } | |
80 | ||
81 | private updatePreview () { | |
82 | if (this.file) { | |
0ea2f79d | 83 | imageToDataURL(this.file).then(result => this.imageSrc = result) |
702785a5 C |
84 | } |
85 | } | |
86 | } |