Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.

Commit 4601e43

Browse files
committed
fix(eocd): enhance EOCD fetching logic with error handling and fallback to full download
1 parent 4917fd8 commit 4601e43

File tree

1 file changed

+43
-9
lines changed

1 file changed

+43
-9
lines changed

src/controllers/GameController.ts

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -498,22 +498,56 @@ export class Games {
498498
return res.status(404).send({ message: 'Download link not available' });
499499
}
500500

501-
// Fetch the last 64KB of the file (maximum EOCD search window)
502-
const rangeHeader = 'bytes=-1024'; // Réduire la plage demandée
503-
const fileRes = await fetch(link, { headers: { Range: rangeHeader } });
504-
505-
if (!fileRes.ok) {
506-
const errorText = await fileRes.text();
507-
return res.status(fileRes.status).send({ message: 'Error fetching EOCD', status: fileRes.status, error: errorText });
501+
let buffer;
502+
try {
503+
// Attempt to fetch the last 1KB of the file (maximum EOCD search window)
504+
const rangeHeader = 'bytes=-1024';
505+
const fileRes = await fetch(link, { headers: { Range: rangeHeader } });
506+
507+
if (!fileRes.ok) {
508+
throw new Error(`Range request failed with status ${fileRes.status}`);
509+
}
510+
511+
buffer = await fileRes.arrayBuffer();
512+
} catch (rangeError) {
513+
// Ensure rangeError is treated as an Error
514+
const errorMessage = rangeError instanceof Error ? rangeError.message : 'Unknown error';
515+
console.warn('Range request failed, falling back to full download:', errorMessage);
516+
517+
// Fallback: Download the entire file
518+
const fullFileRes = await fetch(link);
519+
if (!fullFileRes.ok) {
520+
const errorText = await fullFileRes.text();
521+
return res.status(fullFileRes.status).send({ message: 'Error fetching EOCD', status: fullFileRes.status, error: errorText });
522+
}
523+
524+
buffer = await fullFileRes.arrayBuffer();
508525
}
509526

510-
const buffer = await fileRes.arrayBuffer();
527+
// Extract EOCD from the buffer
528+
const bufferData = Buffer.from(buffer);
529+
const eocd = this.extractEOCD(bufferData);
530+
511531
res.setHeader('Content-Type', 'application/octet-stream');
512-
res.status(200).send(Buffer.from(buffer));
532+
res.status(200).send(eocd);
513533
} catch (error) {
514534
handleError(res, error, 'Error fetching EOCD');
515535
}
516536
}
537+
538+
private extractEOCD(buffer: Buffer): Buffer {
539+
// EOCD signature: 0x06054b50
540+
const eocdSignature = Buffer.from([0x50, 0x4b, 0x05, 0x06]);
541+
const maxEOCDSearch = Math.min(buffer.length, 1024); // Search within the last 1KB
542+
543+
for (let i = buffer.length - maxEOCDSearch; i >= 0; i--) {
544+
if (buffer.slice(i, i + 4).equals(eocdSignature)) {
545+
return buffer.slice(i);
546+
}
547+
}
548+
549+
throw new Error('EOCD not found in the file');
550+
}
517551
}
518552

519553

0 commit comments

Comments
 (0)