修复: 分享下载使用分享者当前存储类型而非创建时存储类型

- 问题: 用户切换到本地存储后,旧分享仍使用创建时的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
- 日志: 添加"(分享者当前)"标识,便于调试
This commit is contained in:
2025-11-11 18:29:20 +08:00
parent 2390b6c91c
commit 88f2b152f6

View File

@@ -1346,8 +1346,6 @@ app.post('/api/share/:code/verify', async (req, res) => {
responseData.file = shareFileCache.get(code); responseData.file = shareFileCache.get(code);
} else { } else {
// 缓存未命中,查询存储 // 缓存未命中,查询存储
const storageType = share.storage_type || 'sftp';
console.log(`[缓存未命中] 分享码: ${code},存储类型: ${storageType}`);
try { try {
// 获取分享者的用户信息 // 获取分享者的用户信息
const shareOwner = UserDB.findById(share.user_id); const shareOwner = UserDB.findById(share.user_id);
@@ -1355,6 +1353,10 @@ app.post('/api/share/:code/verify', async (req, res) => {
throw new Error('分享者不存在'); throw new Error('分享者不存在');
} }
// 使用分享者当前的存储类型(而不是分享创建时的存储类型)
const storageType = shareOwner.current_storage_type || 'sftp';
console.log(`[缓存未命中] 分享码: ${code},存储类型: ${storageType} (分享者当前)`);
// 使用统一存储接口 // 使用统一存储接口
const { StorageInterface } = require('./storage'); const { StorageInterface } = require('./storage');
const userForStorage = { const userForStorage = {
@@ -1409,7 +1411,7 @@ app.post('/api/share/:code/verify', async (req, res) => {
const httpBaseUrl = share.http_download_base_url || ''; const httpBaseUrl = share.http_download_base_url || '';
const baseUrl = httpBaseUrl ? httpBaseUrl.replace(/\/+$/, '') : ''; const baseUrl = httpBaseUrl ? httpBaseUrl.replace(/\/+$/, '') : '';
const normalizedFilePath = filePath.startsWith('/') ? filePath : `/${filePath}`; 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; const httpDownloadUrl = (storageType === 'sftp' && baseUrl) ? `${baseUrl}${normalizedFilePath}` : null;
responseData.file = { responseData.file = {
@@ -1472,8 +1474,8 @@ app.post('/api/share/:code/list', async (req, res) => {
// 使用统一存储接口根据分享的storage_type选择存储后端 // 使用统一存储接口根据分享的storage_type选择存储后端
const { StorageInterface } = require('./storage'); const { StorageInterface } = require('./storage');
const storageType = share.storage_type || 'sftp'; const storageType = shareOwner.current_storage_type || 'sftp';
console.log(`[分享列表] 存储类型: ${storageType}, 分享路径: ${share.share_path}`); console.log(`[分享列表] 存储类型: ${storageType} (分享者当前), 分享路径: ${share.share_path}`);
// 临时构造用户对象以使用存储接口 // 临时构造用户对象以使用存储接口
const userForStorage = { const userForStorage = {
@@ -1657,8 +1659,8 @@ app.get('/api/share/:code/download-file', async (req, res) => {
// 使用统一存储接口根据分享的storage_type选择存储后端 // 使用统一存储接口根据分享的storage_type选择存储后端
const { StorageInterface } = require('./storage'); const { StorageInterface } = require('./storage');
const storageType = share.storage_type || 'sftp'; const storageType = shareOwner.current_storage_type || 'sftp';
console.log(`[分享下载] 存储类型: ${storageType}, 文件路径: ${filePath}`); console.log(`[分享下载] 存储类型: ${storageType} (分享者当前), 文件路径: ${filePath}`);
// 临时构造用户对象以使用存储接口 // 临时构造用户对象以使用存储接口
const userForStorage = { const userForStorage = {