From b72cf7c04bd9fc31b4ef4537eba563a3abf54827 Mon Sep 17 00:00:00 2001 From: alecmerdler Date: Sat, 11 Mar 2017 15:47:44 -0800 Subject: [PATCH] refactoring DatafileService --- .../datafile/datafile.service.impl.spec.ts | 26 ++++ .../datafile/datafile.service.impl.ts | 134 ++++++++++++++++++ 2 files changed, 160 insertions(+) diff --git a/static/js/services/datafile/datafile.service.impl.spec.ts b/static/js/services/datafile/datafile.service.impl.spec.ts index e69de29bb..afdd14f6c 100644 --- a/static/js/services/datafile/datafile.service.impl.spec.ts +++ b/static/js/services/datafile/datafile.service.impl.spec.ts @@ -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", () => { + + }); +}); \ No newline at end of file diff --git a/static/js/services/datafile/datafile.service.impl.ts b/static/js/services/datafile/datafile.service.impl.ts index e69de29bb..14e611a73 100644 --- a/static/js/services/datafile/datafile.service.impl.ts +++ b/static/js/services/datafile/datafile.service.impl.ts @@ -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(); + } + } +} \ No newline at end of file