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