]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-forms/reactive-file.component.ts
Migrate to $localize
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-forms / reactive-file.component.ts
CommitLineData
40e87e9e
C
1import { Component, EventEmitter, forwardRef, Input, OnInit, Output } from '@angular/core'
2import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
f8b2c1b4 3import { Notifier } from '@app/core'
67ed6552 4import { GlobalIconName } from '@app/shared/shared-icons'
40e87e9e
C
5
6@Component({
7 selector: 'my-reactive-file',
8 styleUrls: [ './reactive-file.component.scss' ],
9 templateUrl: './reactive-file.component.html',
10 providers: [
11 {
12 provide: NG_VALUE_ACCESSOR,
13 useExisting: forwardRef(() => ReactiveFileComponent),
14 multi: true
15 }
16 ]
17})
18export 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
7b992a86 24 @Input() icon: GlobalIconName
40e87e9e
C
25
26 @Output() fileChanged = new EventEmitter<Blob>()
27
28 allowedExtensionsMessage = ''
772d5642 29 fileInputValue: any
40e87e9e
C
30
31 private file: File
32
66357162 33 constructor (private notifier: Notifier) { }
40e87e9e
C
34
35 get filename () {
36 if (!this.file) return ''
37
38 return this.file.name
39 }
40
41 ngOnInit () {
42 this.allowedExtensionsMessage = this.extensions.join(', ')
43 }
44
45 fileChange (event: any) {
46 if (event.target.files && event.target.files.length) {
47 const [ file ] = event.target.files
48
49 if (file.size > this.maxFileSize) {
66357162 50 this.notifier.error($localize`This file is too large.`)
40e87e9e
C
51 return
52 }
53
b5487ff4
C
54 const extension = '.' + file.name.split('.').pop()
55 if (this.extensions.includes(extension) === false) {
66357162 56 const message = $localize`PeerTube cannot handle this kind of file. Accepted extensions are ${this.allowedExtensionsMessage}}.`
b5487ff4
C
57 this.notifier.error(message)
58
59 return
60 }
61
40e87e9e
C
62 this.file = file
63
64 this.propagateChange(this.file)
65 this.fileChanged.emit(this.file)
66 }
67 }
68
69 propagateChange = (_: any) => { /* empty */ }
70
71 writeValue (file: any) {
72 this.file = file
772d5642
C
73
74 if (!this.file) this.fileInputValue = null
40e87e9e
C
75 }
76
77 registerOnChange (fn: (_: any) => void) {
78 this.propagateChange = fn
79 }
80
81 registerOnTouched () {
82 // Unused
83 }
84}