ForcePilot/backend/package/yuxi/channel/extensions/bluebubbles/media.py

56 lines
1.4 KiB
Python
Raw Normal View History

import os
import logging
from pathlib import Path
logger = logging.getLogger(__name__)
def is_path_inside_root(file_path: str, root: str) -> bool:
try:
real_path = os.path.realpath(file_path)
real_root = os.path.realpath(root)
return os.path.commonpath([real_path, real_root]) == real_root
except (ValueError, OSError):
return False
def validate_media_path(file_path: str, local_roots: list[str]) -> bool:
if not local_roots:
return False
for root in local_roots:
if is_path_inside_root(file_path, root):
return True
return False
def get_media_size_mb(file_path: str) -> float:
try:
return os.path.getsize(file_path) / (1024 * 1024)
except OSError:
return 0.0
def sanitize_filename(filename: str) -> str:
base = os.path.basename(filename)
base = base.replace("\r", "").replace("\n", "")
return base
def guess_mime_type(file_path: str) -> str:
ext = Path(file_path).suffix.lower()
mime_map = {
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".png": "image/png",
".gif": "image/gif",
".webp": "image/webp",
".mp4": "video/mp4",
".mov": "video/quicktime",
".mp3": "audio/mpeg",
".caf": "audio/x-caf",
".m4a": "audio/mp4",
".pdf": "application/pdf",
".txt": "text/plain",
}
return mime_map.get(ext, "application/octet-stream")