diff --git a/backend/server.js b/backend/server.js index 6121a3e..f847852 100644 --- a/backend/server.js +++ b/backend/server.js @@ -222,6 +222,30 @@ function formatFileSize(bytes) { return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i]; } + +// 生成分享URL(处理非标准端口) +function generateShareUrl(req, shareCode) { + const protocol = req.protocol; + const host = req.get('host'); // 可能包含或不包含端口 + + // 如果 host 已经包含端口号,直接使用 + if (host.includes(':')) { + return `${protocol}://${host}/s/${shareCode}`; + } + + // 如果没有端口号,检查是否需要添加 + // 从环境变量读取公开端口(nginx监听的端口) + const publicPort = process.env.PUBLIC_PORT || null; + + // 如果配置了公开端口,且不是标准端口(80/443),则添加端口号 + if (publicPort && publicPort !== '80' && publicPort !== '443') { + return `${protocol}://${host}:${publicPort}/s/${shareCode}`; + } + + // 标准端口或未配置,不添加端口号 + return `${protocol}://${host}/s/${shareCode}`; +} + // ===== 公开API ===== // 健康检查 @@ -1209,7 +1233,7 @@ app.post('/api/share/create', authMiddleware, (req, res) => { db.prepare('UPDATE shares SET storage_type = ? WHERE id = ?') .run(req.user.current_storage_type || 'sftp', result.id); - const shareUrl = `${req.protocol}://${req.get('host')}/s/${result.share_code}`; + const shareUrl = generateShareUrl(req, result.share_code); res.json({ success: true, @@ -1236,7 +1260,7 @@ app.get('/api/share/my', authMiddleware, (req, res) => { success: true, shares: shares.map(share => ({ ...share, - share_url: `${req.protocol}://${req.get('host')}/s/${share.share_code}` + share_url: generateShareUrl(req, share.share_code) })) }); } catch (error) {