|
| 1 | +"""Custom MIME types handler for ZeroConfigDLNA. |
| 2 | +
|
| 3 | +This module provides a custom implementation of MIME type detection |
| 4 | +that uses a local mime.types file instead of the system default. |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +import re |
| 9 | +from typing import Dict, List, Optional, Tuple |
| 10 | + |
| 11 | + |
| 12 | +class CustomMimeTypes: |
| 13 | + """ |
| 14 | + A custom implementation of the mimetypes module that reads from |
| 15 | + a local mime.types file in the current directory. |
| 16 | + """ |
| 17 | + |
| 18 | + def __init__(self, mime_file_path: Optional[str] = None): |
| 19 | + """ |
| 20 | + Initialize the CustomMimeTypes object. |
| 21 | +
|
| 22 | + Args: |
| 23 | + mime_file_path: Path to the mime.types file. If None, looks for |
| 24 | + mime.types in the same directory as this module. |
| 25 | + """ |
| 26 | + self.types_map: Dict[str, str] = {} |
| 27 | + self.extensions_map: Dict[str, str] = {} |
| 28 | + |
| 29 | + # If no path provided, use the mime.types file in the same directory as this module |
| 30 | + if mime_file_path is None: |
| 31 | + mime_file_path = os.path.join(os.path.dirname(__file__), "mime.types") |
| 32 | + |
| 33 | + # Load the mime types from the file |
| 34 | + self._load_mime_types(mime_file_path) |
| 35 | + |
| 36 | + def _load_mime_types(self, mime_file_path: str) -> None: |
| 37 | + """ |
| 38 | + Load MIME types from the specified file. |
| 39 | +
|
| 40 | + Args: |
| 41 | + mime_file_path: Path to the mime.types file. |
| 42 | + """ |
| 43 | + try: |
| 44 | + with open(mime_file_path, "r", encoding="utf-8") as f: |
| 45 | + for line in f: |
| 46 | + # Skip comments and empty lines |
| 47 | + line = line.strip() |
| 48 | + if not line or line.startswith("#"): |
| 49 | + continue |
| 50 | + |
| 51 | + # Parse the line: mime_type extension [extension ...] |
| 52 | + parts = line.split() |
| 53 | + if len(parts) < 2: |
| 54 | + continue |
| 55 | + |
| 56 | + mime_type = parts[0].lower() |
| 57 | + for ext in parts[1:]: |
| 58 | + ext = ext.lower() |
| 59 | + if not ext.startswith("."): |
| 60 | + ext = "." + ext |
| 61 | + |
| 62 | + # Map extension to MIME type |
| 63 | + self.types_map[ext] = mime_type |
| 64 | + |
| 65 | + # Map MIME type to first extension for that type |
| 66 | + if mime_type not in self.extensions_map: |
| 67 | + self.extensions_map[mime_type] = ext |
| 68 | + except Exception as e: |
| 69 | + print(f"Error loading mime types from {mime_file_path}: {e}") |
| 70 | + # Initialize with basic MIME types if file loading fails |
| 71 | + self._init_basic_types() |
| 72 | + |
| 73 | + def _init_basic_types(self) -> None: |
| 74 | + """Initialize with basic MIME types to ensure operation even if file loading fails.""" |
| 75 | + # Common video formats |
| 76 | + for ext in [".mp4", ".mkv", ".avi", ".mov", ".wmv", ".flv", ".webm"]: |
| 77 | + self.types_map[ext] = ( |
| 78 | + "video/mp4" |
| 79 | + if ext == ".mp4" |
| 80 | + else ( |
| 81 | + "video/x-matroska" |
| 82 | + if ext == ".mkv" |
| 83 | + else ( |
| 84 | + "video/x-msvideo" |
| 85 | + if ext == ".avi" |
| 86 | + else ( |
| 87 | + "video/quicktime" |
| 88 | + if ext == ".mov" |
| 89 | + else ( |
| 90 | + "video/x-ms-wmv" |
| 91 | + if ext == ".wmv" |
| 92 | + else "video/x-flv" if ext == ".flv" else "video/webm" |
| 93 | + ) |
| 94 | + ) |
| 95 | + ) |
| 96 | + ) |
| 97 | + ) |
| 98 | + |
| 99 | + # Common audio formats |
| 100 | + for ext in [".mp3", ".wav", ".ogg", ".aac", ".flac"]: |
| 101 | + self.types_map[ext] = ( |
| 102 | + "audio/mpeg" |
| 103 | + if ext == ".mp3" |
| 104 | + else ( |
| 105 | + "audio/wav" |
| 106 | + if ext == ".wav" |
| 107 | + else ( |
| 108 | + "audio/ogg" |
| 109 | + if ext == ".ogg" |
| 110 | + else "audio/aac" if ext == ".aac" else "audio/flac" |
| 111 | + ) |
| 112 | + ) |
| 113 | + ) |
| 114 | + |
| 115 | + # Common image formats |
| 116 | + for ext in [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp"]: |
| 117 | + self.types_map[ext] = ( |
| 118 | + "image/jpeg" |
| 119 | + if ext in [".jpg", ".jpeg"] |
| 120 | + else ( |
| 121 | + "image/png" |
| 122 | + if ext == ".png" |
| 123 | + else ( |
| 124 | + "image/gif" |
| 125 | + if ext == ".gif" |
| 126 | + else "image/bmp" if ext == ".bmp" else "image/webp" |
| 127 | + ) |
| 128 | + ) |
| 129 | + ) |
| 130 | + |
| 131 | + # Create the extensions map |
| 132 | + for ext, mime_type in self.types_map.items(): |
| 133 | + if mime_type not in self.extensions_map: |
| 134 | + self.extensions_map[mime_type] = ext |
| 135 | + |
| 136 | + def guess_type( |
| 137 | + self, url: str, strict: bool = True |
| 138 | + ) -> Tuple[Optional[str], Optional[str]]: |
| 139 | + """ |
| 140 | + Guess the MIME type of a file based on its URL/filename. |
| 141 | +
|
| 142 | + Args: |
| 143 | + url: URL or filename to guess the MIME type for. |
| 144 | + strict: Whether to be strict about the guessing. |
| 145 | + If True, only return types found in the mime.types file. |
| 146 | +
|
| 147 | + Returns: |
| 148 | + A tuple (type, encoding) where type is the MIME type and |
| 149 | + encoding is the encoding (always None in this implementation). |
| 150 | + """ |
| 151 | + # Extract the filename from the URL and convert to lowercase |
| 152 | + filename = os.path.basename(url.lower()) |
| 153 | + |
| 154 | + # Get the extension |
| 155 | + _, ext = os.path.splitext(filename) |
| 156 | + |
| 157 | + # Return the MIME type if found, otherwise None |
| 158 | + return (self.types_map.get(ext), None) |
| 159 | + |
| 160 | + def guess_extension(self, mime_type: str, strict: bool = True) -> Optional[str]: |
| 161 | + """ |
| 162 | + Guess the extension for a given MIME type. |
| 163 | +
|
| 164 | + Args: |
| 165 | + mime_type: The MIME type to guess the extension for. |
| 166 | + strict: Whether to be strict about the guessing. |
| 167 | + If True, only return extensions found in the mime.types file. |
| 168 | +
|
| 169 | + Returns: |
| 170 | + The extension including the leading dot, or None if not found. |
| 171 | + """ |
| 172 | + # Convert to lowercase |
| 173 | + mime_type = mime_type.lower() |
| 174 | + |
| 175 | + # Return the extension if found, otherwise None |
| 176 | + return self.extensions_map.get(mime_type) |
| 177 | + |
| 178 | + def add_type(self, mime_type: str, ext: str, strict: bool = True) -> None: |
| 179 | + """ |
| 180 | + Add a MIME type to the map. |
| 181 | +
|
| 182 | + Args: |
| 183 | + mime_type: The MIME type to add. |
| 184 | + ext: The extension to associate with the MIME type. |
| 185 | + strict: Ignored, included for compatibility. |
| 186 | + """ |
| 187 | + if not ext.startswith("."): |
| 188 | + ext = "." + ext |
| 189 | + |
| 190 | + mime_type = mime_type.lower() |
| 191 | + ext = ext.lower() |
| 192 | + |
| 193 | + self.types_map[ext] = mime_type |
| 194 | + if mime_type not in self.extensions_map: |
| 195 | + self.extensions_map[mime_type] = ext |
| 196 | + |
| 197 | + def read(self, filename: str, strict: bool = True) -> None: |
| 198 | + """ |
| 199 | + Read MIME types from a file. |
| 200 | +
|
| 201 | + Args: |
| 202 | + filename: Path to the file to read. |
| 203 | + strict: Ignored, included for compatibility. |
| 204 | + """ |
| 205 | + self._load_mime_types(filename) |
| 206 | + |
| 207 | + |
| 208 | +# Create a singleton instance |
| 209 | +mime_types = CustomMimeTypes() |
| 210 | + |
| 211 | + |
| 212 | +# Provide functions that mimic the standard mimetypes module |
| 213 | +def guess_type(url: str, strict: bool = True) -> Tuple[Optional[str], Optional[str]]: |
| 214 | + """ |
| 215 | + Guess the MIME type of a file based on its URL/filename. |
| 216 | +
|
| 217 | + Args: |
| 218 | + url: URL or filename to guess the MIME type for. |
| 219 | + strict: Whether to be strict about the guessing. |
| 220 | +
|
| 221 | + Returns: |
| 222 | + A tuple (type, encoding) where type is the MIME type and |
| 223 | + encoding is the encoding (always None in this implementation). |
| 224 | + """ |
| 225 | + return mime_types.guess_type(url, strict) |
| 226 | + |
| 227 | + |
| 228 | +def guess_extension(mime_type: str, strict: bool = True) -> Optional[str]: |
| 229 | + """ |
| 230 | + Guess the extension for a given MIME type. |
| 231 | +
|
| 232 | + Args: |
| 233 | + mime_type: The MIME type to guess the extension for. |
| 234 | + strict: Whether to be strict about the guessing. |
| 235 | +
|
| 236 | + Returns: |
| 237 | + The extension including the leading dot, or None if not found. |
| 238 | + """ |
| 239 | + return mime_types.guess_extension(mime_type, strict) |
| 240 | + |
| 241 | + |
| 242 | +def add_type(mime_type: str, ext: str, strict: bool = True) -> None: |
| 243 | + """ |
| 244 | + Add a MIME type to the map. |
| 245 | +
|
| 246 | + Args: |
| 247 | + mime_type: The MIME type to add. |
| 248 | + ext: The extension to associate with the MIME type. |
| 249 | + strict: Ignored, included for compatibility. |
| 250 | + """ |
| 251 | + mime_types.add_type(mime_type, ext, strict) |
| 252 | + |
| 253 | + |
| 254 | +def read(filename: str, strict: bool = True) -> None: |
| 255 | + """ |
| 256 | + Read MIME types from a file. |
| 257 | +
|
| 258 | + Args: |
| 259 | + filename: Path to the file to read. |
| 260 | + strict: Ignored, included for compatibility. |
| 261 | + """ |
| 262 | + mime_types.read(filename, strict) |
0 commit comments