jebin2 commited on
Commit
e213765
·
1 Parent(s): 02588dd
Files changed (1) hide show
  1. routers/gemini.py +21 -35
routers/gemini.py CHANGED
@@ -390,8 +390,12 @@ async def download_video(
390
  ):
391
  """
392
  Download a generated video.
393
- If the video hasn't been downloaded yet, downloads it from the stored URL.
 
394
  """
 
 
 
395
  query = select(GeminiJob).where(
396
  GeminiJob.job_id == job_id,
397
  GeminiJob.user_id == user.user_id,
@@ -412,18 +416,6 @@ async def download_video(
412
  detail="Video not ready for download"
413
  )
414
 
415
- # Check if video is already downloaded
416
- filename = job.output_data.get("filename")
417
- if filename:
418
- filepath = os.path.join(DOWNLOADS_DIR, filename)
419
- if os.path.exists(filepath):
420
- return FileResponse(
421
- path=filepath,
422
- media_type="video/mp4",
423
- filename=f"video_{job_id}.mp4"
424
- )
425
-
426
- # Video not downloaded yet, download from stored URL
427
  video_url = job.output_data.get("video_url")
428
  if not video_url:
429
  raise HTTPException(
@@ -431,28 +423,22 @@ async def download_video(
431
  detail="No video URL available"
432
  )
433
 
434
- # Download the video on-demand
435
- from services.gemini_service import GeminiService
436
- service = GeminiService() # Uses default API key
437
-
438
- try:
439
- filename = await service.download_video(video_url, job.job_id)
440
-
441
- # Update job with downloaded filename
442
- job.output_data = {**job.output_data, "filename": filename}
443
- await db.commit()
444
-
445
- filepath = os.path.join(DOWNLOADS_DIR, filename)
446
- return FileResponse(
447
- path=filepath,
448
- media_type="video/mp4",
449
- filename=f"video_{job_id}.mp4"
450
- )
451
- except Exception as e:
452
- raise HTTPException(
453
- status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
454
- detail=f"Failed to download video: {str(e)}"
455
- )
456
 
457
 
458
  @router.post("/job/{job_id}/cancel")
 
390
  ):
391
  """
392
  Download a generated video.
393
+ Downloads from Gemini URL, streams to client, then deletes local file.
394
+ No permanent storage on server.
395
  """
396
+ from fastapi.responses import StreamingResponse
397
+ import httpx
398
+
399
  query = select(GeminiJob).where(
400
  GeminiJob.job_id == job_id,
401
  GeminiJob.user_id == user.user_id,
 
416
  detail="Video not ready for download"
417
  )
418
 
 
 
 
 
 
 
 
 
 
 
 
 
419
  video_url = job.output_data.get("video_url")
420
  if not video_url:
421
  raise HTTPException(
 
423
  detail="No video URL available"
424
  )
425
 
426
+ # Stream video directly from Gemini URL to client
427
+ # No local file storage needed
428
+ async def stream_video():
429
+ async with httpx.AsyncClient(timeout=120.0, follow_redirects=True) as client:
430
+ async with client.stream("GET", video_url) as response:
431
+ response.raise_for_status()
432
+ async for chunk in response.aiter_bytes(chunk_size=8192):
433
+ yield chunk
434
+
435
+ return StreamingResponse(
436
+ stream_video(),
437
+ media_type="video/mp4",
438
+ headers={
439
+ "Content-Disposition": f'attachment; filename="video_{job_id}.mp4"'
440
+ }
441
+ )
 
 
 
 
 
 
442
 
443
 
444
  @router.post("/job/{job_id}/cancel")