From 8e3a6d344657171bda0c02ca4bb854cc8877e21c Mon Sep 17 00:00:00 2001 From: "Wiktor A. Falkowski" Date: Fri, 23 Oct 2026 21:17:55 +0000 Subject: [PATCH] fix(file-manager): correlate upload acknowledgments --- src/features/servers/FileManager.jsx | 33 ++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/features/servers/FileManager.jsx b/src/features/servers/FileManager.jsx index edb1031..a1149f0 100644 --- a/src/features/servers/FileManager.jsx +++ b/src/features/servers/FileManager.jsx @@ -28,6 +28,20 @@ const FileManager = forwardRef(({ serverId, serverImage, sendMessage, isActive, const activeUploadsRef = useRef({}); // Changed to object to track multiple uploads const resumeAttemptedRef = useRef(new Set()); // Track which uploads we've attempted to resume + const findActiveUploadKey = (filePath) => { + if (activeUploadsRef.current[filePath]) return filePath; + const keys = Object.keys(activeUploadsRef.current); + const serverPathMatch = keys.find( + (key) => activeUploadsRef.current[key]?.serverFilePath === filePath + ); + if (serverPathMatch) return serverPathMatch; + + const fileName = filePath?.split('/').pop(); + if (!fileName) return undefined; + const fileNameMatches = keys.filter((key) => key.split('/').pop() === fileName); + return fileNameMatches.length === 1 ? fileNameMatches[0] : undefined; + }; + useImperativeHandle(ref, () => ({ handleWebSocketMessage })); @@ -68,7 +82,7 @@ const FileManager = forwardRef(({ serverId, serverImage, sendMessage, isActive, sendMessage({ type: 'upload_file_chunk', serverId: serverId, - filePath: filePath, + filePath: uploadInfo.serverFilePath || filePath, chunkIndex: uploadInfo.currentChunkIndex, chunkData: uploadInfo.chunks[uploadInfo.currentChunkIndex], totalChunks: uploadInfo.chunks.length @@ -249,7 +263,7 @@ const FileManager = forwardRef(({ serverId, serverImage, sendMessage, isActive, loadFiles(currentPath); break; case 'file_uploaded': - const uploadedFilePath = data.filePath; + const uploadedFilePath = findActiveUploadKey(data.filePath) || data.filePath; // Remove this upload from tracking const newProgress = { ...uploadProgress }; delete newProgress[uploadedFilePath]; @@ -268,8 +282,8 @@ const FileManager = forwardRef(({ serverId, serverImage, sendMessage, isActive, break; case 'upload_chunk_received': // Handle chunk acknowledgment and send next chunk - const chunkFilePath = data.filePath; - if (activeUploadsRef.current[chunkFilePath]) { + const chunkFilePath = findActiveUploadKey(data.filePath); + if (chunkFilePath) { const uploadInfo = activeUploadsRef.current[chunkFilePath]; const { chunks, currentChunkIndex } = uploadInfo; @@ -278,7 +292,7 @@ const FileManager = forwardRef(({ serverId, serverImage, sendMessage, isActive, sendMessage({ type: 'upload_file_chunk', serverId: serverId, - filePath: chunkFilePath, + filePath: uploadInfo.serverFilePath || data.filePath, chunkIndex: currentChunkIndex, chunkData: chunks[currentChunkIndex], totalChunks: chunks.length @@ -302,11 +316,12 @@ const FileManager = forwardRef(({ serverId, serverImage, sendMessage, isActive, break; case 'upload_started': // Upload started, begin sending chunks - const startedFilePath = data.filePath; + const startedFilePath = findActiveUploadKey(data.clientFilePath || data.filePath); const uploadId = data.uploadId; - if (activeUploadsRef.current[startedFilePath]) { + if (startedFilePath && activeUploadsRef.current[startedFilePath]) { const uploadInfo = activeUploadsRef.current[startedFilePath]; const { chunks } = uploadInfo; + uploadInfo.serverFilePath = data.filePath; // Store uploadId for resume capability uploadInfo.uploadId = uploadId; // Clear resume attempt flag since upload has started successfully @@ -318,7 +333,7 @@ const FileManager = forwardRef(({ serverId, serverImage, sendMessage, isActive, sendMessage({ type: 'upload_file_chunk', serverId: serverId, - filePath: startedFilePath, + filePath: uploadInfo.serverFilePath, chunkIndex: 0, chunkData: chunks[0], totalChunks: chunks.length @@ -340,7 +355,7 @@ const FileManager = forwardRef(({ serverId, serverImage, sendMessage, isActive, } break; case 'upload_error': - const errorFilePath = data.filePath || Object.keys(activeUploadsRef.current)[0]; + const errorFilePath = findActiveUploadKey(data.filePath) || data.filePath || Object.keys(activeUploadsRef.current)[0]; const errorProgress = { ...uploadProgress }; delete errorProgress[errorFilePath]; setUploadProgress(errorProgress);