VeuReu commited on
Commit
149ffc4
·
verified ·
1 Parent(s): 98bfbd9

Update storage/media_routers.py

Browse files
Files changed (1) hide show
  1. storage/media_routers.py +131 -0
storage/media_routers.py CHANGED
@@ -17,6 +17,8 @@ router = APIRouter(prefix="/media", tags=["Media Manager"])
17
  MEDIA_ROOT = Path("/data/media")
18
  file_manager = FileManager(MEDIA_ROOT)
19
  HF_TOKEN = os.getenv("HF_TOKEN")
 
 
20
 
21
 
22
  @router.delete("/clear_media", tags=["Media Manager"])
@@ -537,3 +539,132 @@ def list_all_audios(
537
  })
538
 
539
  return results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  MEDIA_ROOT = Path("/data/media")
18
  file_manager = FileManager(MEDIA_ROOT)
19
  HF_TOKEN = os.getenv("HF_TOKEN")
20
+ VALID_SUBTYPES = ("HITL", "MoE", "Salamandra")
21
+ AUDIO_EXTENSIONS = ["*.mp3", "*.wav", "*.aac", "*.m4a", "*.ogg", "*.flac"]
22
 
23
 
24
  @router.delete("/clear_media", tags=["Media Manager"])
 
539
  })
540
 
541
  return results
542
+
543
+
544
+ @router.post("/upload_audio_subtype", tags=["Media Manager"])
545
+ async def upload_audio_subtype(
546
+ audio: UploadFile = File(...),
547
+ sha1: str = Query(..., description="SHA1 of the video folder"),
548
+ subtype: str = Query(..., description="Subtype: HITL, MoE, or Salamandra"),
549
+ token: str = Query(..., description="Token required for authorization")
550
+ ):
551
+ """
552
+ Upload audio for a given subtype (HITL, MoE, Salamandra).
553
+ - Creates folder if missing: /data/media/<sha1>/<subtype>/
554
+ - Deletes any previous audio files
555
+ - Saves the new audio
556
+ """
557
+ validate_token(token)
558
+
559
+ if subtype not in VALID_SUBTYPES:
560
+ raise HTTPException(status_code=400, detail=f"subtype must be one of {VALID_SUBTYPES}")
561
+
562
+ MEDIA_ROOT = Path("/data/media")
563
+ subtype_dir = MEDIA_ROOT / sha1 / subtype
564
+ subtype_dir.mkdir(parents=True, exist_ok=True)
565
+
566
+ # Delete old audio files
567
+ try:
568
+ for pattern in AUDIO_EXTENSIONS:
569
+ for old_audio in subtype_dir.glob(pattern):
570
+ old_audio.unlink()
571
+ except Exception as exc:
572
+ raise HTTPException(status_code=500, detail=f"Failed to delete old audio files: {exc}")
573
+
574
+ final_path = subtype_dir / audio.filename
575
+
576
+ try:
577
+ file_bytes = await audio.read()
578
+ with open(final_path, "wb") as f:
579
+ f.write(file_bytes)
580
+ except Exception as exc:
581
+ raise HTTPException(status_code=500, detail=f"Failed to save audio: {exc}")
582
+
583
+ return JSONResponse(
584
+ status_code=200,
585
+ content={
586
+ "status": "ok",
587
+ "sha1": sha1,
588
+ "subtype": subtype,
589
+ "saved_to": str(final_path)
590
+ }
591
+ )
592
+
593
+
594
+ @router.get("/download_audio_subtype", tags=["Media Manager"])
595
+ def download_audio_subtype(
596
+ sha1: str,
597
+ subtype: str,
598
+ token: str = Query(..., description="Token required for authorization")
599
+ ):
600
+ """
601
+ Download the first audio file for a given subtype.
602
+ """
603
+ validate_token(token)
604
+
605
+ if subtype not in VALID_SUBTYPES:
606
+ raise HTTPException(status_code=400, detail=f"subtype must be one of {VALID_SUBTYPES}")
607
+
608
+ MEDIA_ROOT = Path("/data/media")
609
+ subtype_dir = MEDIA_ROOT / sha1 / subtype
610
+
611
+ if not subtype_dir.exists() or not subtype_dir.is_dir():
612
+ raise HTTPException(status_code=404, detail=f"{subtype} folder not found")
613
+
614
+ # Find audio files
615
+ audio_files = []
616
+ for pattern in AUDIO_EXTENSIONS:
617
+ audio_files.extend(list(subtype_dir.glob(pattern)))
618
+
619
+ if not audio_files:
620
+ raise HTTPException(status_code=404, detail="No audio files found")
621
+
622
+ audio_path = audio_files[0]
623
+
624
+ return FileResponse(
625
+ path=audio_path,
626
+ media_type="audio/" + audio_path.suffix.lstrip("."),
627
+ filename=audio_path.name
628
+ )
629
+
630
+
631
+ @router.get("/list_subtype_audios", tags=["Media Manager"])
632
+ def list_subtype_audios(
633
+ sha1: str = Query(..., description="SHA1 of the video folder"),
634
+ token: str = Query(..., description="Token required for authorization")
635
+ ):
636
+ """
637
+ List the most recent audio file for each subtype (HITL, MoE, Salamandra)
638
+ under /data/media/<sha1>.
639
+
640
+ Returns:
641
+ - sha1: folder name
642
+ - subtype: name of the subtype
643
+ - audio_name: latest audio file or None
644
+ """
645
+ validate_token(token)
646
+
647
+ results = []
648
+
649
+ for subtype in VALID_SUBTYPES:
650
+ subtype_dir = MEDIA_ROOT / sha1 / subtype
651
+
652
+ latest_audio = None
653
+
654
+ if subtype_dir.exists() and subtype_dir.is_dir():
655
+ files = []
656
+ for pattern in AUDIO_EXTENSIONS:
657
+ files.extend(list(subtype_dir.glob(pattern)))
658
+
659
+ if files:
660
+ # Sort by modification time (newest first)
661
+ files.sort(key=lambda f: f.stat().st_mtime, reverse=True)
662
+ latest_audio = files[0].name
663
+
664
+ results.append({
665
+ "sha1": sha1,
666
+ "subtype": subtype,
667
+ "audio_name": latest_audio
668
+ })
669
+
670
+ return results