fix(file-manager): correlate upload acknowledgments
Build and publish Docker image / build-and-publish (push) Successful in 13s
Build and publish Docker image / build-and-publish (push) Successful in 13s
This commit is contained in:
1 parent
473ee8f9e5
commit
8e3a6d3446
1 file changed
+24
-9
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user