From bbf1ec10ef6a33b20faed2f7c45c9f7b1b56dbfa Mon Sep 17 00:00:00 2001 From: vibsin9322 Date: Thu, 21 Aug 2025 19:37:52 +0900 Subject: [PATCH] Improve large file download handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add better error handling for ECONNABORTED (client disconnect) - Add Accept-Ranges header for better download resume support - Add client connection close detection - Improve logging for download interruptions - Better file stat handling for large files ๐Ÿค– Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- server.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index 0441cbd..6145e85 100644 --- a/server.js +++ b/server.js @@ -730,15 +730,30 @@ app.get('/api/download/:id/:attachmentId', async (req, res) => { const encodedName = encodeURIComponent(originalName); // RFC 5987์„ ์ค€์ˆ˜ํ•˜๋Š” ํ—ค๋” ์„ค์ • (ํ•œ๊ธ€ ํŒŒ์ผ๋ช… ์ง€์›) + const stat = fs.statSync(filePath); res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${encodedName}`); res.setHeader('Content-Type', row.mime_type || 'application/octet-stream'); - res.setHeader('Content-Length', row.file_size || fs.statSync(filePath).size); + res.setHeader('Content-Length', stat.size); + res.setHeader('Accept-Ranges', 'bytes'); + res.setHeader('Cache-Control', 'public, max-age=0'); + // ํด๋ผ์ด์–ธํŠธ ์—ฐ๊ฒฐ ๋Š๊น€ ๊ฐ์ง€ + res.on('close', () => { + if (!res.headersSent) { + console.log('๐Ÿ“ ๋‹ค์šด๋กœ๋“œ ์ทจ์†Œ๋จ:', originalName); + } + }); + // ์›๋ณธ ํŒŒ์ผ๋ช…์œผ๋กœ ๋‹ค์šด๋กœ๋“œ res.download(filePath, originalName, (err) => { if (err) { - console.error('๐Ÿ“ ๋‹ค์šด๋กœ๋“œ ์˜ค๋ฅ˜:', err); + // ECONNABORTED๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์—ฐ๊ฒฐ์„ ๋Š์€ ๊ฒฝ์šฐ์ด๋ฏ€๋กœ ๋กœ๊ทธ๋งŒ ๋‚จ๊น€ + if (err.code === 'ECONNABORTED') { + console.log('๐Ÿ“ ๋‹ค์šด๋กœ๋“œ ์ค‘๋‹จ๋จ (ํด๋ผ์ด์–ธํŠธ ์—ฐ๊ฒฐ ํ•ด์ œ):', originalName); + } else { + console.error('๐Ÿ“ ๋‹ค์šด๋กœ๋“œ ์˜ค๋ฅ˜:', err); + } } else { console.log('๐Ÿ“ ๋‹ค์šด๋กœ๋“œ ์™„๋ฃŒ:', originalName); }