Spaces:
Sleeping
Sleeping
Create utils/file_handler.py
Browse files- utils/file_handler.py +76 -0
utils/file_handler.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
File handling utilities
|
| 3 |
+
"""
|
| 4 |
+
import os
|
| 5 |
+
import shutil
|
| 6 |
+
import tempfile
|
| 7 |
+
from typing import Optional, List
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
import logging
|
| 10 |
+
|
| 11 |
+
logger = logging.getLogger(__name__)
|
| 12 |
+
|
| 13 |
+
class FileHandler:
|
| 14 |
+
def __init__(self, base_dir: str = "./cache"):
|
| 15 |
+
self.base_dir = Path(base_dir)
|
| 16 |
+
self.base_dir.mkdir(exist_ok=True)
|
| 17 |
+
self.temp_dir = self.base_dir / "temp"
|
| 18 |
+
self.temp_dir.mkdir(exist_ok=True)
|
| 19 |
+
|
| 20 |
+
def save_uploaded_file(self, uploaded_file, filename: str) -> str:
|
| 21 |
+
"""Save uploaded file to cache"""
|
| 22 |
+
try:
|
| 23 |
+
file_path = self.temp_dir / filename
|
| 24 |
+
shutil.copy(uploaded_file, file_path)
|
| 25 |
+
logger.info(f"Saved uploaded file: {file_path}")
|
| 26 |
+
return str(file_path)
|
| 27 |
+
except Exception as e:
|
| 28 |
+
logger.error(f"Failed to save uploaded file: {str(e)}")
|
| 29 |
+
raise
|
| 30 |
+
|
| 31 |
+
def create_temp_file(self, extension: str = ".wav") -> str:
|
| 32 |
+
"""Create temporary file path"""
|
| 33 |
+
temp_file = tempfile.NamedTemporaryFile(
|
| 34 |
+
suffix=extension,
|
| 35 |
+
dir=self.temp_dir,
|
| 36 |
+
delete=False
|
| 37 |
+
)
|
| 38 |
+
return temp_file.name
|
| 39 |
+
|
| 40 |
+
def cleanup_temp_files(self, max_age_hours: int = 24):
|
| 41 |
+
"""Clean up old temporary files"""
|
| 42 |
+
try:
|
| 43 |
+
current_time = time.time()
|
| 44 |
+
max_age_seconds = max_age_hours * 3600
|
| 45 |
+
|
| 46 |
+
for file_path in self.temp_dir.iterdir():
|
| 47 |
+
if file_path.is_file():
|
| 48 |
+
file_age = current_time - file_path.stat().st_mtime
|
| 49 |
+
if file_age > max_age_seconds:
|
| 50 |
+
file_path.unlink()
|
| 51 |
+
logger.info(f"Deleted old temp file: {file_path}")
|
| 52 |
+
except Exception as e:
|
| 53 |
+
logger.error(f"Cleanup failed: {str(e)}")
|
| 54 |
+
|
| 55 |
+
def get_file_size(self, file_path: str) -> int:
|
| 56 |
+
"""Get file size in bytes"""
|
| 57 |
+
return os.path.getsize(file_path)
|
| 58 |
+
|
| 59 |
+
def validate_audio_file(self, file_path: str) -> bool:
|
| 60 |
+
"""Validate if file is a supported audio format"""
|
| 61 |
+
supported_formats = ['.wav', '.mp3', '.flac', '.ogg', '.m4a']
|
| 62 |
+
file_ext = Path(file_path).suffix.lower()
|
| 63 |
+
return file_ext in supported_formats
|
| 64 |
+
|
| 65 |
+
def get_storage_info(self) -> dict:
|
| 66 |
+
"""Get storage usage information"""
|
| 67 |
+
total_size = sum(
|
| 68 |
+
f.stat().st_size for f in self.base_dir.rglob('*') if f.is_file()
|
| 69 |
+
)
|
| 70 |
+
return {
|
| 71 |
+
"total_size_mb": total_size / (1024 * 1024),
|
| 72 |
+
"file_count": len(list(self.base_dir.rglob('*'))),
|
| 73 |
+
"temp_dir_size_mb": sum(
|
| 74 |
+
f.stat().st_size for f in self.temp_dir.iterdir() if f.is_file()
|
| 75 |
+
) / (1024 * 1024)
|
| 76 |
+
}
|