Fred808 commited on
Commit
d48f294
·
verified ·
1 Parent(s): dc81e34

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +42 -70
main.py CHANGED
@@ -1060,81 +1060,53 @@ async def get_batch_files(batch_id: str):
1060
  files=files
1061
  )
1062
 
1063
- @app.get("/download-all")
1064
- async def download_all_files():
1065
- """Get list of all downloadable files on the server"""
1066
- try:
1067
- # Get all files from the download directory
1068
- download_dir = Path(downloader.download_dir)
1069
- all_files = [f for f in download_dir.glob("*") if f.is_file()]
1070
-
1071
- if not all_files:
1072
- raise HTTPException(status_code=404, detail="No files found on server")
1073
-
1074
- # Return list of files with download URLs
1075
- file_list = []
1076
- for file_path in all_files:
1077
- filename = file_path.name
1078
- file_list.append({
1079
- "filename": filename,
1080
- "url": f"/video/file/{filename}",
1081
- "size": os.path.getsize(file_path)
1082
- })
1083
-
1084
- return {
1085
- "success": True,
1086
- "total_files": len(file_list),
1087
- "files": file_list
1088
- }
1089
-
1090
- except HTTPException:
1091
- raise
1092
- except Exception as e:
1093
- logger.error(f"Error listing all files: {e}")
1094
- raise HTTPException(status_code=500, detail=str(e))
1095
 
1096
  @app.get("/batch/download-all/{batch_id}")
1097
  async def download_batch_files(batch_id: str):
1098
- """Get list of all downloadable files for a specific batch"""
1099
- try:
1100
- if batch_id not in batch_status_store:
1101
- raise HTTPException(status_code=404, detail="Batch not found")
1102
 
1103
- batch_status = batch_status_store[batch_id]
1104
-
1105
- if batch_status["status"] != "completed":
1106
- raise HTTPException(
1107
- status_code=400,
1108
- detail=f"Batch is not completed yet. Current status: {batch_status['status']}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1109
  )
1110
-
1111
- # Get all successfully downloaded files
1112
- file_list = []
1113
- for result in batch_status["results"]:
1114
- if result.get("success") and result.get("download_path"):
1115
- file_path = result["download_path"]
1116
- if os.path.exists(file_path):
1117
- file_list.append({
1118
- "filename": os.path.basename(file_path),
1119
- "url": f"/video/file/{os.path.basename(file_path)}",
1120
- "size": os.path.getsize(file_path)
1121
- })
1122
-
1123
- if not file_list:
1124
- raise HTTPException(status_code=404, detail="No files found for this batch")
1125
-
1126
- return {
1127
- "success": True,
1128
- "batch_id": batch_id,
1129
- "total_files": len(file_list),
1130
- "files": file_list
1131
- }
1132
-
1133
- except HTTPException:
1134
- raise
1135
- except Exception as e:
1136
- logger.error(f"Error getting batch files: {e}")
1137
- raise HTTPException(status_code=500, detail=str(e))
1138
 
1139
  @app.get("/video/file/{filename}")
1140
  async def download_file(filename: str):
 
1060
  files=files
1061
  )
1062
 
1063
+
1064
+
1065
+ from fastapi.responses import RedirectResponse
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1066
 
1067
  @app.get("/batch/download-all/{batch_id}")
1068
  async def download_batch_files(batch_id: str):
1069
+ """Download all files from a specific batch"""
1070
+ if batch_id not in batch_status_store:
1071
+ raise HTTPException(status_code=404, detail="Batch not found")
 
1072
 
1073
+ batch_status = batch_status_store[batch_id]
1074
+
1075
+ if batch_status["status"] != "completed":
1076
+ raise HTTPException(
1077
+ status_code=400,
1078
+ detail=f"Batch not completed. Status: {batch_status['status']}"
1079
+ )
1080
+
1081
+ # Get first available file from batch
1082
+ for result in batch_status["results"]:
1083
+ if result.get("success") and result.get("download_path"):
1084
+ file_path = result["download_path"]
1085
+ if os.path.exists(file_path):
1086
+ filename = os.path.basename(file_path)
1087
+ return FileResponse(
1088
+ path=file_path,
1089
+ filename=filename,
1090
+ media_type='application/octet-stream'
1091
+ )
1092
+
1093
+ raise HTTPException(status_code=404, detail="No downloadable files found in this batch")
1094
+
1095
+ @app.get("/batch/download-all")
1096
+ async def download_all_files():
1097
+ """Download all available files (returns first found file)"""
1098
+ download_dir = Path(downloader.download_dir)
1099
+
1100
+ # Get first available file in download directory
1101
+ for file_path in download_dir.glob("*"):
1102
+ if file_path.is_file():
1103
+ return FileResponse(
1104
+ path=str(file_path),
1105
+ filename=file_path.name,
1106
+ media_type='application/octet-stream'
1107
  )
1108
+
1109
+ raise HTTPException(status_code=404, detail="No files available for download")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1110
 
1111
  @app.get("/video/file/{filename}")
1112
  async def download_file(filename: str):