diff --git a/server.py b/server.py index 1aa8332..69797c8 100644 --- a/server.py +++ b/server.py @@ -2162,7 +2162,28 @@ async def get_cached_raw(content_hash: str): cache_path = get_cache_path(content_hash) if not cache_path: raise HTTPException(404, f"Content {content_hash} not in cache") - return FileResponse(cache_path) + + # Detect media type and set appropriate content-type and filename + media_type_name = detect_media_type(cache_path) + if media_type_name == "video": + # Check actual format + with open(cache_path, "rb") as f: + header = f.read(12) + if header[:4] == b'\x1a\x45\xdf\xa3': # WebM/MKV + return FileResponse(cache_path, media_type="video/x-matroska", filename=f"{content_hash}.mkv") + elif header[4:8] == b'ftyp': # MP4 + return FileResponse(cache_path, media_type="video/mp4", filename=f"{content_hash}.mp4") + return FileResponse(cache_path, media_type="video/mp4", filename=f"{content_hash}.mp4") + elif media_type_name == "image": + with open(cache_path, "rb") as f: + header = f.read(8) + if header[:8] == b'\x89PNG\r\n\x1a\n': + return FileResponse(cache_path, media_type="image/png", filename=f"{content_hash}.png") + elif header[:2] == b'\xff\xd8': + return FileResponse(cache_path, media_type="image/jpeg", filename=f"{content_hash}.jpg") + return FileResponse(cache_path, media_type="image/jpeg", filename=f"{content_hash}.jpg") + + return FileResponse(cache_path, filename=f"{content_hash}.bin") @app.get("/cache/{content_hash}/mp4") @@ -3609,10 +3630,10 @@ def is_ios_request(request: Request) -> bool: def video_src_for_request(content_hash: str, request: Request) -> str: - """Get video src URL, using MP4 endpoint for iOS.""" + """Get video src URL, using MP4 endpoint for iOS, raw for others.""" if is_ios_request(request): return f"/cache/{content_hash}/mp4" - return f"/cache/{content_hash}" + return f"/cache/{content_hash}/raw" def detect_media_type(cache_path: Path) -> str: