Spaces:
paola1
/
Sleeping

paola1 commited on
Commit
85fe8e5
·
verified ·
1 Parent(s): c6f5b47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -4
app.py CHANGED
@@ -6,6 +6,7 @@ import os
6
  import schedule
7
  import time
8
  import threading
 
9
 
10
  app = FastAPI()
11
 
@@ -71,10 +72,22 @@ async def download_file(upload_id: str):
71
  raise HTTPException(status_code=404, detail="Upload not found or has expired")
72
 
73
  upload_data = data_storage[upload_id]
74
- if upload_data['data_type'] == "file":
75
- return Response(content=upload_data['file_data'], media_type="application/octet-stream", headers={"Content-Disposition": f"attachment; filename={upload_data['file_name']}"})
76
- elif upload_data['data_type'] == "text":
77
- return Response(content=upload_data['file_data'], media_type="text/plain", headers={"Content-Disposition": f"attachment; filename={upload_data['file_name']}"})
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  if __name__ == "__main__":
80
  scheduled_function()
 
6
  import schedule
7
  import time
8
  import threading
9
+ import mimetypes
10
 
11
  app = FastAPI()
12
 
 
72
  raise HTTPException(status_code=404, detail="Upload not found or has expired")
73
 
74
  upload_data = data_storage[upload_id]
75
+ file_name = upload_data['file_name']
76
+
77
+ # Guess the MIME type based on the file extension
78
+ mime_type, _ = mimetypes.guess_type(file_name)
79
+ if mime_type is None: # Default to octet-stream if type is unknown
80
+ mime_type = "application/octet-stream"
81
+
82
+ # For text uploads, ensure the MIME type is set correctly
83
+ if upload_data['data_type'] == "text":
84
+ mime_type = "text/plain"
85
+ # Optionally, force a `.txt` extension for text files if no extension is provided
86
+ if '.' not in file_name:
87
+ file_name += '.txt'
88
+
89
+ return Response(content=upload_data['file_data'], media_type=mime_type,
90
+ headers={"Content-Disposition": f"attachment; filename={file_name}"})
91
 
92
  if __name__ == "__main__":
93
  scheduled_function()