From 88f2b152f68ee4f481a485c441b4d7f0139ef9dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=96=BB=E5=8B=87=E7=A5=A5?= <237899745@qq.com> Date: Tue, 11 Nov 2025 18:29:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D:=20=E5=88=86=E4=BA=AB?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E4=BD=BF=E7=94=A8=E5=88=86=E4=BA=AB=E8=80=85?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E5=AD=98=E5=82=A8=E7=B1=BB=E5=9E=8B=E8=80=8C?= =?UTF-8?q?=E9=9D=9E=E5=88=9B=E5=BB=BA=E6=97=B6=E5=AD=98=E5=82=A8=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 问题: 用户切换到本地存储后,旧分享仍使用创建时的SFTP存储类型,导致404错误 - 修复: 在4个位置将share.storage_type改为shareOwner.current_storage_type - /api/share/:code/verify (缓存未命中逻辑) - /api/share/:code/verify (错误回退逻辑) - /api/share/:code/list (文件列表) - /api/share/:code/download-file (文件下载) - 重构: 调整代码顺序,先获取shareOwner再定义storageType - 日志: 添加"(分享者当前)"标识,便于调试 --- backend/server.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/backend/server.js b/backend/server.js index 389648f..6121a3e 100644 --- a/backend/server.js +++ b/backend/server.js @@ -1346,8 +1346,6 @@ app.post('/api/share/:code/verify', async (req, res) => { responseData.file = shareFileCache.get(code); } else { // 缓存未命中,查询存储 - const storageType = share.storage_type || 'sftp'; - console.log(`[缓存未命中] 分享码: ${code},存储类型: ${storageType}`); try { // 获取分享者的用户信息 const shareOwner = UserDB.findById(share.user_id); @@ -1355,6 +1353,10 @@ app.post('/api/share/:code/verify', async (req, res) => { throw new Error('分享者不存在'); } + // 使用分享者当前的存储类型(而不是分享创建时的存储类型) + const storageType = shareOwner.current_storage_type || 'sftp'; + console.log(`[缓存未命中] 分享码: ${code},存储类型: ${storageType} (分享者当前)`); + // 使用统一存储接口 const { StorageInterface } = require('./storage'); const userForStorage = { @@ -1409,7 +1411,7 @@ app.post('/api/share/:code/verify', async (req, res) => { const httpBaseUrl = share.http_download_base_url || ''; const baseUrl = httpBaseUrl ? httpBaseUrl.replace(/\/+$/, '') : ''; const normalizedFilePath = filePath.startsWith('/') ? filePath : `/${filePath}`; - const storageType = share.storage_type || 'sftp'; + const storageType = shareOwner.current_storage_type || 'sftp'; const httpDownloadUrl = (storageType === 'sftp' && baseUrl) ? `${baseUrl}${normalizedFilePath}` : null; responseData.file = { @@ -1472,8 +1474,8 @@ app.post('/api/share/:code/list', async (req, res) => { // 使用统一存储接口,根据分享的storage_type选择存储后端 const { StorageInterface } = require('./storage'); - const storageType = share.storage_type || 'sftp'; - console.log(`[分享列表] 存储类型: ${storageType}, 分享路径: ${share.share_path}`); + const storageType = shareOwner.current_storage_type || 'sftp'; + console.log(`[分享列表] 存储类型: ${storageType} (分享者当前), 分享路径: ${share.share_path}`); // 临时构造用户对象以使用存储接口 const userForStorage = { @@ -1657,8 +1659,8 @@ app.get('/api/share/:code/download-file', async (req, res) => { // 使用统一存储接口,根据分享的storage_type选择存储后端 const { StorageInterface } = require('./storage'); - const storageType = share.storage_type || 'sftp'; - console.log(`[分享下载] 存储类型: ${storageType}, 文件路径: ${filePath}`); + const storageType = shareOwner.current_storage_type || 'sftp'; + console.log(`[分享下载] 存储类型: ${storageType} (分享者当前), 文件路径: ${filePath}`); // 临时构造用户对象以使用存储接口 const userForStorage = {