support for basic mime types (#2229)

This commit is contained in:
Denys Vuika
2017-08-18 13:34:15 +01:00
committed by Eugenio Romano
parent 4aa3377641
commit 731ce5fa8a
3 changed files with 134 additions and 8 deletions

View File

@@ -16,7 +16,9 @@
*/
import { Component, Inject, OnInit, ViewEncapsulation } from '@angular/core';
import { Http, Response } from '@angular/http';
import { MD_DIALOG_DATA, MdDialogRef } from '@angular/material';
import { Observable } from 'rxjs/Rx';
import { ViewerDialogSettings } from './viewer-dialog.settings';
@@ -38,25 +40,52 @@ export class ViewerDialogComponent implements OnInit {
unknownFormatText = 'Document preview could not be loaded.';
viewerType: string = null;
asText: Observable<string>;
private types = [
{ mimeType: 'application/x-javascript', type: 'text' },
{ mimeType: 'application/pdf', type: 'pdf' }
];
constructor(private dialogRef: MdDialogRef<ViewerDialogComponent>,
@Inject(MD_DIALOG_DATA) settings: ViewerDialogSettings) {
@Inject(MD_DIALOG_DATA) settings: ViewerDialogSettings,
private http: Http) {
this.fileUrl = settings.fileUrl;
this.fileName = settings.fileName;
this.fileMimeType = settings.fileMimeType;
this.downloadUrl = settings.downloadUrl;
// console.log(settings);
}
ngOnInit() {
this.viewerType = this.detectViewerType(this.fileMimeType);
this.asText = this.getAsText();
}
private detectViewerType(mimeType: string) {
if (mimeType) {
mimeType = mimeType.toLowerCase();
if (mimeType.startsWith('image/')) {
return 'image';
}
if (mimeType.startsWith('text/')) {
return 'text';
}
if (mimeType.startsWith('video/')) {
return 'video';
}
if (mimeType.startsWith('audio/')) {
return 'audio';
}
const registered = this.types.find(t => t.mimeType === mimeType);
if (registered) {
return registered.type;
}
}
return 'unknown';
}
@@ -75,6 +104,10 @@ export class ViewerDialogComponent implements OnInit {
}
}
private getAsText(): Observable<string> {
return this.http.get(this.fileUrl).map((res: Response) => res.text());
}
close() {
this.dialogRef.close(true);
}