1
0
Fork 0
peertube/client/src/app/shared/images/preview-upload.component.ts

81 lines
1.9 KiB
TypeScript
Raw Normal View History

2019-05-17 08:45:53 +00:00
import { Component, forwardRef, Input, OnInit } from '@angular/core'
2018-02-16 18:30:58 +00:00
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser'
import { ServerService } from '@app/core'
2019-12-18 14:31:54 +00:00
import { ServerConfig } from '@shared/models'
2018-02-16 18:30:58 +00:00
@Component({
2019-05-17 08:45:53 +00:00
selector: 'my-preview-upload',
styleUrls: [ './preview-upload.component.scss' ],
templateUrl: './preview-upload.component.html',
2018-02-16 18:30:58 +00:00
providers: [
{
provide: NG_VALUE_ACCESSOR,
2019-05-17 08:45:53 +00:00
useExisting: forwardRef(() => PreviewUploadComponent),
2018-02-16 18:30:58 +00:00
multi: true
}
]
})
2019-05-17 08:45:53 +00:00
export class PreviewUploadComponent implements OnInit, ControlValueAccessor {
2018-02-16 18:30:58 +00:00
@Input() inputLabel: string
@Input() inputName: string
@Input() previewWidth: string
@Input() previewHeight: string
imageSrc: SafeResourceUrl
2019-05-17 08:45:53 +00:00
allowedExtensionsMessage = ''
2018-02-16 18:30:58 +00:00
2019-12-18 14:31:54 +00:00
private serverConfig: ServerConfig
2018-07-12 17:02:00 +00:00
private file: File
2018-02-16 18:30:58 +00:00
constructor (
private sanitizer: DomSanitizer,
2018-07-12 17:02:00 +00:00
private serverService: ServerService
2018-02-16 18:30:58 +00:00
) {}
get videoImageExtensions () {
2019-12-18 14:31:54 +00:00
return this.serverConfig.video.image.extensions
2018-02-16 18:30:58 +00:00
}
get maxVideoImageSize () {
2019-12-18 14:31:54 +00:00
return this.serverConfig.video.image.size.max
2018-02-16 18:30:58 +00:00
}
2019-05-17 08:45:53 +00:00
ngOnInit () {
2019-12-18 14:31:54 +00:00
this.serverConfig = this.serverService.getTmpConfig()
this.serverService.getConfig()
.subscribe(config => this.serverConfig = config)
2019-05-17 08:45:53 +00:00
this.allowedExtensionsMessage = this.videoImageExtensions.join(', ')
}
2018-07-12 17:02:00 +00:00
onFileChanged (file: File) {
this.file = file
2018-06-22 13:42:55 +00:00
2018-07-12 17:02:00 +00:00
this.propagateChange(this.file)
this.updatePreview()
2018-02-16 18:30:58 +00:00
}
propagateChange = (_: any) => { /* empty */ }
writeValue (file: any) {
this.file = file
this.updatePreview()
}
registerOnChange (fn: (_: any) => void) {
this.propagateChange = fn
}
registerOnTouched () {
// Unused
}
private updatePreview () {
if (this.file) {
const url = URL.createObjectURL(this.file)
this.imageSrc = this.sanitizer.bypassSecurityTrustResourceUrl(url)
}
}
}