Skip to content

Commit 5e4b61c

Browse files
committed
[cmd:quicklook] optimize the Archive(zip|gzip|tar) preview plugin
1 parent daa3e7e commit 5e4b61c

File tree

1 file changed

+65
-57
lines changed

1 file changed

+65
-57
lines changed

js/commands/quicklook.plugins.js

Lines changed: 65 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -676,14 +676,60 @@ elFinder.prototype.commands.quicklook.plugins = [
676676
function(ql) {
677677
var mimes = ['application/zip', 'application/x-gzip', 'application/x-tar'],
678678
preview = ql.preview,
679-
fm = ql.fm;
679+
fm = ql.fm,
680+
tarFiles = function(tar) {
681+
var filenames = [],
682+
tarlen = tar.length,
683+
offset = 0,
684+
toStr = function(arr) {
685+
return String.fromCharCode.apply(null, arr).replace(/\0+$/, '');
686+
},
687+
h, name, prefix, size, dbs, toStr;
688+
while (offset < tarlen && tar[offset] !== 0) {
689+
h = tar.subarray(offset, offset + 512);
690+
name = toStr(h.subarray(0, 100));
691+
if (prefix = toStr(h.subarray(345, 500))) {
692+
name = prefix + name;
693+
}
694+
size = parseInt(toStr(h.subarray(124, 136)), 8);
695+
dbs = Math.ceil(size / 512) * 512;
696+
if (name === '././@LongLink') {
697+
name = toStr(tar.subarray(offset + 512, offset + 512 + dbs));
698+
}
699+
(name !== 'pax_global_header') && filenames.push(name);
700+
offset = offset + 512 + dbs;
701+
}
702+
return filenames;
703+
};
680704

681-
if (typeof Uint8Array !== 'undefined' && elFinder.Zlib) {
705+
if (typeof Uint8Array !== 'undefined') {
682706
preview.on('update', function(e) {
683707
var file = e.file,
684-
doc, xhr, loading, url;
708+
doc, xhr, loading, url,
709+
makeList = function(filenames) {
710+
var header, doc;
711+
if (filenames && filenames.length) {
712+
filenames = $.map(filenames, function(str) {
713+
return fm.decodeRawString(str);
714+
});
715+
filenames.sort();
716+
loading.remove();
717+
header = '<strong>'+fm.escape(file.mime)+'</strong> ('+fm.formatSize(file.size)+')'+'<hr/>'
718+
doc = $('<div class="elfinder-quicklook-preview-archive-wrapper">'+header+'<pre class="elfinder-quicklook-preview-text">'+fm.escape(filenames.join("\n"))+'</pre></div>')
719+
.on('touchstart', function(e) {
720+
if ($(this)['scroll' + (fm.direction === 'ltr'? 'Right' : 'Left')]() > 5) {
721+
e.originalEvent._preventSwipeX = true;
722+
}
723+
})
724+
.appendTo(preview);
725+
ql.hideinfo();
726+
}
727+
};
685728

686-
if ($.inArray(file.mime, mimes) !== -1) {
729+
if ($.inArray(file.mime, mimes) !== -1 && (
730+
file.mime === 'application/x-tar'
731+
|| (elFinder.Zlib && (file.mime === 'application/zip' || file.mime === 'application/x-gzip'))
732+
)) {
687733
// this is our file - stop event propagation
688734
e.stopImmediatePropagation();
689735

@@ -697,61 +743,23 @@ elFinder.prototype.commands.quicklook.plugins = [
697743

698744
xhr = new XMLHttpRequest();
699745
xhr.onload = function(e) {
700-
var filenames = [], header, unzip, tar, tarlen, offset, h, name, prefix, size, dbs, toStr;
746+
var unzip, filenames;
701747
if (this.readyState === 4 && this.response) {
702-
setTimeout(function() {
703-
try {
704-
if (file.mime === 'application/zip') {
705-
unzip = new elFinder.Zlib.Unzip(new Uint8Array(xhr.response));
706-
filenames = unzip.getFilenames();
707-
} else {
708-
if (file.mime === 'application/x-gzip') {
709-
unzip = new elFinder.Zlib.Gunzip(new Uint8Array(xhr.response));
710-
tar = unzip.decompress();
711-
} else {
712-
tar = new Uint8Array(xhr.response);
713-
}
714-
tarlen = tar.length;
715-
offset = 0;
716-
toStr = function(arr) {
717-
return String.fromCharCode.apply(null, arr).replace(/\0+$/, '');
718-
};
719-
while (offset < tarlen && tar[offset] !== 0) {
720-
h = tar.subarray(offset, offset + 512);
721-
name = toStr(h.subarray(0, 100));
722-
if (prefix = toStr(h.subarray(345, 500))) {
723-
name = prefix + name;
724-
}
725-
size = parseInt(toStr(h.subarray(124, 136)), 8);
726-
dbs = Math.ceil(size / 512) * 512;
727-
if (name === '././@LongLink') {
728-
name = toStr(tar.subarray(offset + 512, offset + 512 + dbs));
729-
}
730-
(name !== 'pax_global_header') && filenames.push(name);
731-
offset = offset + 512 + dbs;
732-
}
733-
}
734-
} catch (e) {
735-
loading.remove();
736-
fm.debug('error', e);
748+
try {
749+
if (file.mime === 'application/zip') {
750+
unzip = new elFinder.Zlib.Unzip(new Uint8Array(xhr.response));
751+
filenames = unzip.getFilenames();
752+
} else if (file.mime === 'application/x-gzip') {
753+
unzip = new elFinder.Zlib.Gunzip(new Uint8Array(xhr.response));
754+
filenames = tarFiles(unzip.decompress());
755+
} else if (file.mime === 'application/x-tar') {
756+
filenames = tarFiles(new Uint8Array(xhr.response));
737757
}
738-
if (filenames && filenames.length) {
739-
filenames = $.map(filenames, function(str) {
740-
return fm.decodeRawString(str);
741-
});
742-
filenames.sort();
743-
loading.remove();
744-
header = '<strong>'+fm.escape(file.mime)+'</strong> ('+fm.formatSize(file.size)+')'+'<hr/>'
745-
doc = $('<div class="elfinder-quicklook-preview-archive-wrapper">'+header+'<pre class="elfinder-quicklook-preview-text">'+fm.escape(filenames.join("\n"))+'</pre></div>')
746-
.on('touchstart', function(e) {
747-
if ($(this)['scroll' + (fm.direction === 'ltr'? 'Right' : 'Left')]() > 5) {
748-
e.originalEvent._preventSwipeX = true;
749-
}
750-
})
751-
.appendTo(preview);
752-
ql.hideinfo();
753-
}
754-
}, 70);
758+
makeList(filenames);
759+
} catch (e) {
760+
loading.remove();
761+
fm.debug('error', e);
762+
}
755763
} else {
756764
loading.remove();
757765
}

0 commit comments

Comments
 (0)