--- /dev/null
+<div class="root">
+ <div>
+ <div class="button-file">
+ <span>{{ inputLabel }}</span>
+ <input
+ type="file"
+ [name]="inputName" [id]="inputName" [accept]="videoImageExtensions"
+ (change)="fileChange($event)"
+ />
+ </div>
+ <div class="image-constraints">(extensions: {{ videoImageExtensions }}, max size: {{ maxVideoImageSize | bytes }})</div>
+ </div>
+
+ <img *ngIf="imageSrc" [ngStyle]="{ width: previewWidth, height: previewHeight }" [src]="imageSrc" class="preview" />
+ <div *ngIf="!imageSrc" [ngStyle]="{ width: previewWidth, height: previewHeight }" class="preview no-image"></div>
+</div>
--- /dev/null
+import { Component, forwardRef, Input } from '@angular/core'
+import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
+import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser'
+import { ServerService } from '@app/core'
+import 'rxjs/add/observable/forkJoin'
+
+@Component({
+ selector: 'my-video-image',
+ styleUrls: [ './video-image.component.scss' ],
+ templateUrl: './video-image.component.html',
+ providers: [
+ {
+ provide: NG_VALUE_ACCESSOR,
+ useExisting: forwardRef(() => VideoImageComponent),
+ multi: true
+ }
+ ]
+})
+
+export class VideoImageComponent implements ControlValueAccessor {
+ @Input() inputLabel: string
+ @Input() inputName: string
+ @Input() previewWidth: string
+ @Input() previewHeight: string
+
+ imageSrc: SafeResourceUrl
+
+ private file: Blob
+
+ constructor (
+ private sanitizer: DomSanitizer,
+ private serverService: ServerService
+ ) {}
+
+ get videoImageExtensions () {
+ return this.serverService.getConfig().video.image.extensions.join(',')
+ }
+
+ get maxVideoImageSize () {
+ return this.serverService.getConfig().video.image.size.max
+ }
+
+ fileChange (event: any) {
+ if (event.target.files && event.target.files.length) {
+ const [ file ] = event.target.files
+
+ this.file = file
+ this.propagateChange(this.file)
+ this.updatePreview()
+ }
+ }
+
+ propagateChange = (_: any) => { /* empty */ }
+
+ writeValue (file: any) {
+ this.file = file
+ this.updatePreview()
+ }
+
+ registerOnChange (fn: (_: any) => void) {
+ this.propagateChange = fn
+ }
+
+ registerOnTouched () {
+ // Unused
+ }
+
+ private updatePreview () {
+ if (this.file) {
+ const url = URL.createObjectURL(this.file)
+ this.imageSrc = this.sanitizer.bypassSecurityTrustResourceUrl(url)
+ }
+ }
+}