SergeyO7 commited on
Commit
e5d5560
·
verified ·
1 Parent(s): ecdc640

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -25
app.py CHANGED
@@ -31,43 +31,45 @@ async def check_n_load_attach(session: aiohttp.ClientSession, task_id
31
  try:
32
  async with session.get(file_url, timeout=15) as response:
33
  if response.status == 200:
34
- # Get filename from Content-Disposition
35
- filename = None
36
- content_disposition = response.headers.get("Content-Disposition")
37
- if content_disposition and "filename=" in content_disposition:
38
- filename = content_disposition.split("filename=")[-1].strip('"')
39
- if not filename:
40
- # Determine extension from Content-Type
41
- content_type = str(response.headers.get("Content-Type", "")).lower()
42
- extension = ""
43
- if "image/png" in content_type:
44
- extension = ".png"
45
- elif "image/jpeg" in content_type:
46
- extension = ".jpg"
47
- elif "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" in content_type:
48
- extension = ".xlsx"
49
- elif "audio/mpeg" in content_type:
50
- extension = ".mp3"
51
- elif "application/pdf" in content_type:
52
- extension = ".pdf"
53
- elif "text/x-python" in content_type:
54
- extension = ".py"
55
- else:
56
- extension = ""
57
- filename = f"{task_id}{extension}"
58
 
59
- # Save the file
 
60
  local_file_path = os.path.join("downloads", filename)
61
  os.makedirs("downloads", exist_ok=True)
 
 
62
  async with aiofiles.open(local_file_path, "wb") as file:
63
  async for chunk in response.content.iter_chunked(8192):
64
  await file.write(chunk)
65
  print(f"File downloaded successfully: {local_file_path}")
66
  return local_file_path
 
 
 
67
  except aiohttp.ClientError as e:
68
  print(f"Error downloading attachment for task {task_id}: {str(e)}")
69
  return None
70
 
 
71
  async def fetch_questions(session: aiohttp.ClientSession, questions_url: str) -> list:
72
  """Fetch questions asynchronously."""
73
  try:
 
31
  try:
32
  async with session.get(file_url, timeout=15) as response:
33
  if response.status == 200:
34
+
35
+ # Determine file extension from Content-Type
36
+ content_type = str(response.headers.get("Content-Type", "")).lower()
37
+ extension = ""
38
+ if "image/png" in content_type:
39
+ extension = ".png"
40
+ elif "image/jpeg" in content_type:
41
+ extension = ".jpg"
42
+ elif "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" in content_type:
43
+ extension = ".xlsx"
44
+ elif "audio/mpeg" in content_type:
45
+ extension = ".mp3"
46
+ elif "application/pdf" in content_type:
47
+ extension = ".pdf"
48
+ elif "text/x-python" in content_type:
49
+ extension = ".py"
50
+ else:
51
+ print(f"Unsupported content type: {content_type} for task {task_id}")
52
+ return None
 
 
 
 
 
53
 
54
+ # Use task_id as the filename to ensure uniqueness
55
+ filename = f"{task_id}{extension}"
56
  local_file_path = os.path.join("downloads", filename)
57
  os.makedirs("downloads", exist_ok=True)
58
+
59
+ # Save the file
60
  async with aiofiles.open(local_file_path, "wb") as file:
61
  async for chunk in response.content.iter_chunked(8192):
62
  await file.write(chunk)
63
  print(f"File downloaded successfully: {local_file_path}")
64
  return local_file_path
65
+ else:
66
+ print(f"Failed to download file for task {task_id}: HTTP {response.status}")
67
+ return None
68
  except aiohttp.ClientError as e:
69
  print(f"Error downloading attachment for task {task_id}: {str(e)}")
70
  return None
71
 
72
+
73
  async def fetch_questions(session: aiohttp.ClientSession, questions_url: str) -> list:
74
  """Fetch questions asynchronously."""
75
  try: