refactoring DatafileService
This commit is contained in:
parent
40e6540074
commit
b72cf7c04b
2 changed files with 160 additions and 0 deletions
|
@ -0,0 +1,26 @@
|
|||
import { DatafileServiceImpl } from './datafile.service.impl';
|
||||
|
||||
|
||||
describe("DatafileServiceImpl", () => {
|
||||
var datafileServiceImpl: DatafileServiceImpl;
|
||||
|
||||
beforeEach(() => {
|
||||
datafileServiceImpl = new DatafileServiceImpl();
|
||||
});
|
||||
|
||||
describe("blobToString", () => {
|
||||
|
||||
});
|
||||
|
||||
describe("arrayToString", () => {
|
||||
|
||||
});
|
||||
|
||||
describe("readDataArrayAsPossibleArchive", () => {
|
||||
|
||||
});
|
||||
|
||||
describe("downloadDataFileAsArrayBuffer", () => {
|
||||
|
||||
});
|
||||
});
|
|
@ -0,0 +1,134 @@
|
|||
import { DatafileService } from './datafile.service';
|
||||
declare const JSZip: (buf: any) => void;
|
||||
declare const Zlib: any;
|
||||
declare const Untar: (uint8Array: Uint8Array) => void;
|
||||
|
||||
|
||||
export class DatafileServiceImpl implements DatafileService {
|
||||
|
||||
public blobToString(blob: any, callback: (result: string) => void): void {
|
||||
|
||||
}
|
||||
|
||||
public arrayToString(buf: any, callback: (result: string) => void): void {
|
||||
|
||||
}
|
||||
|
||||
public readDataArrayAsPossibleArchive(buf: any,
|
||||
success: (result: any) => void,
|
||||
failure: (error: any) => void): void {
|
||||
|
||||
}
|
||||
|
||||
public downloadDataFileAsArrayBuffer($scope: ng.IScope,
|
||||
url: string,
|
||||
progress: (percent: number) => void,
|
||||
error: () => void,
|
||||
loaded: (uint8array: Uint8Array) => void): void {
|
||||
|
||||
}
|
||||
|
||||
private getName(filePath: string): string {
|
||||
var parts: string[] = filePath.split('/');
|
||||
|
||||
return parts[parts.length - 1];
|
||||
}
|
||||
|
||||
private tryAsZip(buf: any, success: (result: any) => void, failure: (error?: any) => void): void {
|
||||
var zip = null;
|
||||
var zipFiles = null;
|
||||
try {
|
||||
var zip = new JSZip(buf);
|
||||
zipFiles = zip.files;
|
||||
} catch (e) {
|
||||
failure();
|
||||
return;
|
||||
}
|
||||
|
||||
var files = [];
|
||||
for (var filePath in zipFiles) {
|
||||
if (zipFiles.hasOwnProperty(filePath)) {
|
||||
files.push({
|
||||
'name': this.getName(filePath),
|
||||
'path': filePath,
|
||||
'canRead': true,
|
||||
'toBlob': (function(fp) {
|
||||
return function() {
|
||||
return new Blob([zip.file(fp).asArrayBuffer()]);
|
||||
};
|
||||
}(filePath))
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
success(files);
|
||||
}
|
||||
|
||||
private tryAsTarGz(buf: any, success: (result: any) => void, failure: (error?: any) => void): void {
|
||||
var gunzip = new Zlib.Gunzip(new Uint8Array(buf));
|
||||
var plain = null;
|
||||
|
||||
try {
|
||||
plain = gunzip.decompress();
|
||||
} catch (e) {
|
||||
failure();
|
||||
return;
|
||||
}
|
||||
|
||||
if (plain.byteLength == 0) {
|
||||
plain = buf;
|
||||
}
|
||||
|
||||
this.tryAsTar(plain, success, failure);
|
||||
}
|
||||
|
||||
private tryAsTar(buf: any, success: (result: any) => void, failure: (error?: any) => void): void {
|
||||
var collapsePath = function(originalPath) {
|
||||
// Tar files can contain entries of the form './', so we need to collapse
|
||||
// those paths down.
|
||||
var parts = originalPath.split('/');
|
||||
for (var i = parts.length - 1; i >= 0; i--) {
|
||||
var part = parts[i];
|
||||
if (part == '.') {
|
||||
parts.splice(i, 1);
|
||||
}
|
||||
}
|
||||
return parts.join('/');
|
||||
};
|
||||
|
||||
try {
|
||||
var handler = new Untar(new Uint8Array(buf));
|
||||
handler.process((status, read, files, err) => {
|
||||
switch (status) {
|
||||
case 'error':
|
||||
failure(err);
|
||||
break;
|
||||
|
||||
case 'done':
|
||||
var processed = [];
|
||||
for (var i = 0; i < files.length; ++i) {
|
||||
var currentFile = files[i];
|
||||
var path = collapsePath(currentFile.meta.filename);
|
||||
|
||||
if (path == '' || path == 'pax_global_header') { continue; }
|
||||
|
||||
processed.push({
|
||||
'name': this.getName(path),
|
||||
'path': path,
|
||||
'canRead': true,
|
||||
'toBlob': (function(currentFile) {
|
||||
return function() {
|
||||
return new Blob([currentFile.buffer], {type: 'application/octet-binary'});
|
||||
};
|
||||
}(currentFile))
|
||||
});
|
||||
}
|
||||
success(processed);
|
||||
break;
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
failure();
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue