sathvik77 commited on
Commit
432f562
·
1 Parent(s): 2bfa333

feat: GET /download/{job_id} endpoint and APScheduler file cleanup

Browse files

- Created backend/cleanup.py with _cleanup_old_files (removes mp3s older than 1h) and start_cleanup_scheduler (APScheduler interval job every 15 min)
- Updated main.py startup to call start_cleanup_scheduler and added GET /download/{job_id} returning FileResponse or 404/409
- Added 3 download tests covering done→200, processing→409, and unknown job→404

Files changed (3) hide show
  1. cleanup.py +28 -0
  2. main.py +21 -0
  3. tests/test_main.py +35 -0
cleanup.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ from pathlib import Path
3
+ from apscheduler.schedulers.asyncio import AsyncIOScheduler
4
+
5
+ _scheduler: AsyncIOScheduler | None = None
6
+
7
+
8
+ def _cleanup_old_files(upload_dir: Path, output_dir: Path, max_age_seconds: int = 3600) -> None:
9
+ now = time.time()
10
+ for directory in [upload_dir, output_dir]:
11
+ for f in directory.glob("*.mp3"):
12
+ try:
13
+ if now - f.stat().st_mtime > max_age_seconds:
14
+ f.unlink(missing_ok=True)
15
+ except OSError:
16
+ pass # file may have been deleted between glob and unlink
17
+
18
+
19
+ def start_cleanup_scheduler(upload_dir: Path, output_dir: Path) -> None:
20
+ global _scheduler
21
+ _scheduler = AsyncIOScheduler()
22
+ _scheduler.add_job(
23
+ _cleanup_old_files,
24
+ "interval",
25
+ minutes=15,
26
+ args=[upload_dir, output_dir],
27
+ )
28
+ _scheduler.start()
main.py CHANGED
@@ -7,6 +7,7 @@ from fastapi.responses import FileResponse
7
 
8
  from models import JobState, JobStatus, UploadResponse
9
  from processor import enqueue_job, get_job_status, init_queue, start_worker
 
10
 
11
  BASE_DIR = Path(__file__).parent
12
  UPLOAD_DIR = BASE_DIR / "tmp" / "uploads"
@@ -29,6 +30,7 @@ app.add_middleware(
29
  async def startup():
30
  UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
31
  OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
 
32
  init_queue()
33
  await start_worker()
34
 
@@ -76,3 +78,22 @@ async def upload(file: UploadFile = File(...)):
76
  raise HTTPException(status_code=503, detail=str(e)) from e
77
 
78
  return UploadResponse(job_id=job_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  from models import JobState, JobStatus, UploadResponse
9
  from processor import enqueue_job, get_job_status, init_queue, start_worker
10
+ from cleanup import start_cleanup_scheduler
11
 
12
  BASE_DIR = Path(__file__).parent
13
  UPLOAD_DIR = BASE_DIR / "tmp" / "uploads"
 
30
  async def startup():
31
  UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
32
  OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
33
+ start_cleanup_scheduler(UPLOAD_DIR, OUTPUT_DIR)
34
  init_queue()
35
  await start_worker()
36
 
 
78
  raise HTTPException(status_code=503, detail=str(e)) from e
79
 
80
  return UploadResponse(job_id=job_id)
81
+
82
+
83
+ @app.get("/download/{job_id}")
84
+ async def download(job_id: str):
85
+ job = get_job_status(job_id)
86
+ if job is None:
87
+ raise HTTPException(status_code=404, detail="Job not found")
88
+ if job.state != JobState.done:
89
+ raise HTTPException(status_code=409, detail="Job not complete yet")
90
+
91
+ output_file = OUTPUT_DIR / f"{job_id}.mp3"
92
+ if not output_file.exists():
93
+ raise HTTPException(status_code=404, detail="Output file not found")
94
+
95
+ return FileResponse(
96
+ path=output_file,
97
+ media_type="audio/mpeg",
98
+ filename="karaoke.mp3",
99
+ )
tests/test_main.py CHANGED
@@ -83,3 +83,38 @@ async def test_upload_rejects_non_mp3_bytes(client):
83
  )
84
  assert response.status_code == 400
85
  assert "valid MP3" in response.json()["detail"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  )
84
  assert response.status_code == 400
85
  assert "valid MP3" in response.json()["detail"]
86
+
87
+
88
+ async def test_download_done_job(client, tmp_path, monkeypatch):
89
+ import main
90
+ from models import JobState, JobStatus
91
+ import processor
92
+
93
+ job_id = "done-job-123"
94
+ fake_output = tmp_path / f"{job_id}.mp3"
95
+ fake_output.write_bytes(b"\xff\xfb" + b"\x00" * 200)
96
+
97
+ fake_status = JobStatus(job_id=job_id, state=JobState.done, progress=100)
98
+ monkeypatch.setitem(processor._jobs, job_id, fake_status)
99
+ monkeypatch.setattr(main, "OUTPUT_DIR", tmp_path)
100
+
101
+ response = await client.get(f"/download/{job_id}")
102
+ assert response.status_code == 200
103
+ assert "audio" in response.headers["content-type"]
104
+
105
+
106
+ async def test_download_processing_job_returns_409(client, monkeypatch):
107
+ import processor
108
+ from models import JobState, JobStatus
109
+
110
+ job_id = "processing-job"
111
+ fake_status = JobStatus(job_id=job_id, state=JobState.processing, progress=40)
112
+ monkeypatch.setitem(processor._jobs, job_id, fake_status)
113
+
114
+ response = await client.get(f"/download/{job_id}")
115
+ assert response.status_code == 409
116
+
117
+
118
+ async def test_download_unknown_job_returns_404(client):
119
+ response = await client.get("/download/does-not-exist")
120
+ assert response.status_code == 404