tests for DataFileServiceImpl
This commit is contained in:
parent
527e108d2e
commit
1142519139
3 changed files with 185 additions and 185 deletions
|
@ -1,181 +1,181 @@
|
||||||
/**
|
// /**
|
||||||
* Service which provides helper methods for downloading a data file from a URL, and extracting
|
// * Service which provides helper methods for downloading a data file from a URL, and extracting
|
||||||
* its contents as .tar, .tar.gz, or .zip file. Note that this service depends on external
|
// * its contents as .tar, .tar.gz, or .zip file. Note that this service depends on external
|
||||||
* library code in the lib/ directory:
|
// * library code in the lib/ directory:
|
||||||
* - jszip.min.js
|
// * - jszip.min.js
|
||||||
* - Blob.js
|
// * - Blob.js
|
||||||
* - zlib.js
|
// * - zlib.js
|
||||||
*/
|
// */
|
||||||
angular.module('quay').factory('DataFileService', [function() {
|
// angular.module('quay').factory('DataFileService', [function() {
|
||||||
var dataFileService = {};
|
// var dataFileService = {};
|
||||||
|
//
|
||||||
dataFileService.getName_ = function(filePath) {
|
// dataFileService.getName_ = function(filePath) {
|
||||||
var parts = filePath.split('/');
|
// var parts = filePath.split('/');
|
||||||
return parts[parts.length - 1];
|
// return parts[parts.length - 1];
|
||||||
};
|
// };
|
||||||
|
//
|
||||||
dataFileService.tryAsZip_ = function(buf, success, failure) {
|
// dataFileService.tryAsZip_ = function(buf, success, failure) {
|
||||||
var zip = null;
|
// var zip = null;
|
||||||
var zipFiles = null;
|
// var zipFiles = null;
|
||||||
try {
|
// try {
|
||||||
var zip = new JSZip(buf);
|
// var zip = new JSZip(buf);
|
||||||
zipFiles = zip.files;
|
// zipFiles = zip.files;
|
||||||
} catch (e) {
|
// } catch (e) {
|
||||||
failure();
|
// failure();
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
var files = [];
|
// var files = [];
|
||||||
for (var filePath in zipFiles) {
|
// for (var filePath in zipFiles) {
|
||||||
if (zipFiles.hasOwnProperty(filePath)) {
|
// if (zipFiles.hasOwnProperty(filePath)) {
|
||||||
files.push({
|
// files.push({
|
||||||
'name': dataFileService.getName_(filePath),
|
// 'name': dataFileService.getName_(filePath),
|
||||||
'path': filePath,
|
// 'path': filePath,
|
||||||
'canRead': true,
|
// 'canRead': true,
|
||||||
'toBlob': (function(fp) {
|
// 'toBlob': (function(fp) {
|
||||||
return function() {
|
// return function() {
|
||||||
return new Blob([zip.file(fp).asArrayBuffer()]);
|
// return new Blob([zip.file(fp).asArrayBuffer()]);
|
||||||
};
|
// };
|
||||||
}(filePath))
|
// }(filePath))
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
success(files);
|
// success(files);
|
||||||
};
|
// };
|
||||||
|
//
|
||||||
dataFileService.tryAsTarGz_ = function(buf, success, failure) {
|
// dataFileService.tryAsTarGz_ = function(buf, success, failure) {
|
||||||
var gunzip = new Zlib.Gunzip(new Uint8Array(buf));
|
// var gunzip = new Zlib.Gunzip(new Uint8Array(buf));
|
||||||
var plain = null;
|
// var plain = null;
|
||||||
|
//
|
||||||
try {
|
// try {
|
||||||
plain = gunzip.decompress();
|
// plain = gunzip.decompress();
|
||||||
} catch (e) {
|
// } catch (e) {
|
||||||
failure();
|
// failure();
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
if (plain.byteLength == 0) {
|
// if (plain.byteLength == 0) {
|
||||||
plain = buf;
|
// plain = buf;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
dataFileService.tryAsTar_(plain, success, failure);
|
// dataFileService.tryAsTar_(plain, success, failure);
|
||||||
};
|
// };
|
||||||
|
//
|
||||||
dataFileService.tryAsTar_ = function(buf, success, failure) {
|
// dataFileService.tryAsTar_ = function(buf, success, failure) {
|
||||||
var collapsePath = function(originalPath) {
|
// var collapsePath = function(originalPath) {
|
||||||
// Tar files can contain entries of the form './', so we need to collapse
|
// // Tar files can contain entries of the form './', so we need to collapse
|
||||||
// those paths down.
|
// // those paths down.
|
||||||
var parts = originalPath.split('/');
|
// var parts = originalPath.split('/');
|
||||||
for (var i = parts.length - 1; i >= 0; i--) {
|
// for (var i = parts.length - 1; i >= 0; i--) {
|
||||||
var part = parts[i];
|
// var part = parts[i];
|
||||||
if (part == '.') {
|
// if (part == '.') {
|
||||||
parts.splice(i, 1);
|
// parts.splice(i, 1);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
return parts.join('/');
|
// return parts.join('/');
|
||||||
};
|
// };
|
||||||
|
//
|
||||||
try {
|
// try {
|
||||||
var handler = new Untar(new Uint8Array(buf));
|
// var handler = new Untar(new Uint8Array(buf));
|
||||||
handler.process(function(status, read, files, err) {
|
// handler.process(function(status, read, files, err) {
|
||||||
switch (status) {
|
// switch (status) {
|
||||||
case 'error':
|
// case 'error':
|
||||||
failure(err);
|
// failure(err);
|
||||||
break;
|
// break;
|
||||||
|
//
|
||||||
case 'done':
|
// case 'done':
|
||||||
var processed = [];
|
// var processed = [];
|
||||||
for (var i = 0; i < files.length; ++i) {
|
// for (var i = 0; i < files.length; ++i) {
|
||||||
var currentFile = files[i];
|
// var currentFile = files[i];
|
||||||
var path = collapsePath(currentFile.meta.filename);
|
// var path = collapsePath(currentFile.meta.filename);
|
||||||
|
//
|
||||||
if (path == '' || path == 'pax_global_header') { continue; }
|
// if (path == '' || path == 'pax_global_header') { continue; }
|
||||||
|
//
|
||||||
processed.push({
|
// processed.push({
|
||||||
'name': dataFileService.getName_(path),
|
// 'name': dataFileService.getName_(path),
|
||||||
'path': path,
|
// 'path': path,
|
||||||
'canRead': true,
|
// 'canRead': true,
|
||||||
'toBlob': (function(currentFile) {
|
// 'toBlob': (function(currentFile) {
|
||||||
return function() {
|
// return function() {
|
||||||
return new Blob([currentFile.buffer], {type: 'application/octet-binary'});
|
// return new Blob([currentFile.buffer], {type: 'application/octet-binary'});
|
||||||
};
|
// };
|
||||||
}(currentFile))
|
// }(currentFile))
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
success(processed);
|
// success(processed);
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
} catch (e) {
|
// } catch (e) {
|
||||||
failure();
|
// failure();
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
};
|
// };
|
||||||
|
//
|
||||||
dataFileService.blobToString = function(blob, callback) {
|
// dataFileService.blobToString = function(blob, callback) {
|
||||||
var reader = new FileReader();
|
// var reader = new FileReader();
|
||||||
reader.onload = function(event){
|
// reader.onload = function(event){
|
||||||
callback(reader.result);
|
// callback(reader.result);
|
||||||
};
|
// };
|
||||||
reader.readAsText(blob);
|
// reader.readAsText(blob);
|
||||||
};
|
// };
|
||||||
|
//
|
||||||
dataFileService.arrayToString = function(buf, callback) {
|
// dataFileService.arrayToString = function(buf, callback) {
|
||||||
var bb = new Blob([buf], {type: 'application/octet-binary'});
|
// var bb = new Blob([buf], {type: 'application/octet-binary'});
|
||||||
var f = new FileReader();
|
// var f = new FileReader();
|
||||||
f.onload = function(e) {
|
// f.onload = function(e) {
|
||||||
callback(e.target.result);
|
// callback(e.target.result);
|
||||||
};
|
// };
|
||||||
f.onerror = function(e) {
|
// f.onerror = function(e) {
|
||||||
callback(null);
|
// callback(null);
|
||||||
};
|
// };
|
||||||
f.onabort = function(e) {
|
// f.onabort = function(e) {
|
||||||
callback(null);
|
// callback(null);
|
||||||
};
|
// };
|
||||||
f.readAsText(bb);
|
// f.readAsText(bb);
|
||||||
};
|
// };
|
||||||
|
//
|
||||||
dataFileService.readDataArrayAsPossibleArchive = function(buf, success, failure) {
|
// dataFileService.readDataArrayAsPossibleArchive = function(buf, success, failure) {
|
||||||
dataFileService.tryAsZip_(buf, success, function() {
|
// dataFileService.tryAsZip_(buf, success, function() {
|
||||||
dataFileService.tryAsTarGz_(buf, success, function() {
|
// dataFileService.tryAsTarGz_(buf, success, function() {
|
||||||
dataFileService.tryAsTar_(buf, success, failure);
|
// dataFileService.tryAsTar_(buf, success, failure);
|
||||||
});
|
// });
|
||||||
});
|
// });
|
||||||
};
|
// };
|
||||||
|
//
|
||||||
dataFileService.downloadDataFileAsArrayBuffer = function($scope, url, progress, error, loaded) {
|
// dataFileService.downloadDataFileAsArrayBuffer = function($scope, url, progress, error, loaded) {
|
||||||
var request = new XMLHttpRequest();
|
// var request = new XMLHttpRequest();
|
||||||
request.open('GET', url, true);
|
// request.open('GET', url, true);
|
||||||
request.responseType = 'arraybuffer';
|
// request.responseType = 'arraybuffer';
|
||||||
|
//
|
||||||
request.onprogress = function(e) {
|
// request.onprogress = function(e) {
|
||||||
$scope.$apply(function() {
|
// $scope.$apply(function() {
|
||||||
var percentLoaded;
|
// var percentLoaded;
|
||||||
if (e.lengthComputable) {
|
// if (e.lengthComputable) {
|
||||||
progress(e.loaded / e.total);
|
// progress(e.loaded / e.total);
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
};
|
// };
|
||||||
|
//
|
||||||
request.onerror = function() {
|
// request.onerror = function() {
|
||||||
$scope.$apply(function() {
|
// $scope.$apply(function() {
|
||||||
error();
|
// error();
|
||||||
});
|
// });
|
||||||
};
|
// };
|
||||||
|
//
|
||||||
request.onload = function() {
|
// request.onload = function() {
|
||||||
if (this.status == 200) {
|
// if (this.status == 200) {
|
||||||
$scope.$apply(function() {
|
// $scope.$apply(function() {
|
||||||
var uint8array = new Uint8Array(request.response);
|
// var uint8array = new Uint8Array(request.response);
|
||||||
loaded(uint8array);
|
// loaded(uint8array);
|
||||||
});
|
// });
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
};
|
// };
|
||||||
|
//
|
||||||
request.send();
|
// request.send();
|
||||||
};
|
// };
|
||||||
|
//
|
||||||
return dataFileService;
|
// return dataFileService;
|
||||||
}]);
|
// }]);
|
||||||
|
|
|
@ -115,10 +115,10 @@ describe("DataFileServiceImpl", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("readDataArrayAsPossibleArchive", () => {
|
describe("readDataArrayAsPossibleArchive", () => {
|
||||||
|
// TODO
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("downloadDataFileAsArrayBuffer", () => {
|
describe("downloadDataFileAsArrayBuffer", () => {
|
||||||
|
// TODO
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -9,7 +9,7 @@ declare const Untar: (uint8Array: Uint8Array) => void;
|
||||||
export class DataFileServiceImpl implements DataFileService {
|
export class DataFileServiceImpl implements DataFileService {
|
||||||
|
|
||||||
constructor(private fileReaderFactory: () => FileReader) {
|
constructor(private fileReaderFactory: () => FileReader) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public blobToString(blob: Blob, callback: (result: string) => void): void {
|
public blobToString(blob: Blob, callback: (result: string) => void): void {
|
||||||
|
|
Reference in a new issue