diff options
Diffstat (limited to 'client/src/app/shared/shared-forms/preview-upload.component.ts')
-rw-r--r-- | client/src/app/shared/shared-forms/preview-upload.component.ts | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/client/src/app/shared/shared-forms/preview-upload.component.ts b/client/src/app/shared/shared-forms/preview-upload.component.ts new file mode 100644 index 000000000..7519734ba --- /dev/null +++ b/client/src/app/shared/shared-forms/preview-upload.component.ts | |||
@@ -0,0 +1,92 @@ | |||
1 | import { Component, forwardRef, Input, OnInit } from '@angular/core' | ||
2 | import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms' | ||
3 | import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser' | ||
4 | import { ServerService } from '@app/core' | ||
5 | import { ServerConfig } from '@shared/models' | ||
6 | import { BytesPipe } from 'ngx-pipes' | ||
7 | import { I18n } from '@ngx-translate/i18n-polyfill' | ||
8 | |||
9 | @Component({ | ||
10 | selector: 'my-preview-upload', | ||
11 | styleUrls: [ './preview-upload.component.scss' ], | ||
12 | templateUrl: './preview-upload.component.html', | ||
13 | providers: [ | ||
14 | { | ||
15 | provide: NG_VALUE_ACCESSOR, | ||
16 | useExisting: forwardRef(() => PreviewUploadComponent), | ||
17 | multi: true | ||
18 | } | ||
19 | ] | ||
20 | }) | ||
21 | export class PreviewUploadComponent implements OnInit, ControlValueAccessor { | ||
22 | @Input() inputLabel: string | ||
23 | @Input() inputName: string | ||
24 | @Input() previewWidth: string | ||
25 | @Input() previewHeight: string | ||
26 | |||
27 | imageSrc: SafeResourceUrl | ||
28 | allowedExtensionsMessage = '' | ||
29 | maxSizeText: string | ||
30 | |||
31 | private serverConfig: ServerConfig | ||
32 | private bytesPipe: BytesPipe | ||
33 | private file: Blob | ||
34 | |||
35 | constructor ( | ||
36 | private sanitizer: DomSanitizer, | ||
37 | private serverService: ServerService, | ||
38 | private i18n: I18n | ||
39 | ) { | ||
40 | this.bytesPipe = new BytesPipe() | ||
41 | this.maxSizeText = this.i18n('max size') | ||
42 | } | ||
43 | |||
44 | get videoImageExtensions () { | ||
45 | return this.serverConfig.video.image.extensions | ||
46 | } | ||
47 | |||
48 | get maxVideoImageSize () { | ||
49 | return this.serverConfig.video.image.size.max | ||
50 | } | ||
51 | |||
52 | get maxVideoImageSizeInBytes () { | ||
53 | return this.bytesPipe.transform(this.maxVideoImageSize) | ||
54 | } | ||
55 | |||
56 | ngOnInit () { | ||
57 | this.serverConfig = this.serverService.getTmpConfig() | ||
58 | this.serverService.getConfig() | ||
59 | .subscribe(config => this.serverConfig = config) | ||
60 | |||
61 | this.allowedExtensionsMessage = this.videoImageExtensions.join(', ') | ||
62 | } | ||
63 | |||
64 | onFileChanged (file: Blob) { | ||
65 | this.file = file | ||
66 | |||
67 | this.propagateChange(this.file) | ||
68 | this.updatePreview() | ||
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 | } | ||