VeuReu commited on
Commit
5e0eda9
·
verified ·
1 Parent(s): 149ffc4

Update storage/media_routers.py

Browse files
Files changed (1) hide show
  1. storage/media_routers.py +131 -1
storage/media_routers.py CHANGED
@@ -667,4 +667,134 @@ def list_subtype_audios(
667
  "audio_name": latest_audio
668
  })
669
 
670
- return results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
667
  "audio_name": latest_audio
668
  })
669
 
670
+ return results
671
+
672
+
673
+ @router.post("/upload_subtype_video", tags=["Media Manager"])
674
+ async def upload_subtype_video(
675
+ sha1: str = Query(..., description="SHA1 associated to the media folder"),
676
+ subtype: str = Query(..., description="Subtype: HITL, MoE or Salamandra"),
677
+ video: UploadFile = File(...),
678
+ token: str = Query(..., description="Token required for authorization")
679
+ ):
680
+ """
681
+ Upload a video to /data/media/<sha1>/<subtype>/.
682
+ Steps:
683
+ - Validate subtype.
684
+ - Create subtype folder if missing.
685
+ - Delete existing MP4 files.
686
+ - Save new MP4.
687
+ """
688
+ validate_token(token)
689
+
690
+ VALID_SUBTYPES = ("HITL", "MoE", "Salamandra")
691
+ if subtype not in VALID_SUBTYPES:
692
+ raise HTTPException(status_code=400, detail="Invalid subtype")
693
+
694
+ MEDIA_ROOT = Path("/data/media")
695
+ file_manager = FileManager(MEDIA_ROOT)
696
+
697
+ subtype_dir = MEDIA_ROOT / sha1 / subtype
698
+ subtype_dir.mkdir(parents=True, exist_ok=True)
699
+
700
+ # Remove old mp4 files
701
+ for f in subtype_dir.glob("*.mp4"):
702
+ f.unlink()
703
+
704
+ file_bytes = await video.read()
705
+ final_path = subtype_dir / video.filename
706
+
707
+ save_result = file_manager.upload_file(io.BytesIO(file_bytes), final_path)
708
+ if not save_result["operation_success"]:
709
+ raise HTTPException(status_code=500, detail=save_result["error"])
710
+
711
+ return {
712
+ "status": "ok",
713
+ "sha1": sha1,
714
+ "subtype": subtype,
715
+ "saved_to": str(final_path)
716
+ }
717
+
718
+
719
+ @router.get("/download_subtype_video", tags=["Media Manager"])
720
+ def download_subtype_video(
721
+ sha1: str,
722
+ subtype: str,
723
+ token: str = Query(..., description="Token required for authorization")
724
+ ):
725
+ """
726
+ Download the video stored under /data/media/<sha1>/<subtype>.
727
+ Returns the first MP4 found.
728
+ """
729
+ validate_token(token)
730
+
731
+ VALID_SUBTYPES = ("HITL", "MoE", "Salamandra")
732
+ if subtype not in VALID_SUBTYPES:
733
+ raise HTTPException(status_code=400, detail="Invalid subtype")
734
+
735
+ MEDIA_ROOT = Path("/data/media")
736
+ file_manager = FileManager(MEDIA_ROOT)
737
+
738
+ subtype_dir = MEDIA_ROOT / sha1 / subtype
739
+
740
+ if not subtype_dir.exists() or not subtype_dir.is_dir():
741
+ raise HTTPException(status_code=404, detail="Subtype folder not found")
742
+
743
+ mp4_files = list(subtype_dir.glob("*.mp4"))
744
+ if not mp4_files:
745
+ raise HTTPException(status_code=404, detail="No MP4 files found")
746
+
747
+ video_path = mp4_files[0]
748
+ relative_path = video_path.relative_to(MEDIA_ROOT)
749
+
750
+ handler = file_manager.get_file(relative_path)
751
+ if handler is None:
752
+ raise HTTPException(status_code=404, detail="Video not accessible")
753
+
754
+ handler.close()
755
+
756
+ return FileResponse(
757
+ path=video_path,
758
+ media_type="video/mp4",
759
+ filename=video_path.name
760
+ )
761
+
762
+
763
+ @router.get("/list_subtype_videos", tags=["Media Manager"])
764
+ def list_subtype_videos(
765
+ sha1: str,
766
+ token: str = Query(..., description="Token required for authorization")
767
+ ):
768
+ """
769
+ List the most recent .mp4 video for each subtype (HITL, MoE, Salamandra)
770
+ inside /data/media/<sha1>.
771
+ Returns:
772
+ - sha1
773
+ - subtype
774
+ - video_name (latest mp4 or None)
775
+ """
776
+ validate_token(token)
777
+
778
+ MEDIA_ROOT = Path("/data/media")
779
+ VALID_SUBTYPES = ("HITL", "MoE", "Salamandra")
780
+
781
+ results = []
782
+
783
+ for subtype in VALID_SUBTYPES:
784
+ subtype_dir = MEDIA_ROOT / sha1 / subtype
785
+
786
+ latest_video = None
787
+
788
+ if subtype_dir.exists() and subtype_dir.is_dir():
789
+ files = list(subtype_dir.glob("*.mp4"))
790
+ if files:
791
+ files.sort(key=lambda f: f.stat().st_mtime, reverse=True)
792
+ latest_video = files[0].name
793
+
794
+ results.append({
795
+ "sha1": sha1,
796
+ "subtype": subtype,
797
+ "video_name": latest_video
798
+ })
799
+
800
+ return results