Skip to content

Commit 7e739da

Browse files
committed
Fix: Multiple errors managing cache of files in servers
1 parent 1619cd8 commit 7e739da

File tree

5 files changed

+175
-50
lines changed

5 files changed

+175
-50
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
3636
- Long paths not workin in Windows (paths equal or greater than 260 characters) [`884bccd`](https://github.com/ollm/OpenComic/commit/884bccd153a4cb2775fbfb5f0e7c6575188ec073)
3737
- Extracting 7zip in separate stacks to avoid errors when extracting many files [`061827f`](https://github.com/ollm/OpenComic/commit/061827fc737aa8edfde14e7b092a8b44e571bf08)
3838
- Prevent scroll event while reading is loading [`4c93a72`](https://github.com/ollm/OpenComic/commit/4c93a724220fc820347464a988727ef4649052fe)
39+
- Multiple errors managing cache of files in servers
3940

4041
## [v1.2.0](https://github.com/ollm/OpenComic/releases/tag/v1.2.0) (29-03-2024)
4142

scripts/dom.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ function continueReadingError()
927927

928928
function compressedError(error, showInPage = true, snackbarKey = '')
929929
{
930-
// console.error(error);
930+
console.error(error);
931931

932932
if(showInPage)
933933
{

scripts/file-manager.js

+84-21
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ var file = function(path, _config = false) {
5454

5555
if(isServer(path))
5656
{
57-
if(inArray(fileExtension(path), compressedExtensions.all) || !containsCompressed(path))
57+
if(!containsCompressed(path))
5858
files = await this.readServer(path, _realPath);
5959
else
60-
files = await this.readInsideCompressedServer(path, _realPath);
60+
files = await this.readServerCompressed(path, _realPath);
6161
}
6262
else if(containsCompressed(path))
6363
{
@@ -282,8 +282,6 @@ var file = function(path, _config = false) {
282282
let sha = sha1(path);
283283
let cacheFile = 'server-files-'+sha+'.json';
284284

285-
let _containsCompressed = containsCompressed(path, 0, false);
286-
287285
if(this.config.cache && (this.config.cacheOnly || this.config.cacheServer || serverInOfflineMode))
288286
{
289287
let json = cache.readJson(cacheFile);
@@ -298,42 +296,108 @@ var file = function(path, _config = false) {
298296
if(this.config.cacheOnly)
299297
throw new Error('notCacheOnly');
300298

301-
let files = [];
299+
let files = await serverClient.read(path);
300+
301+
return files;
302+
303+
}
304+
305+
this.readServerCompressed = async function(path = false, _realPath = false) {
306+
307+
path = serverClient.fixPath(path || this.path);
308+
_realPath = _realPath || realPath(path, -1);
309+
310+
if(inArray(fileExtension(path), compressedExtensions.all))
311+
return this._readServerCompressed(path, _realPath);
312+
else
313+
return this.readServerInsideCompressed(path, _realPath);
314+
}
315+
316+
this._readServerCompressed = async function(path = false, _realPath = false) {
317+
318+
path = serverClient.fixPath(path || this.path);
319+
_realPath = _realPath || realPath(path, -1);
302320

303-
if(_containsCompressed)
321+
if(this.config.cache && (this.config.cacheOnly || this.config.cacheServer || serverInOfflineMode))
304322
{
305-
let firstCompressed = firstCompressedFile(path, 0);
323+
let files = false;
324+
325+
const cacheOnly = this.config.cacheOnly;
326+
this.updateConfig({cacheOnly: true});
306327

307-
if(!fs.existsSync(realPath(firstCompressed)))
328+
try
329+
{
330+
files = await this.readCompressed(path, _realPath);
331+
}
332+
catch(error)
333+
{
334+
if(!error.message || !/notCacheOnly/.test(error.message))
335+
throw new Error(error);
336+
}
337+
338+
this.updateConfig({cacheOnly: cacheOnly});
339+
340+
if(this.config.cacheOnly)
341+
throw new Error('notCacheOnly');
342+
343+
if(files !== false)
344+
return files;
345+
}
346+
347+
let firstCompressed = firstCompressedFile(path, 0);
348+
349+
if(firstCompressed)
350+
{
351+
if(!serverClient.existsSync(realPath(firstCompressed, -1)))
308352
{
309353
// Download file to tmp
310354
let file = await serverClient.download(path, {only: [firstCompressed]});
311355

312356
if(this.config.fromThumbnailsGeneration)
313357
downloadedCompressedFile(firstCompressed);
314358
}
315-
316-
return this.readCompressed(path);
317-
}
318-
else
319-
{
320-
files = await serverClient.read(path);
321359
}
322360

323-
return files;
361+
return this.readCompressed(path, _realPath);
324362

325363
}
326364

327-
this.readInsideCompressedServer = async function(path = false, _realPath = false) {
365+
this.readServerInsideCompressed = async function(path = false, _realPath = false) {
328366

329367
path = path || this.path;
330368
_realPath = _realPath || realPath(path, -1);
331369

370+
if(this.config.cache && (this.config.cacheOnly || this.config.cacheServer || serverInOfflineMode))
371+
{
372+
let files = false;
373+
374+
const cacheOnly = this.config.cacheOnly;
375+
this.updateConfig({cacheOnly: true});
376+
377+
try
378+
{
379+
files = await this.readInsideCompressed(path, _realPath);
380+
}
381+
catch(error)
382+
{
383+
if(!error.message || !/notCacheOnly/.test(error.message))
384+
throw new Error(error);
385+
}
386+
387+
this.updateConfig({cacheOnly: cacheOnly});
388+
389+
if(this.config.cacheOnly)
390+
throw new Error('notCacheOnly');
391+
392+
if(files !== false)
393+
return files;
394+
}
395+
332396
let firstCompressed = firstCompressedFile(path, 0);
333397

334398
if(firstCompressed)
335399
{
336-
if(!fs.existsSync(realPath(firstCompressed)))
400+
if(!serverClient.existsSync(realPath(firstCompressed, -1)))
337401
{
338402
// Download file to tmp
339403
let file = await serverClient.download(path, {only: [firstCompressed]});
@@ -412,7 +476,7 @@ var file = function(path, _config = false) {
412476
}
413477
else
414478
{
415-
image = await this._images(reverse ? -1 : 1, _files, from, fromReached, poster, deep + 1);
479+
image = await this._images((reverse ? -1 : 1), _files, from, fromReached, poster, deep + 1);
416480
fromReached = image.fromReached;
417481
image = image.images[0] || false;
418482
}
@@ -552,7 +616,7 @@ var file = function(path, _config = false) {
552616
try
553617
{
554618
let file = fileManager.file(dirname);
555-
file.updateConfig({...this.config, ...{fastRead: true, specialFiles: true, sha: false, cacheServer: true}});
619+
file.updateConfig({fastRead: true, specialFiles: true, sha: false, cacheServer: true});
556620
let files = await file.read();
557621

558622
let poster = this._poster(files);
@@ -1306,7 +1370,7 @@ var fileCompressed = function(path, _realPath = false, forceType = false, prefix
13061370
fs.mkdirSync(this.tmp);
13071371

13081372
if(this.config.only)
1309-
fs.writeFileSync(this.tmpPartialExtraction, '');
1373+
fs.writeFileSync(this.tmpPartialExtraction, '');
13101374
}
13111375
else if(!this.config.only)
13121376
{
@@ -1941,7 +2005,6 @@ var fileCompressed = function(path, _realPath = false, forceType = false, prefix
19412005

19422006
console.time('extract7z: '+this.path);
19432007

1944-
let only = this.config.only;
19452008
let _this = this;
19462009

19472010
const onlyLen = this.config._only ? this.config._only.length : false;

scripts/image.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ async function resize(fromImage, toImage, config = {})
2020
if(/*inArray(extension, imageExtensions.ico)/* || */inArray(extension, imageExtensions.ico)) // Unsupported images format for resize
2121
return reject({});
2222

23-
sharp(fromImage).jpeg({quality: config.quality}).resize(config).toFile(toImage, function(error) {
23+
sharp(fromImage, {failOn: 'none'}).jpeg({quality: config.quality}).resize(config).toFile(toImage, async function(error) {
2424

25-
if(error)
25+
if(error && /unsupported image format/iu.test(error?.message || ''))
2626
{
2727
if(!imageMagick) imageMagick = require('gm').subClass({imageMagick: true});
2828

@@ -68,6 +68,11 @@ async function resize(fromImage, toImage, config = {})
6868
}
6969
});
7070
}
71+
else if(error)
72+
{
73+
console.error(fromImage, error);
74+
reject(error);
75+
}
7176
else
7277
{
7378
resolve(toImage);

0 commit comments

Comments
 (0)