feat: Poll for file processing status after upload
Implement polling for the uploaded file's `access_ready` status to ensure it's fully processed and available before presenting the final details. This improves the user experience by: - Displaying an initial "waiting for file processing" message. - Editing the same message with the final upload embed once processing is complete, providing a smoother interaction. Additionally, updated API response keys for file size (from `file_size` to `size`) and original name (from `original_name` to `file_name`) to align with recent API changes.
This commit is contained in:
parent
6ac3a6d5bc
commit
47353d7c8b
@ -259,6 +259,34 @@ class UploadCog(commands.Cog, name="Upload"):
|
||||
print(f"Direct upload failed: {response.status} - {error_text}")
|
||||
raise Exception(f"API request failed: {response.status} - {error_text}")
|
||||
|
||||
# Poll until access_ready is true or timeout after 30 seconds
|
||||
file_id = upload_data.get("id", "unknown")
|
||||
file_url = f"https://slipstreamm.dev/uploads/{file_id}"
|
||||
|
||||
# Send initial message that we're waiting for the file to be processed
|
||||
status_message = await interaction.followup.send("File uploaded successfully. Waiting for file processing to complete...")
|
||||
|
||||
# Poll for access_ready status
|
||||
max_attempts = 30 # 30 seconds max wait time
|
||||
for attempt in range(max_attempts):
|
||||
try:
|
||||
# Get the current file status
|
||||
file_status = await self._make_api_request("GET", f"/upload/api/file/{file_id}/status")
|
||||
print(f"File status poll attempt {attempt+1}: {file_status}")
|
||||
|
||||
if file_status.get("access_ready", False):
|
||||
print(f"File is ready after {attempt+1} attempts")
|
||||
# Update upload_data with the latest information
|
||||
upload_data = file_status
|
||||
break
|
||||
|
||||
# Wait 1 second before polling again
|
||||
await asyncio.sleep(1)
|
||||
except Exception as e:
|
||||
print(f"Error polling file status: {e}")
|
||||
# Continue polling despite errors
|
||||
await asyncio.sleep(1)
|
||||
|
||||
# Create embed with upload information
|
||||
embed = discord.Embed(
|
||||
title="File Uploaded Successfully",
|
||||
@ -266,11 +294,8 @@ class UploadCog(commands.Cog, name="Upload"):
|
||||
color=discord.Color.green()
|
||||
)
|
||||
|
||||
file_id = upload_data.get("id", "unknown")
|
||||
file_url = f"{self.api_base_url}/uploads/{file_id}"
|
||||
|
||||
# Format file size nicely
|
||||
file_size_bytes = upload_data.get('file_size', 0)
|
||||
file_size_bytes = upload_data.get('size', 0)
|
||||
if file_size_bytes < 1024:
|
||||
file_size_str = f"{file_size_bytes} bytes"
|
||||
elif file_size_bytes < 1024 * 1024:
|
||||
@ -279,7 +304,7 @@ class UploadCog(commands.Cog, name="Upload"):
|
||||
file_size_str = f"{file_size_bytes / (1024 * 1024):.2f} MB"
|
||||
|
||||
embed.add_field(name="File ID", value=file_id, inline=True)
|
||||
embed.add_field(name="Original Name", value=upload_data.get("original_name", "unknown"), inline=True)
|
||||
embed.add_field(name="Original Name", value=upload_data.get("file_name", "unknown"), inline=True)
|
||||
embed.add_field(name="File Size", value=file_size_str, inline=True)
|
||||
embed.add_field(name="Content Type", value=upload_data.get("content_type", "unknown"), inline=True)
|
||||
embed.add_field(name="Scan Status", value=upload_data.get("scan_status", "unknown"), inline=True)
|
||||
@ -288,8 +313,8 @@ class UploadCog(commands.Cog, name="Upload"):
|
||||
# Add clickable link
|
||||
embed.description += f"\n\n[Click here to download]({file_url})"
|
||||
|
||||
# Send the final upload success message to the original interaction channel
|
||||
await interaction.followup.send(embed=embed)
|
||||
# Edit the status message with the final embed
|
||||
await status_message.edit(content=None, embed=embed)
|
||||
|
||||
except Exception as e:
|
||||
# If an error occurs during captcha generation or upload, send an ephemeral error message
|
||||
|
Loading…
x
Reference in New Issue
Block a user