Fix: Resolve upload race condition

This commit is contained in:
gpt-engineer-app[bot]
2025-09-29 17:53:55 +00:00
parent 1d67f6ea79
commit 22266a39c7

View File

@@ -130,8 +130,8 @@ export function UppyPhotoUpload({
setUploadProgress(0);
onUploadStart?.();
// Process each file to get upload URL
for (const file of files) {
// Process all files in parallel to get upload URLs
const uploadPromises = files.map(async (file) => {
try {
const response = await supabase.functions.invoke('upload-image', {
body: { metadata, variant },
@@ -156,6 +156,8 @@ export function UppyPhotoUpload({
endpoint: result.uploadURL,
});
}
return { success: true, fileId: file.id };
} catch (error) {
console.error('Failed to get upload URL:', error);
uppy.removeFile(file.id);
@@ -165,7 +167,22 @@ export function UppyPhotoUpload({
description: `Failed to prepare upload for ${file.name}`,
});
onUploadError?.(error as Error);
return { success: false, fileId: file.id };
}
});
// Wait for all URL fetching to complete
const results = await Promise.all(uploadPromises);
// Check if any files are ready to upload
const successfulFiles = results.filter(r => r.success);
if (successfulFiles.length > 0) {
// All URLs are fetched, now trigger the upload
uppy.upload();
} else {
// No files ready, reset state
setIsUploading(false);
setUploadProgress(0);
}
});
@@ -279,7 +296,7 @@ export function UppyPhotoUpload({
if (!uppyRef.current || disabled) return;
try {
// Add files to Uppy
// Add files to Uppy - the files-added event will handle URL fetching and upload
files.forEach((file) => {
try {
uppyRef.current?.addFile({
@@ -298,10 +315,7 @@ export function UppyPhotoUpload({
}
});
// Auto-start upload after adding files
if (uppyRef.current.getFiles().length > 0) {
uppyRef.current.upload();
}
// Don't call upload() here - the files-added handler will trigger it after URLs are ready
} catch (error) {
console.error('Error adding files:', error);
toast({