Skip to content

Commit

Permalink
Fix: Error opening a PDF/Zip file after adding it to the library
Browse files Browse the repository at this point in the history
  • Loading branch information
ollm committed Jul 20, 2023
1 parent f71d081 commit e24fb9f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 35 deletions.
97 changes: 65 additions & 32 deletions scripts/file-compressed.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ function setProgress(progress, contentRightZindex)
});
}

var currentExtracting = false;
var waitingCurrentExtractionCallback = false;

function waitingCurrentExtraction()
{
currentExtracting = false;

if(waitingCurrentExtractionCallback)
{
waitingCurrentExtractionCallback.apply(null, arguments);
waitingCurrentExtractionCallback = false;
}
}

function extractZip(path, virtualPath, sha, all, json, callback)
{
let cacheFile = 'compressed-files-'+sha+'.json';
Expand All @@ -136,6 +150,8 @@ function extractZip(path, virtualPath, sha, all, json, callback)

try
{
currentExtracting = path;

fs.createReadStream(path).pipe(

unzip.Extract({path: p.join(tempFolder, shaExt)}).on('close', function () {
Expand All @@ -149,22 +165,22 @@ function extractZip(path, virtualPath, sha, all, json, callback)

compressedFiles[sha] = files;

callback((all) ? files : file.allToFirst(files));
callCallbacks([(all) ? files : file.allToFirst(files)], waitingCurrentExtraction, callback);

}).on('error', function(error){

if(/0xafbc7a37/.test(error.message)) // 7zip file
fileCompressed.extract7zip(path, virtualPath, sha, all, json, callback);
else
callback({error: ERROR_UNZIPPING_THE_FILE, detail: error.message});
callCallbacks([{error: ERROR_UNZIPPING_THE_FILE, detail: error.message}], waitingCurrentExtraction, callback);

})
);
}
catch(error)
{
console.error(error);
callback({error: ERROR_UNZIPPING_THE_FILE, detail: error.message});
callCallbacks([{error: ERROR_UNZIPPING_THE_FILE, detail: error.message}], waitingCurrentExtraction, callback);
}
}

Expand All @@ -178,6 +194,8 @@ function extract7zip(path, virtualPath, sha, all, json, callback)
if(un7z === false) un7z = require('node-7z');
if(bin7z === false) bin7z = asarToAsarUnpacked(require('7zip-bin').path7za);

currentExtracting = path;

un7z.extractFull(path, p.join(tempFolder, shaExt), {$progress: true, p: false/*'myPassword'*/, $bin: bin7z}).on('progress', function(progress) {

setProgress(progress.percent / 100, contentRightZindex);
Expand All @@ -195,17 +213,17 @@ function extract7zip(path, virtualPath, sha, all, json, callback)

compressedFiles[sha] = files;

callback((all) ? files : file.allToFirst(files));
callCallbacks([(all) ? files : file.allToFirst(files)], waitingCurrentExtraction, callback);
}
catch(error)
{
console.error(error);
callback({error: ERROR_UNZIPPING_THE_FILE, detail: error.message});
callCallbacks([{error: ERROR_UNZIPPING_THE_FILE, detail: error.message}], waitingCurrentExtraction, callback);
}

}).on('error', function(error){

callback({error: ERROR_UNZIPPING_THE_FILE, detail: error.stderr});
callCallbacks([{error: ERROR_UNZIPPING_THE_FILE, detail: error.stderr}], waitingCurrentExtraction, callback);

});
}
Expand All @@ -230,7 +248,9 @@ function extractRar(path, virtualPath, sha, all, json, callback)
path: path,
bin: bin,
});


currentExtracting = path;

archive.list(function (error, entries) {

try
Expand Down Expand Up @@ -312,19 +332,19 @@ function extractRar(path, virtualPath, sha, all, json, callback)

compressedFiles[sha] = files;

callback((all) ? files : file.allToFirst(files));
callCallbacks([(all) ? files : file.allToFirst(files)], waitingCurrentExtraction, callback);

});
}
else
{
callback({error: ERROR_UNZIPPING_THE_FILE, detail: error.message});
callCallbacks([{error: ERROR_UNZIPPING_THE_FILE, detail: error.message}], waitingCurrentExtraction, callback);
}
}
catch(error)
{
console.error(error);
callback({error: ERROR_UNZIPPING_THE_FILE, detail: error.message});
callCallbacks([{error: ERROR_UNZIPPING_THE_FILE, detail: error.message}], waitingCurrentExtraction, callback);
}

});
Expand All @@ -339,6 +359,8 @@ function extractTar(path, virtualPath, sha, all, json, callback)

if(untar === false) untar = require('tar-fs');

currentExtracting = path;

