fix blob input in text viewer (#1942)

This commit is contained in:
Eugenio Romano
2017-06-07 16:26:33 +01:00
committed by Eugenio Romano
parent d3eee53ee4
commit c6bfe892ee
6 changed files with 60 additions and 22 deletions

View File

@@ -72,7 +72,7 @@ export class AboutComponent implements OnInit {
if (corePackage) { if (corePackage) {
let commitIsh = corePackage.version.split('-'); let commitIsh = corePackage.version.split('-');
if (commitIsh.length > 1) { if (commitIsh.length > 1) {
this.githubUrlCommitAlpha = this.githubUrlCommitAlpha + commitIsh; this.githubUrlCommitAlpha = this.githubUrlCommitAlpha + commitIsh[1];
} else { } else {
this.githubUrlCommitAlpha = this.githubUrlCommitAlpha + corePackage.version; this.githubUrlCommitAlpha = this.githubUrlCommitAlpha + corePackage.version;
} }

View File

@@ -29,7 +29,7 @@ export class ImgViewerComponent implements OnChanges {
urlFile: string; urlFile: string;
@Input() @Input()
blobFile: any; blobFile: Blob;
@Input() @Input()
nameFile: string; nameFile: string;

View File

@@ -3,6 +3,7 @@
overflow: auto; overflow: auto;
font-size: 1em; font-size: 1em;
padding: 20px; padding: 20px;
max-width: 70%;
} }
.full_width{ .full_width{

View File

@@ -52,16 +52,16 @@ describe('Test ng2-alfresco-viewer Text View component', () => {
debug = fixture.debugElement; debug = fixture.debugElement;
element = fixture.nativeElement; element = fixture.nativeElement;
component = fixture.componentInstance; component = fixture.componentInstance;
component.urlFile = require('../assets/fake-test-file.txt');
}); });
describe('View', () => { describe('View', () => {
it('Should text container be present with urlfile', (done) => { it('Should text container be present with urlfile', (done) => {
fixture.detectChanges(); fixture.detectChanges();
let change = new SimpleChange(null, null, true); let urlFile = require('../assets/fake-test-file.txt');
let change = new SimpleChange(null, urlFile, true);
component.ngOnChanges(change).then(() => { component.ngOnChanges({ 'urlFile': change }).then(() => {
fixture.detectChanges(); fixture.detectChanges();
fixture.whenStable().then(() => { fixture.whenStable().then(() => {
expect(element.querySelector('#adf-viewer-text-container').textContent).toContain('example'); expect(element.querySelector('#adf-viewer-text-container').textContent).toContain('example');
@@ -69,5 +69,20 @@ describe('Test ng2-alfresco-viewer Text View component', () => {
}); });
}); });
}); });
it('Should text container be present with Blob file', (done) => {
let blobFile = new Blob(['text example'], {type: 'text/txt'});
let change = new SimpleChange(null, blobFile, true);
component.ngOnChanges({ 'blobFile': change }).then(() => {
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(element.querySelector('#adf-viewer-text-container').textContent).toContain('example');
done();
});
});
});
}); });
}); });

View File

@@ -16,8 +16,7 @@
*/ */
import { Component, Input } from '@angular/core'; import { Component, Input } from '@angular/core';
import { SimpleChange } from '@angular/core'; import { SimpleChanges } from '@angular/core';
import { ContentService } from 'ng2-alfresco-core';
import { Http, Response, RequestOptions, ResponseContentType } from '@angular/http'; import { Http, Response, RequestOptions, ResponseContentType } from '@angular/http';
import 'rxjs/add/operator/toPromise'; import 'rxjs/add/operator/toPromise';
@@ -32,36 +31,59 @@ export class TxtViewerComponent {
urlFile: any; urlFile: any;
@Input() @Input()
blobFile: any; blobFile: Blob;
content: string; 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']; let blobFile = changes['blobFile'];
if (blobFile && blobFile.currentValue) { 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) { if (!this.urlFile && !this.blobFile) {
throw new Error('Attribute urlFile or blobFile is required'); throw new Error('Attribute urlFile or blobFile is required');
} }
}
private getUrlContent(url: string): Promise<any> {
return new Promise((resolve, reject) => { 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 { private readBlob(blob: Blob): Promise<any> {
this.http.get(this.urlFile, new RequestOptions({ return new Promise((resolve, reject) => {
responseType: ResponseContentType.Text let reader = new FileReader();
})).toPromise().then(
(res: Response) => { reader.onload = () => {
this.content = res.text(); this.content = reader.result;
resolve(); resolve();
}, (event) => { };
reject(event);
}); reader.onerror = (error: ErrorEvent) => {
reject(error);
};
reader.readAsText(blob);
});
} }
} }

View File

@@ -239,7 +239,7 @@ export class ViewerComponent {
* @returns {boolean} * @returns {boolean}
*/ */
private isText(): boolean { private isText(): boolean {
return this.extension === 'txt' || this.mimeType === 'text/txt'; return this.extension === 'txt' || this.mimeType === 'text/txt' || this.mimeType === 'text/plain';
} }
/** /**