Skip to content

Commit c1259bb

Browse files
committed
New: Code of reading compressed files has been rewritten, improving thumbnail generation performance, especially in PDF files
1 parent 68eff46 commit c1259bb

11 files changed

+2579
-2586
lines changed

package-lock.json

+297-1,010
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@
6969
"ldd": "^1.0.0",
7070
"mime": "^2.5.2",
7171
"node-7z": "^3.0.0",
72-
"pdfjs-dist": "^2.9.359",
72+
"pdfjs-dist": "^3.8.162",
7373
"request": "^2.88.0",
7474
"sha1": "^1.1.1",
7575
"sharp": "^0.30.6",
7676
"tar-fs": "^2.0.0",
7777
"unrar": "^0.2.0",
78-
"unzipper": "^0.10.11"
78+
"unzipper": "^0.10.14"
7979
},
8080
"devDependencies": {
8181
"electron": "^19.0.4",

scripts/cache.js

+64-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function processTheImageQueue()
1313
var img = queuedImages[0];
1414
var sha = img.sha;
1515

16-
var realPath = file.realPath(img.file);
16+
var realPath = fileManager.realPath(img.file);
1717

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

@@ -174,7 +174,6 @@ var data = false;
174174

175175
function returnCacheImage(file, sha, callback = false, vars = false)
176176
{
177-
178177
if(!data) data = storage.get('cache');
179178

180179
if(!callback)
@@ -184,11 +183,11 @@ function returnCacheImage(file, sha, callback = false, vars = false)
184183
sha = sha1(file);
185184
}
186185

187-
var size = Math.round(window.devicePixelRatio * 150);
186+
let size = Math.round(window.devicePixelRatio * 150);
188187

189-
var imgCache = data[sha];
188+
let imgCache = data[sha];
190189

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

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

215+
async function returnThumbnailsImages(images, callback, file = false)
216+
{
217+
if(!data) data = storage.get('cache');
218+
219+
let size = Math.round(window.devicePixelRatio * 150);
220+
221+
let thumbnails = {};
222+
let toGenerateThumbnails = [];
223+
let toGenerateThumbnailsData = {};
224+
225+
for(let i = 0, len = images.length; i < len; i++)
226+
{
227+
let image = images[i];
228+
229+
let sha = image.sha || sha1(image.path);
230+
let imgCache = data[sha];
231+
232+
let path = p.join(cacheFolder, sha+'.jpg?size='+size);
233+
234+
if(typeof imgCache == 'undefined' || !fs.existsSync(p.join(cacheFolder, sha+'.jpg')))
235+
{
236+
toGenerateThumbnails.push(image);
237+
toGenerateThumbnailsData[image.path] = {sha: sha, vars: image.vars};
238+
239+
thumbnails[sha] = {cache: false, path: '', sha: sha};
240+
}
241+
else
242+
{
243+
data[sha].lastAccess = time();
244+
245+
if(imgCache.size != size)
246+
{
247+
toGenerateThumbnails.push(image);
248+
toGenerateThumbnailsData[image.path] = {sha: sha, vars: image.vars};
249+
250+
thumbnails[sha] = {cache: true, path: escapeBackSlash(path), sha: sha};
251+
}
252+
else
253+
{
254+
thumbnails[sha] = {cache: true, path: escapeBackSlash(path), sha: sha};
255+
}
256+
}
257+
}
258+
259+
if(toGenerateThumbnails.length > 0 && file)
260+
{
261+
// Consider adding this to a queue if it causes problems
262+
file.makeAvailable(toGenerateThumbnails, function(image) {
263+
264+
let data = toGenerateThumbnailsData[image.path];
265+
addImageToQueue(image.path, size, data.sha, callback, data.vars || false);
266+
267+
});
268+
}
269+
270+
return thumbnails;
271+
}
272+
273+
216274
function writeFile(name, content)
217275
{
218276
fs.writeFile(p.join(cacheFolder, name), content, function(){});
@@ -229,6 +287,7 @@ function readFile(name)
229287
module.exports = {
230288
folder: cacheFolder,
231289
returnCacheImage: returnCacheImage,
290+
returnThumbnailsImages: returnThumbnailsImages,
232291
cleanQueue: cleanQueue,
233292
writeFile: writeFile,
234293
readFile: readFile,

0 commit comments

Comments
 (0)