mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-08-14 17:58:44 +00:00
fix blob input in text viewer (#1942)
This commit is contained in:
committed by
Eugenio Romano
parent
d3eee53ee4
commit
c6bfe892ee
@@ -16,8 +16,7 @@
|
||||
*/
|
||||
|
||||
import { Component, Input } from '@angular/core';
|
||||
import { SimpleChange } from '@angular/core';
|
||||
import { ContentService } from 'ng2-alfresco-core';
|
||||
import { SimpleChanges } from '@angular/core';
|
||||
import { Http, Response, RequestOptions, ResponseContentType } from '@angular/http';
|
||||
import 'rxjs/add/operator/toPromise';
|
||||
|
||||
@@ -32,36 +31,59 @@ export class TxtViewerComponent {
|
||||
urlFile: any;
|
||||
|
||||
@Input()
|
||||
blobFile: any;
|
||||
blobFile: Blob;
|
||||
|
||||
content: string;
|
||||
|
||||
constructor(private http: Http, private contentService: ContentService) {
|
||||
constructor(private http: Http) {
|
||||
}
|
||||
|
||||
ngOnChanges(changes: SimpleChange) {
|
||||
ngOnChanges(changes: SimpleChanges): Promise<any> {
|
||||
|
||||
let blobFile = changes['blobFile'];
|
||||
if (blobFile && blobFile.currentValue) {
|
||||
this.urlFile = this.contentService.createTrustedUrl(this.blobFile);
|
||||
return this.readBlob(blobFile.currentValue);
|
||||
}
|
||||
|
||||
let urlFile = changes['urlFile'];
|
||||
if (urlFile && urlFile.currentValue) {
|
||||
return this.getUrlContent(urlFile.currentValue);
|
||||
}
|
||||
|
||||
if (!this.urlFile && !this.blobFile) {
|
||||
throw new Error('Attribute urlFile or blobFile is required');
|
||||
}
|
||||
}
|
||||
|
||||
private getUrlContent(url: string): Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.getUrlContent(resolve, reject);
|
||||
|
||||
this.http.get(url, new RequestOptions({
|
||||
responseType: ResponseContentType.Text
|
||||
})).toPromise().then(
|
||||
(res: Response) => {
|
||||
this.content = res.text();
|
||||
resolve();
|
||||
}, (event) => {
|
||||
reject(event);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private getUrlContent(resolve, reject): void {
|
||||
this.http.get(this.urlFile, new RequestOptions({
|
||||
responseType: ResponseContentType.Text
|
||||
})).toPromise().then(
|
||||
(res: Response) => {
|
||||
this.content = res.text();
|
||||
private readBlob(blob: Blob): Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let reader = new FileReader();
|
||||
|
||||
reader.onload = () => {
|
||||
this.content = reader.result;
|
||||
resolve();
|
||||
}, (event) => {
|
||||
reject(event);
|
||||
});
|
||||
};
|
||||
|
||||
reader.onerror = (error: ErrorEvent) => {
|
||||
reject(error);
|
||||
};
|
||||
|
||||
reader.readAsText(blob);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user