1 import { Component, EventEmitter, forwardRef, Input, OnInit, Output } from '@angular/core'
2 import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
3 import { Notifier } from '@app/core'
4 import { GlobalIconName } from '@app/shared/shared-icons'
7 selector: 'my-reactive-file',
8 styleUrls: [ './reactive-file.component.scss' ],
9 templateUrl: './reactive-file.component.html',
12 provide: NG_VALUE_ACCESSOR,
13 useExisting: forwardRef(() => ReactiveFileComponent),
18 export class ReactiveFileComponent implements OnInit, ControlValueAccessor {
19 @Input() inputLabel: string
20 @Input() inputName: string
21 @Input() extensions: string[] = []
22 @Input() maxFileSize: number
23 @Input() displayFilename = false
24 @Input() icon: GlobalIconName
26 @Output() fileChanged = new EventEmitter<Blob>()
28 allowedExtensionsMessage = ''
33 constructor (private notifier: Notifier) { }
36 if (!this.file) return ''
42 this.allowedExtensionsMessage = this.extensions.join(', ')
45 fileChange (event: any) {
46 if (event.target.files && event.target.files.length) {
47 const [ file ] = event.target.files
49 if (file.size > this.maxFileSize) {
50 this.notifier.error($localize`This file is too large.`)
54 const extension = '.' + file.name.split('.').pop()
55 if (this.extensions.includes(extension.toLowerCase()) === false) {
56 const message = $localize`PeerTube cannot handle this kind of file. Accepted extensions are ${this.allowedExtensionsMessage}}.`
57 this.notifier.error(message)
64 this.propagateChange(this.file)
65 this.fileChanged.emit(this.file)
69 propagateChange = (_: any) => { /* empty */ }
71 writeValue (file: any) {
74 if (!this.file) this.fileInputValue = null
77 registerOnChange (fn: (_: any) => void) {
78 this.propagateChange = fn
81 registerOnTouched () {