]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-forms/preview-upload.component.ts
fix video download modal select width
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-forms / preview-upload.component.ts
CommitLineData
7b992a86 1import { Component, forwardRef, Input, OnInit } from '@angular/core'
702785a5
C
2import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
3import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser'
4import { ServerService } from '@app/core'
94676e63
C
5import { ServerConfig } from '@shared/models'
6import { BytesPipe } from '../shared-main'
702785a5
C
7
8@Component({
7b992a86
C
9 selector: 'my-preview-upload',
10 styleUrls: [ './preview-upload.component.scss' ],
11 templateUrl: './preview-upload.component.html',
702785a5
C
12 providers: [
13 {
14 provide: NG_VALUE_ACCESSOR,
7b992a86 15 useExisting: forwardRef(() => PreviewUploadComponent),
702785a5
C
16 multi: true
17 }
18 ]
19})
7b992a86 20export class PreviewUploadComponent implements OnInit, ControlValueAccessor {
702785a5
C
21 @Input() inputLabel: string
22 @Input() inputName: string
23 @Input() previewWidth: string
24 @Input() previewHeight: string
25
26 imageSrc: SafeResourceUrl
7b992a86 27 allowedExtensionsMessage = ''
6f02515e 28 maxSizeText: string
702785a5 29
ba430d75 30 private serverConfig: ServerConfig
6f02515e 31 private bytesPipe: BytesPipe
be27ef3b 32 private file: Blob
702785a5
C
33
34 constructor (
35 private sanitizer: DomSanitizer,
66357162 36 private serverService: ServerService
6f02515e
RK
37 ) {
38 this.bytesPipe = new BytesPipe()
66357162 39 this.maxSizeText = $localize`max size`
6f02515e 40 }
702785a5
C
41
42 get videoImageExtensions () {
ba430d75 43 return this.serverConfig.video.image.extensions
702785a5
C
44 }
45
46 get maxVideoImageSize () {
ba430d75 47 return this.serverConfig.video.image.size.max
702785a5
C
48 }
49
6f02515e
RK
50 get maxVideoImageSizeInBytes () {
51 return this.bytesPipe.transform(this.maxVideoImageSize)
52 }
53
7b992a86 54 ngOnInit () {
ba430d75
C
55 this.serverConfig = this.serverService.getTmpConfig()
56 this.serverService.getConfig()
57 .subscribe(config => this.serverConfig = config)
58
7b992a86
C
59 this.allowedExtensionsMessage = this.videoImageExtensions.join(', ')
60 }
61
be27ef3b 62 onFileChanged (file: Blob) {
40e87e9e 63 this.file = file
0c237b19 64
40e87e9e
C
65 this.propagateChange(this.file)
66 this.updatePreview()
702785a5
C
67 }
68
69 propagateChange = (_: any) => { /* empty */ }
70
71 writeValue (file: any) {
72 this.file = file
73 this.updatePreview()
74 }
75
76 registerOnChange (fn: (_: any) => void) {
77 this.propagateChange = fn
78 }
79
80 registerOnTouched () {
81 // Unused
82 }
83
84 private updatePreview () {
85 if (this.file) {
86 const url = URL.createObjectURL(this.file)
87 this.imageSrc = this.sanitizer.bypassSecurityTrustResourceUrl(url)
88 }
89 }
90}