Skip to content
This repository was archived by the owner on Apr 8, 2026. It is now read-only.

Commit 8ca97a7

Browse files
committed
fix(download): improve error handling for file metadata fetch and remove deprecated EOCD fetching logic
1 parent 4601e43 commit 8ca97a7

1 file changed

Lines changed: 2 additions & 75 deletions

File tree

src/controllers/GameController.ts

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,8 @@ export class Games {
463463
}
464464

465465
const fileRes = await fetch(link, { method: 'HEAD' });
466-
console.log('Response headers:', fileRes.headers);
467-
if (!fileRes.headers.get('accept-ranges') || fileRes.headers.get('accept-ranges') === 'none') {
468-
throw new Error('Le serveur ne supporte pas les requêtes Range');
466+
if (!fileRes.ok) {
467+
return res.status(fileRes.status).send({ message: 'Error fetching file metadata' });
469468
}
470469

471470
res.setHeader('Content-Length', fileRes.headers.get('content-length') || '0');
@@ -476,78 +475,6 @@ export class Games {
476475
handleError(res, error, 'Error fetching file metadata');
477476
}
478477
}
479-
480-
@httpGet('/:gameId/eocd', LoggedCheck.middleware)
481-
public async getEOCD(req: AuthenticatedRequest, res: Response) {
482-
const { gameId } = req.params;
483-
const userId = req.user.user_id;
484-
485-
try {
486-
const game = await this.gameService.getGame(gameId);
487-
if (!game) {
488-
return res.status(404).send({ message: 'Game not found' });
489-
}
490-
491-
const owns = (await this.gameService.userOwnsGame(gameId, userId)) || game.owner_id === userId;
492-
if (!owns) {
493-
return res.status(403).send({ message: 'Access denied' });
494-
}
495-
496-
const link = game.download_link;
497-
if (!link) {
498-
return res.status(404).send({ message: 'Download link not available' });
499-
}
500-
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();
525-
}
526-
527-
// Extract EOCD from the buffer
528-
const bufferData = Buffer.from(buffer);
529-
const eocd = this.extractEOCD(bufferData);
530-
531-
res.setHeader('Content-Type', 'application/octet-stream');
532-
res.status(200).send(eocd);
533-
} catch (error) {
534-
handleError(res, error, 'Error fetching EOCD');
535-
}
536-
}
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-
}
551478
}
552479

553480

0 commit comments

Comments
 (0)