Add download button for displayFilesAsHTML files

+ Added bytes attribute for "file objects" in untar and unzip
+ Added download button on files displayed by displayFilesAsHTML
feature-extract-files
toby 2017-03-12 12:51:04 -04:00
parent 94ea086e05
commit 78abacdadc
2 changed files with 40 additions and 12 deletions

View File

@ -1028,20 +1028,43 @@ var Utils = {
}; };
var formatFile = function(file, i) { var formatFile = function(file, i) {
var blob = new Blob(
[new Uint8Array(file.bytes)],
{type: "octet/stream"}
);
var blobUrl = window.URL.createObjectURL(blob);
var downloadAnchorElem = $("<a></a>")
.html("\u21B4")
.attr("href", blobUrl)
.attr("title", "Download '" + file.fileName + "'")
.attr("download", file.fileName);
var expandFileContentsElem = $("<a></a>")
.html("&#x1F50D")
.addClass("collapsed")
.attr("data-toggle", "collapse")
.attr("aria-expanded", "true")
.attr("aria-controls", "collapse" + i)
.attr("href", "#collapse" + i)
.attr("title", "Show/hide contents of '" + file.fileName + "'");
var html = "<div class='panel panel-default'>" + var html = "<div class='panel panel-default'>" +
"<div class='panel-heading' role='tab' id='heading" + i + "'>" + "<div class='panel-heading' role='tab' id='heading" + i + "'>" +
"<h4 class='panel-title'>" + "<h4 class='panel-title'>" +
"<a class='collapsed' role='button' data-toggle='collapse' " + "<div>" +
"href='#collapse" + i + "' " +
"aria-expanded='true' aria-controls='collapse" + i +"'>" +
file.fileName + file.fileName +
" " +
$(expandFileContentsElem).prop("outerHTML") +
" " +
$(downloadAnchorElem).prop("outerHTML") +
"<span class='pull-right'>" + "<span class='pull-right'>" +
// These are for formatting when stripping HTML // These are for formatting when stripping HTML
"<span style='display: none'>\n</span>" + "<span style='display: none'>\n</span>" +
file.size.toLocaleString() + " bytes" + file.size.toLocaleString() + " bytes" +
"<span style='display: none'>\n</span>" + "<span style='display: none'>\n</span>" +
"</span>" + "</span>" +
"</a>" + "</div>" +
"</h4>" + "</h4>" +
"</div>" + "</div>" +
"<div id='collapse" + i + "' class='panel-collapse collapse' " + "<div id='collapse" + i + "' class='panel-collapse collapse' " +

View File

@ -309,9 +309,8 @@ var Compress = {
files = []; files = [];
filenames.forEach(function(fileName) { filenames.forEach(function(fileName) {
var contents = unzip.decompress(fileName); var bytes = unzip.decompress(fileName);
var contents = Utils.byteArrayToUtf8(bytes);
contents = Utils.byteArrayToUtf8(contents);
var file = { var file = {
fileName: fileName, fileName: fileName,
@ -320,6 +319,7 @@ var Compress = {
var isDir = contents.length === 0 && fileName.endsWith("/"); var isDir = contents.length === 0 && fileName.endsWith("/");
if (!isDir) { if (!isDir) {
file.bytes = bytes;
file.contents = contents; file.contents = contents;
} }
@ -477,6 +477,13 @@ var Compress = {
this.position = 0; this.position = 0;
}; };
Stream.prototype.getBytes = function(bytesToGet) {
var newPosition = this.position + bytesToGet;
var bytes = this.bytes.slice(this.position, newPosition);
this.position = newPosition;
return bytes;
};
Stream.prototype.readString = function(numBytes) { Stream.prototype.readString = function(numBytes) {
var result = ""; var result = "";
for (var i = this.position; i < this.position + numBytes; i++) { for (var i = this.position; i < this.position + numBytes; i++) {
@ -535,11 +542,9 @@ var Compress = {
endPosition += 512 - (file.size % 512); endPosition += 512 - (file.size % 512);
} }
file.contents = ""; file.bytes = stream.getBytes(file.size);
file.contents = Utils.byteArrayToUtf8(file.bytes);
while (stream.position < endPosition) { stream.position = endPosition;
file.contents += stream.readString(512);
}
} else if (file.type === "5") { } else if (file.type === "5") {
// Directory // Directory
files.push(file); files.push(file);