This commit is contained in:
Baobhan Sith
2025-04-20 15:23:58 +08:00
parent 1160f8a514
commit 77cd9272ba
31 changed files with 2781 additions and 2113 deletions
+7 -3
View File
@@ -1,6 +1,6 @@
import { Request, Response } from 'express';
import path from 'path'; // 需要 path 用于处理文件名
import { activeSshConnections } from '../websocket'; // 导入共享的连接 Map
import { clientStates } from '../websocket'; // Import the exported clientStates map
/**
* 处理文件下载请求 (GET /api/v1/sftp/download)
@@ -25,9 +25,13 @@ export const downloadFile = async (req: Request, res: Response): Promise<void> =
// 查找与当前用户会话关联的活动 WebSocket 连接和 SFTP 会话
let userSftpSession = null;
// 注意:这种查找方式效率不高,实际应用中可能需要更优化的结构来按 userId 查找连接
for (const [ws, connData] of activeSshConnections.entries()) {
// TODO: Refactor this to use SftpService instead of directly accessing clientStates
// This direct access is not ideal and couples the controller to websocket internals.
for (const [sessionId, state] of clientStates.entries()) {
const ws = state.ws; // Get the WebSocket instance from the state
const connData = state; // Use the entire state object
// 假设 AuthenticatedWebSocket 上存储了 userId
if ((ws as any).userId === userId && connData.sftp) {
if (ws.userId === userId && connData.sftp) { // Access userId directly from AuthenticatedWebSocket
// 这里简单地取第一个找到的匹配连接,没有处理 connectionId 的匹配
// TODO: 需要一种方式将 HTTP 请求与特定的 WebSocket/SSH/SFTP 会话关联起来
// 临时方案:假设一个用户只有一个活动的 SSH/SFTP 会话