Skip to content

Commit

Permalink
Fix: Some errors reading compressed files
Browse files Browse the repository at this point in the history
  • Loading branch information
ollm committed Mar 26, 2024
1 parent d31bcdc commit 05eefb3
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 12 deletions.
91 changes: 90 additions & 1 deletion scripts/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,84 @@ function writeFileSync(name, content)
fs.writeFileSync(p.join(cacheFolder, name), content, function(){});
}

var jsonMemory = {};

function setJsonInMemory(name, json)
{
if(jsonMemory[name])
clearTimeout(jsonMemory[name].timeout);

jsonMemory[name] = {
timeout: setTimeout(function(){
delete jsonMemory[name];
}, 1000 * 60 * 60),
json: json,
lastUsage: app.time(),
};

if(app.rand(0, 20))
{
let num = 0;
let data = [];

for(let key in jsonMemory)
{
num++;

data.push({
name: key,
lastUsage: jsonMemory[key].lastUsage,
});
}

let max = 500;

if(num > max)
{
data.sort(function(a, b) {

if(a.lastUsage === b.lastUsage)
return 0;

return a.lastUsage > b.lastUsage ? 1 : -1;

});

for(let i = 0, len = data.length - max; i < len; i++)
{
delete jsonMemory[data[i].name];
}
}
}
}

function readJsonInMemory(name)
{
if(jsonMemory[name])
{
clearTimeout(jsonMemory[name].timeout);

jsonMemory[name].timeout = setTimeout(function(){
delete jsonMemory[name];
}, 1000 * 60 * 60);

jsonMemory[name].lastUsage = app.time();

return jsonMemory[name].json;
}

return false;
}

function flushJsonMemory()
{
jsonMemory = {};
}

async function writeJson(name, json)
{
setJsonInMemory(name, json);

let path = p.join(cacheFolder, name+'.zstd');

let encoded = await zstdEncoder.encode(Buffer.from(JSON.stringify(json)));
Expand All @@ -205,6 +281,8 @@ async function writeJson(name, json)

function writeJsonSync(name, json)
{
setJsonInMemory(name, json);

let path = p.join(cacheFolder, name+'.zstd');

let encoded = zstdEncoder.encodeSync(Buffer.from(JSON.stringify(json)));
Expand All @@ -223,12 +301,21 @@ function readFile(name)

function readJson(name)
{
let json = readJsonInMemory(name);
if(json) return json;

let path = p.join(cacheFolder, name+'.zstd');

if(fs.existsSync(path))
return JSON.parse(zstdDecoder.decodeSync(fs.readFileSync(path)));
{
json = JSON.parse(zstdDecoder.decodeSync(fs.readFileSync(path)));
setJsonInMemory(name, json);
return json;
}
else
{
return false;
}
}

function existsFile(name)
Expand Down Expand Up @@ -383,6 +470,8 @@ module.exports = {
existsJson: existsJson,
existsFile: existsFile,
deleteInCache: deleteInCache,
flushJsonMemory: flushJsonMemory,
jsonMemory: function(){return jsonMemory},
queuedImages: function(){return queuedImages},
processingTheImageQueue: function(){return processingTheImageQueue},
stopQueue: stopQueue,
Expand Down
6 changes: 3 additions & 3 deletions scripts/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -938,15 +938,15 @@ function compressedError(error, showInPage = true)

if(showInPage)
{
handlebarsContext.compressedError = (error.detail || error.message);
handlebarsContext.compressedError = error ? (error.detail || error.message) : '';
handlebarsContext.contentRightMessage = template.load('content.right.message.compressed.error.html');
template._contentRight().firstElementChild.innerHTML = template.load('content.right.message.html');
}
else
{
events.snackbar({
key: 'compressedError',
text: language.error.uncompress.title+': '+(error.detail || error.message),
text: language.error.uncompress.title+(error ? ': '+(error.detail || error.message) : ''),
duration: 6,
buttons: [
{
Expand Down Expand Up @@ -1142,7 +1142,7 @@ async function _getFolderThumbnails(file, images, _images, path, folderSha, isAs

}, file);

if(isAsync)
if(isAsync && poster.path)
{
addImageToDom(poster.sha, poster.path);
addImageToDom(folderSha+'-0', poster.path);
Expand Down
10 changes: 4 additions & 6 deletions scripts/ebook/epub.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ var epub = function(path, config = {}) {
await this.findContentOpf();

this.epub = new epubjs.Book(this.opf);
this.toc = await this.epub.loaded.navigation;

await this.epub.opened;
}

this.getHrefNames = function(items, hrefNames = {}) {
Expand All @@ -124,9 +126,6 @@ var epub = function(path, config = {}) {

await this.openEpub();

this.toc = await this.epub.loaded.navigation;
await this.epub.opened;

this.epubFiles = [];

if(this.epub.cover)
Expand Down Expand Up @@ -213,7 +212,6 @@ var epub = function(path, config = {}) {

await this.openEpub();

this.toc = await this.epub.loaded.navigation;
let metadata = await this.epub.loaded.metadata;

let res = fs.readFileSync(this.opf, 'utf8');
Expand Down Expand Up @@ -269,7 +267,7 @@ var epub = function(path, config = {}) {

this.renderFiles = async function(files, config, callback = false) {

await this.findContentOpf();
await this.openEpub();

let chapters = [];

Expand Down Expand Up @@ -315,7 +313,7 @@ var epub = function(path, config = {}) {

this.epubPages = async function(config, callback = false) {

await this.findContentOpf();
await this.openEpub();
let files = await this.readEpubFiles();

let chapters = [];
Expand Down
17 changes: 15 additions & 2 deletions scripts/file-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,7 @@ var fileCompressed = function(path, _realPath = false, forceType = false, prefix

this.config = this._config = {
// only: false,
cache: true,
width: window.devicePixelRatio * 150, // Vector width
height: false, // Vector height
force: false, // Forces the extraction even if the file exists
Expand Down Expand Up @@ -1649,6 +1650,18 @@ var fileCompressed = function(path, _realPath = false, forceType = false, prefix

}

this.fixCorruptedName = function(name, pos = 0) {

if(//.test(name))
{
let ext = p.extname(name);
return pos+' - '+sha1(name)+(ext ? '.'+ext : '');
}

return name;

}

// ZIP
this.zip = false;

Expand Down Expand Up @@ -1712,7 +1725,7 @@ var fileCompressed = function(path, _realPath = false, forceType = false, prefix
for(let i = 0, len = zip.files.length; i < len; i++)
{
let entry = zip.files[i];
let name = p.normalize(entry.path);
let name = p.normalize(this.fixCorruptedName(entry.path, i));

files.push({name: name, path: p.join(this.path, name), folder: (entry.type === 'Directory' ? true : false)});
this.setFileStatus(name, {extracted: false});
Expand Down Expand Up @@ -1747,7 +1760,7 @@ var fileCompressed = function(path, _realPath = false, forceType = false, prefix
for(let i = 0, len = zip.files.length; i < len; i++)
{
let entry = zip.files[i];
let name = p.normalize(entry.path);
let name = p.normalize(this.fixCorruptedName(entry.path, i));

let extract = !only || only[name] ? true : false;

Expand Down
1 change: 1 addition & 0 deletions scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ async function clearCache()
{
storage.set('cache', {});
await fse.emptyDir(cache.folder);
cache.flushJsonMemory();

getStorageSize();
}
Expand Down

0 comments on commit 05eefb3

Please sign in to comment.