修复: 非标准端口部署时分享链接未携带端口号的问题
- 添加 generateShareUrl 辅助函数,智能处理端口号 - 支持通过 PUBLIC_PORT 环境变量配置nginx监听端口 - 更新 POST /api/share/create 使用新的URL生成方法 - 更新 GET /api/share/my 使用新的URL生成方法 - 修复IP模式部署时分享链接无法正常访问的问题
This commit is contained in:
@@ -222,6 +222,30 @@ function formatFileSize(bytes) {
|
|||||||
return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i];
|
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 =====
|
// ===== 公开API =====
|
||||||
|
|
||||||
// 健康检查
|
// 健康检查
|
||||||
@@ -1209,7 +1233,7 @@ app.post('/api/share/create', authMiddleware, (req, res) => {
|
|||||||
db.prepare('UPDATE shares SET storage_type = ? WHERE id = ?')
|
db.prepare('UPDATE shares SET storage_type = ? WHERE id = ?')
|
||||||
.run(req.user.current_storage_type || 'sftp', result.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({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
@@ -1236,7 +1260,7 @@ app.get('/api/share/my', authMiddleware, (req, res) => {
|
|||||||
success: true,
|
success: true,
|
||||||
shares: shares.map(share => ({
|
shares: shares.map(share => ({
|
||||||
...share,
|
...share,
|
||||||
share_url: `${req.protocol}://${req.get('host')}/s/${share.share_code}`
|
share_url: generateShareUrl(req, share.share_code)
|
||||||
}))
|
}))
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user