fomext commited on
Commit
31536a1
·
verified ·
1 Parent(s): c77ac80

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -10
app.py CHANGED
@@ -7,6 +7,10 @@ import subprocess
7
  import threading
8
  from typing import Dict
9
 
 
 
 
 
10
  app = FastAPI()
11
 
12
  UPLOAD_DIR = "uploads"
@@ -148,25 +152,42 @@ def job_status(job_id: str):
148
  # -----------------------------
149
  # Download stem + auto cleanup
150
  # -----------------------------
151
- @app.get("/download/{job_id}/{stem}")
152
- def download_stem(
153
  job_id: str,
154
- stem: str,
155
  background_tasks: BackgroundTasks
156
  ):
157
  job = jobs.get(job_id)
158
  if not job or job["status"] != "completed":
159
  raise HTTPException(404, "Job not completed")
160
 
161
- path = job["stems"].get(stem)
162
- if not path or not os.path.exists(path):
163
- raise HTTPException(404, "Stem not found")
164
 
165
- # delete after response finishes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
  background_tasks.add_task(cleanup_job, job_id)
 
167
 
168
  return FileResponse(
169
- path,
170
- media_type="audio/wav",
171
- filename=f"{stem}.wav"
172
  )
 
 
7
  import threading
8
  from typing import Dict
9
 
10
+ import zipfile
11
+ import tempfile
12
+
13
+
14
  app = FastAPI()
15
 
16
  UPLOAD_DIR = "uploads"
 
152
  # -----------------------------
153
  # Download stem + auto cleanup
154
  # -----------------------------
155
+ @app.get("/download/{job_id}/all")
156
+ def download_all_stems(
157
  job_id: str,
 
158
  background_tasks: BackgroundTasks
159
  ):
160
  job = jobs.get(job_id)
161
  if not job or job["status"] != "completed":
162
  raise HTTPException(404, "Job not completed")
163
 
164
+ stems = job.get("stems")
165
+ if not stems:
166
+ raise HTTPException(404, "No stems found")
167
 
168
+ # create temp zip file
169
+ tmp = tempfile.NamedTemporaryFile(
170
+ delete=False,
171
+ suffix=".zip"
172
+ )
173
+ zip_path = tmp.name
174
+ tmp.close()
175
+
176
+ with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf:
177
+ for stem_name, stem_path in stems.items():
178
+ if os.path.exists(stem_path):
179
+ zipf.write(
180
+ stem_path,
181
+ arcname=f"{stem_name}.wav"
182
+ )
183
+
184
+ # cleanup after response is sent
185
  background_tasks.add_task(cleanup_job, job_id)
186
+ background_tasks.add_task(os.remove, zip_path)
187
 
188
  return FileResponse(
189
+ zip_path,
190
+ media_type="application/zip",
191
+ filename=f"{job_id}_stems.zip"
192
  )
193
+