tecuts commited on
Commit
82c6cbd
·
verified ·
1 Parent(s): c3deef4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -22
app.py CHANGED
@@ -86,6 +86,9 @@ app.mount("/files", StaticFiles(directory=DOWNLOADS_DIR), name="files")
86
  class DownloadRequest(BaseModel):
87
  url: str
88
 
 
 
 
89
  class FileInfo(BaseModel):
90
  filename: str
91
  download_url: str
@@ -149,32 +152,53 @@ async def download_file(request: DownloadRequest):
149
  space_url = os.getenv("SPACE_URL", "http://localhost:7860")
150
 
151
  for file_path in all_files:
152
- # Get the filename without the directory structure
153
- filename = os.path.basename(file_path)
154
-
155
- # Move file to download directory root if it's in a subdirectory
156
- if os.path.dirname(file_path):
 
157
  new_path = os.path.join(download_subdir, filename)
158
- shutil.move(file_path, new_path)
159
-
160
- # Get file extension
161
- file_type = os.path.splitext(filename)[1].lstrip('.')
162
-
163
- # Generate download URL
164
- encoded_filename = quote(filename)
165
- download_url = f"{space_url}/files/{timestamp}/{encoded_filename}"
166
-
167
- downloaded_files.append(FileInfo(
168
- filename=filename,
169
- download_url=download_url,
170
- file_type=file_type
171
- ))
172
-
173
- logger.info(f"Processed file: {filename} -> {download_url}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
 
175
  # Move back to original directory
176
  os.chdir(original_dir)
177
 
 
 
 
178
  return DownloadResponse(
179
  success=True,
180
  message=f"Successfully downloaded {len(downloaded_files)} files",
@@ -196,7 +220,6 @@ async def download_file(request: DownloadRequest):
196
  finally:
197
  if 'original_dir' in locals():
198
  os.chdir(original_dir)
199
-
200
  @app.get("/")
201
  async def root():
202
  return {"message": "Welcome to GAMDL API. Visit /docs for API documentation."}
 
86
  class DownloadRequest(BaseModel):
87
  url: str
88
 
89
+
90
+ # [Previous cookie handling code remains the same...]
91
+
92
  class FileInfo(BaseModel):
93
  filename: str
94
  download_url: str
 
152
  space_url = os.getenv("SPACE_URL", "http://localhost:7860")
153
 
154
  for file_path in all_files:
155
+ try:
156
+ # Convert to absolute path
157
+ abs_file_path = os.path.abspath(file_path)
158
+ filename = os.path.basename(file_path)
159
+
160
+ # Create new path in download directory
161
  new_path = os.path.join(download_subdir, filename)
162
+ abs_new_path = os.path.abspath(new_path)
163
+
164
+ logger.info(f"Moving file from {abs_file_path} to {abs_new_path}")
165
+
166
+ # Copy file instead of moving
167
+ shutil.copy2(abs_file_path, abs_new_path)
168
+
169
+ # Get file extension
170
+ file_type = os.path.splitext(filename)[1].lstrip('.')
171
+
172
+ # Generate download URL
173
+ encoded_filename = quote(filename)
174
+ download_url = f"{space_url}/files/{timestamp}/{encoded_filename}"
175
+
176
+ downloaded_files.append(FileInfo(
177
+ filename=filename,
178
+ download_url=download_url,
179
+ file_type=file_type
180
+ ))
181
+
182
+ logger.info(f"Processed file: {filename} -> {download_url}")
183
+
184
+ except Exception as e:
185
+ logger.error(f"Error processing file {file_path}: {str(e)}")
186
+ continue
187
+
188
+ # Clean up original files after successful copy
189
+ for root, dirs, files in os.walk('.'):
190
+ for dir_name in dirs:
191
+ if dir_name != download_subdir:
192
+ dir_path = os.path.join(root, dir_name)
193
+ logger.info(f"Removing directory: {dir_path}")
194
+ shutil.rmtree(dir_path, ignore_errors=True)
195
 
196
  # Move back to original directory
197
  os.chdir(original_dir)
198
 
199
+ if not downloaded_files:
200
+ raise Exception("Failed to process any files")
201
+
202
  return DownloadResponse(
203
  success=True,
204
  message=f"Successfully downloaded {len(downloaded_files)} files",
 
220
  finally:
221
  if 'original_dir' in locals():
222
  os.chdir(original_dir)
 
223
  @app.get("/")
224
  async def root():
225
  return {"message": "Welcome to GAMDL API. Visit /docs for API documentation."}