Split readZipArchive into two functions instead of recursive calls
@@ -108,28 +108,19 @@ export default class LinkOverlay extends React.Component {
if (zipPath in zipEntriesCache) {
this.loadFileFromZipEntries(zipEntriesCache[zipPath], zipFile);
} else {
- this.readZipArchive(zipPath, zipFile, true);
+ this.readZipArchiveHttpRange(zipPath, zipFile, true);
}
}
- /*
- * Tries to read the file from a ZIP archive. A flag decides whether to use a HTTP range request for faster access.
- * Automatically tries again with a normal HTTP request in case a HTTP range request is not supported. Tries to load
- * the file via XML in case the HTTP request in general fails.
- */
- readZipArchive(zipPath, zipFile, useHttpRange) {
- const reader = useHttpRange
- ? new zip.HttpRangeReader(zipPath)
- : new zip.HttpReader(zipPath);
+ /* Tries to read the file from a ZIP archive with a HTTP range request. */
+ readZipArchiveHttpRange(zipPath, zipFile) {
try {
zip.createReader(
- reader,
+ new zip.HttpRangeReader(zipPath),
zipReader => this.loadFileFromZipArchive(zipReader, zipFile, zipPath),
error => {
- if (useHttpRange && error === "HTTP Range not supported.") {
- this.readZipArchive(zipPath, zipFile, false);
- } else if (!useHttpRange) {
- this.loadFileFromZipViaXML(zipPath, zipFile);
+ if (error === "HTTP Range not supported.") {
+ this.readZipArchiveNoHttpRange(zipPath, zipFile);
} else {
const errorMsg =
typeof error === "string"
@@ -148,6 +139,25 @@ export default class LinkOverlay extends React.Component {
}
}
+ /* Tries to read the file from a ZIP archive with a normal HTTP request. */
+ readZipArchiveNoHttpRange(zipPath, zipFile) {
+ try {
+ zip.createReader(
+ new zip.HttpReader(zipPath),
+ zipReader => this.loadFileFromZipArchive(zipReader, zipFile, zipPath),
+ error => {
+ this.loadFileFromZipViaXML(zipPath, zipFile);
+ },
+ );
+ } catch (error) {
+ const errorMsg =
+ typeof error === "string"
+ ? error
+ : "ZIP reader could not be initialized";
+ this.setError(errorMsg);
+ }
+ }
+
/* Loads a file from the zip archive via an XML request.
* This should only be necessary for Google Chrome as a HTTP Reader does not work there.
*/