var untarP = fs.createReadStream(path).pipe(untar.extract(p.join(tempFolder, shaExt))).on('finish', function () {

try
Expand All @@ -352,17 +374,17 @@ function extractTar(path, virtualPath, sha, all, json, callback)

compressedFiles[sha] = files;

callback((all) ? files : file.allToFirst(files));
callCallbacks([(all) ? files : file.allToFirst(files)], waitingCurrentExtraction, callback);
}
catch(error)
{
console.error(error);
callback({error: ERROR_UNZIPPING_THE_FILE, detail: error.message});
callCallbacks([{error: ERROR_UNZIPPING_THE_FILE, detail: error.message}], waitingCurrentExtraction, callback);
}

}).on('error', function(error){

callback({error: ERROR_UNZIPPING_THE_FILE, detail: error.message});
callCallbacks([{error: ERROR_UNZIPPING_THE_FILE, detail: error.message}], waitingCurrentExtraction, callback);

untarP.destroy();

Expand All @@ -382,6 +404,8 @@ function extractPdf(path, virtualPath, sha, all, json, callback)
unpdf.GlobalWorkerOptions.workerSrc = p.join(appDir, 'node_modules/pdfjs-dist/build/pdf.worker.js');
}

currentExtracting = path;

(async function() {

try
Expand Down Expand Up @@ -426,20 +450,19 @@ function extractPdf(path, virtualPath, sha, all, json, callback)
console.timeEnd('pdf render');

if(fs.existsSync(p.join(tempFolder, shaExt))) fs.renameSync(p.join(tempFolder, shaExt), p.join(tempFolder, sha));

var files = file.returnAll(p.join(tempFolder, sha), {from: p.join(tempFolder, sha), to: virtualPath});

if(!json || json.mtime != mtime)
cache.writeFile(cacheFile, JSON.stringify({mtime: mtime, files: files}), {}, function(){});

compressedFiles[sha] = files;

callback((all) ? files : file.allToFirst(files));
callCallbacks([(all) ? files : file.allToFirst(files)], waitingCurrentExtraction, callback);
}
catch(error)
{
console.error(error);
callback({error: ERROR_UNZIPPING_THE_FILE, detail: error.message});
callCallbacks([{error: ERROR_UNZIPPING_THE_FILE, detail: error.message}], waitingCurrentExtraction, callback);
}

})();
Expand Down Expand Up @@ -483,25 +506,34 @@ function returnFiles(path, all, fromCache, callback)
}
else
{
if(inArray(fileExtension(path), compressedExtensions.zip))
if(path === currentExtracting)
{
fileCompressed.extractZip(path, virtualPath, sha, all, json, callback);
}
else if(inArray(fileExtension(path), compressedExtensions['7z']))
{
fileCompressed.extract7zip(path, virtualPath, sha, all, json, callback);
}
else if(inArray(fileExtension(path), compressedExtensions.rar))
{
fileCompressed.extractRar(path, virtualPath, sha, all, json, callback);
}
else if(inArray(fileExtension(path), compressedExtensions.tar))
{
fileCompressed.extractTar(path, virtualPath, sha, all, json, callback);
console.log('Tried to unzip the same file 2 times', path);

waitingCurrentExtractionCallback = callback;
}
else if(inArray(fileExtension(path), compressedExtensions.pdf))
else
{
fileCompressed.extractPdf(path, virtualPath, sha, all, json, callback);
if(inArray(fileExtension(path), compressedExtensions.zip))
{
fileCompressed.extractZip(path, virtualPath, sha, all, json, callback);
}
else if(inArray(fileExtension(path), compressedExtensions['7z']))
{
fileCompressed.extract7zip(path, virtualPath, sha, all, json, callback);
}
else if(inArray(fileExtension(path), compressedExtensions.rar))
{
fileCompressed.extractRar(path, virtualPath, sha, all, json, callback);
}
else if(inArray(fileExtension(path), compressedExtensions.tar))
{
fileCompressed.extractTar(path, virtualPath, sha, all, json, callback);
}
else if(inArray(fileExtension(path), compressedExtensions.pdf))
{
fileCompressed.extractPdf(path, virtualPath, sha, all, json, callback);
}
}

return true;
Expand Down Expand Up @@ -563,4 +595,5 @@ module.exports = {
extractTar: extractTar,
extractPdf: extractPdf,
fileDocumentsToRender: function(){return fileDocumentsToRender},
currentExtracting: function(){return currentExtracting},
};
15 changes: 12 additions & 3 deletions scripts/opencomic.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ var errorDialog = true;

window.addEventListener('error', function(evt) {

var error = false;
let error = false;

if(evt.message)
if(evt.message && !/debug\-evaluate/.test(evt.message))
error = 'Error: '+evt.message +' at linenumber '+evt.lineno+':'+evt.colno+' of file '+evt.filename;

if(error !== false && errorDialog)
Expand Down Expand Up @@ -988,6 +988,15 @@ function addComic(folders = false)

}

function callCallbacks()
{
for(let i = 1, len = arguments.length; i < len; i++)
{
if(arguments[i])
arguments[i].apply(null, arguments[0])
}
}

//Cheack errors

function checkError(value, error = false)
Expand All @@ -1003,7 +1012,7 @@ function checkError(value, error = false)
return false;
}

//Errors list
//Errors list

const NOT_POSSIBLE_WITHOUT_DECOMPRESSING = 1;
const ERROR_UNZIPPING_THE_FILE = 2;

0 comments on commit e24fb9f

Please sign in to comment.