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

Commit c911043

Browse files
committed
fix(download): improve game download handling with direct file streaming and enhanced response headers
1 parent 6cdd28e commit c911043

1 file changed

Lines changed: 33 additions & 8 deletions

File tree

src/controllers/GameController.ts

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,28 +394,53 @@ export class Games {
394394
const { gameId } = req.params;
395395
const userId = req.user.user_id;
396396
try {
397-
// Vérifiez si le jeu existe
398397
const game = await this.gameService.getGame(gameId);
399398
if (!game) {
400399
return res.status(404).send({ message: 'Game not found' });
401400
}
402-
403-
// Vérifiez si l'utilisateur possède le jeu ou y a accès
404401
const owns = (await this.gameService.userOwnsGame(gameId, userId)) || game.owner_id === userId;
405402
if (!owns) {
406403
return res.status(403).send({ message: 'Access denied' });
407404
}
408-
409-
// Vérifiez si un lien de téléchargement est disponible
410405
const link = game.download_link;
411406
if (!link) {
412407
return res.status(404).send({ message: 'Download link not available' });
413408
}
414409

415-
// Renvoyer directement le lien du fichier ZIP en réponse
416-
res.status(200).send({ downloadLink: link });
410+
const headers: any = {};
411+
if (req.headers.range) {
412+
headers.Range = req.headers.range;
413+
}
414+
415+
const fileRes = await fetch(link, { headers });
416+
if (!fileRes.ok) {
417+
return res.status(fileRes.status).send({ message: 'Error fetching file' });
418+
}
419+
420+
res.setHeader('Content-Disposition', `attachment; filename="${game.name}.zip"`);
421+
res.setHeader('Content-Type', fileRes.headers.get('content-type') || 'application/octet-stream');
422+
423+
const contentLength = fileRes.headers.get('content-length');
424+
if (contentLength !== null) {
425+
res.setHeader('Content-Length', contentLength);
426+
}
427+
const acceptRanges = fileRes.headers.get('accept-ranges');
428+
if (acceptRanges !== null) {
429+
res.setHeader('Accept-Ranges', acceptRanges);
430+
}
431+
const contentRange = fileRes.headers.get('content-range');
432+
if (contentRange !== null) {
433+
res.setHeader('Content-Range', contentRange);
434+
}
435+
436+
res.status(fileRes.status);
437+
if (fileRes.body) {
438+
fileRes.body.pipe(res);
439+
} else {
440+
res.end();
441+
}
417442
} catch (error) {
418-
handleError(res, error, 'Error processing download request');
443+
handleError(res, error, 'Error downloading game');
419444
}
420445
}
421446
}

0 commit comments

Comments
 (0)