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'
5 import { I18n } from '@ngx-translate/i18n-polyfill'
8 selector: 'my-reactive-file',
9 styleUrls: [ './reactive-file.component.scss' ],
10 templateUrl: './reactive-file.component.html',
13 provide: NG_VALUE_ACCESSOR,
14 useExisting: forwardRef(() => ReactiveFileComponent),
19 export class ReactiveFileComponent implements OnInit, ControlValueAccessor {
20 @Input() inputLabel: string
21 @Input() inputName: string
22 @Input() extensions: string[] = []
23 @Input() maxFileSize: number
24 @Input() displayFilename = false
25 @Input() icon: GlobalIconName
27 @Output() fileChanged = new EventEmitter<Blob>()
29 allowedExtensionsMessage = ''
35 private notifier: Notifier,
40 if (!this.file) return ''
46 this.allowedExtensionsMessage = this.extensions.join(', ')
49 fileChange (event: any) {
50 if (event.target.files && event.target.files.length) {
51 const [ file ] = event.target.files
53 if (file.size > this.maxFileSize) {
54 this.notifier.error(this.i18n('This file is too large.'))
58 const extension = '.' + file.name.split('.').pop()
59 if (this.extensions.includes(extension) === false) {
60 const message = this.i18n(
61 'PeerTube cannot handle this kind of file. Accepted extensions are {{extensions}}.',
62 { extensions: this.allowedExtensionsMessage }
64 this.notifier.error(message)
71 this.propagateChange(this.file)
72 this.fileChanged.emit(this.file)
76 propagateChange = (_: any) => { /* empty */ }
78 writeValue (file: any) {
81 if (!this.file) this.fileInputValue = null
84 registerOnChange (fn: (_: any) => void) {
85 this.propagateChange = fn
88 registerOnTouched () {