Skip to content

Commit

Permalink
New: Code of reading compressed files has been rewritten, improving t…
Browse files Browse the repository at this point in the history
…humbnail generation performance, especially in PDF files
  • Loading branch information
ollm committed Jul 30, 2023
1 parent 68eff46 commit c1259bb
Show file tree
Hide file tree
Showing 11 changed files with 2,579 additions and 2,586 deletions.
1,307 changes: 297 additions & 1,010 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@
"ldd": "^1.0.0",
"mime": "^2.5.2",
"node-7z": "^3.0.0",
"pdfjs-dist": "^2.9.359",
"pdfjs-dist": "^3.8.162",
"request": "^2.88.0",
"sha1": "^1.1.1",
"sharp": "^0.30.6",
"tar-fs": "^2.0.0",
"unrar": "^0.2.0",
"unzipper": "^0.10.11"
"unzipper": "^0.10.14"
},
"devDependencies": {
"electron": "^19.0.4",
Expand Down
69 changes: 64 additions & 5 deletions scripts/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function processTheImageQueue()
var img = queuedImages[0];
var sha = img.sha;

var realPath = file.realPath(img.file);
var realPath = fileManager.realPath(img.file);

sharp(realPath).jpeg({quality: 95}).resize({width: img.size, background: 'white'}).toFile(p.join(cacheFolder, sha+'.jpg'), function(error) {

Expand Down Expand Up @@ -174,7 +174,6 @@ var data = false;

function returnCacheImage(file, sha, callback = false, vars = false)
{

if(!data) data = storage.get('cache');

if(!callback)
Expand All @@ -184,11 +183,11 @@ function returnCacheImage(file, sha, callback = false, vars = false)
sha = sha1(file);
}

var size = Math.round(window.devicePixelRatio * 150);
let size = Math.round(window.devicePixelRatio * 150);

var imgCache = data[sha];
let imgCache = data[sha];

var path = p.join(cacheFolder, sha+'.jpg?size='+size);
let path = p.join(cacheFolder, sha+'.jpg?size='+size);

if(typeof imgCache == 'undefined' || !fs.existsSync(p.join(cacheFolder, sha+'.jpg')))
{
Expand All @@ -213,6 +212,65 @@ function returnCacheImage(file, sha, callback = false, vars = false)
}
}

async function returnThumbnailsImages(images, callback, file = false)
{
if(!data) data = storage.get('cache');

let size = Math.round(window.devicePixelRatio * 150);

let thumbnails = {};
let toGenerateThumbnails = [];
let toGenerateThumbnailsData = {};

for(let i = 0, len = images.length; i < len; i++)
{
let image = images[i];

let sha = image.sha || sha1(image.path);
let imgCache = data[sha];

let path = p.join(cacheFolder, sha+'.jpg?size='+size);

if(typeof imgCache == 'undefined' || !fs.existsSync(p.join(cacheFolder, sha+'.jpg')))
{
toGenerateThumbnails.push(image);
toGenerateThumbnailsData[image.path] = {sha: sha, vars: image.vars};

thumbnails[sha] = {cache: false, path: '', sha: sha};
}
else
{
data[sha].lastAccess = time();

if(imgCache.size != size)
{
toGenerateThumbnails.push(image);
toGenerateThumbnailsData[image.path] = {sha: sha, vars: image.vars};

thumbnails[sha] = {cache: true, path: escapeBackSlash(path), sha: sha};
}
else
{
thumbnails[sha] = {cache: true, path: escapeBackSlash(path), sha: sha};
}
}
}

if(toGenerateThumbnails.length > 0 && file)
{
// Consider adding this to a queue if it causes problems
file.makeAvailable(toGenerateThumbnails, function(image) {

let data = toGenerateThumbnailsData[image.path];
addImageToQueue(image.path, size, data.sha, callback, data.vars || false);

});
}

return thumbnails;
}


function writeFile(name, content)
{
fs.writeFile(p.join(cacheFolder, name), content, function(){});
Expand All @@ -229,6 +287,7 @@ function readFile(name)
module.exports = {
folder: cacheFolder,
returnCacheImage: returnCacheImage,
returnThumbnailsImages: returnThumbnailsImages,
cleanQueue: cleanQueue,
writeFile: writeFile,
readFile: readFile,
Expand Down

0 comments on commit c1259bb

Please sign in to comment.