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
24 @Input() displayFilename = false
25 @Input() displayReset = false
27 @Input() icon: GlobalIconName
28 @Input() buttonTooltip: string
30 @Output() fileChanged = new EventEmitter<Blob>()
32 allowedExtensionsMessage = ''
37 constructor (private notifier: Notifier) { }
40 if (!this.file) return ''
46 this.allowedExtensionsMessage = this.extensions.join(', ')
49 fileChange (event: any) {
50 if (event.target.files?.length) {
51 const [ file ] = event.target.files
53 if (file.size > this.maxFileSize) {
54 this.notifier.error($localize`This file is too large.`)
58 const extension = '.' + file.name.split('.').pop()
59 if (this.extensions.includes(extension.toLowerCase()) === false) {
60 const message = $localize`PeerTube cannot handle this kind of file. Accepted extensions are ${this.allowedExtensionsMessage}.`
61 this.notifier.error(message)
68 this.propagateChange(this.file)
70 this.fileChanged.emit(this.file)
74 this.writeValue(undefined)
75 this.propagateChange(undefined)
76 this.fileChanged.emit(undefined)
79 propagateChange = (_: any) => { /* empty */ }
81 writeValue (file: any) {
84 if (!this.file) this.fileInputValue = null
87 registerOnChange (fn: (_: any) => void) {
88 this.propagateChange = fn
91 registerOnTouched () {