function get_mime_type
Determines the MIME type of a file based on its file extension by mapping common extensions to their corresponding MIME type strings.
/tf/active/vicechatdev/CDocs/utils/__init__.py
135 - 157
simple
Purpose
This function provides a simple file extension to MIME type mapping for common document and image formats. It's useful for setting proper Content-Type headers when serving files, validating file uploads, or preparing files for transmission. The function returns a default MIME type ('application/octet-stream') for unknown extensions, making it safe to use with any filename.
Source Code
def get_mime_type(filename: str) -> str:
"""
Get MIME type for a file based on extension.
Args:
filename: Filename
Returns:
MIME type string
"""
ext = get_file_extension(filename)
mime_types = {
"pdf": "application/pdf",
"doc": "application/msword",
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"txt": "text/plain",
"rtf": "application/rtf",
"jpg": "image/jpeg",
"jpeg": "image/jpeg",
"png": "image/png",
"gif": "image/gif"
}
return mime_types.get(ext, "application/octet-stream")
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
filename |
str | - | positional_or_keyword |
Parameter Details
filename: A string representing the filename (with or without path). The function extracts the file extension from this string to determine the MIME type. Can be a full path like '/path/to/file.pdf' or just a filename like 'document.pdf'. Case handling depends on the get_file_extension function implementation.
Return Value
Type: str
Returns a string representing the MIME type of the file. For recognized extensions (pdf, doc, docx, txt, rtf, jpg, jpeg, png, gif), returns the appropriate MIME type (e.g., 'application/pdf', 'image/jpeg'). For unrecognized extensions, returns 'application/octet-stream' as a safe default for binary data.
Usage Example
# Assuming get_file_extension is defined elsewhere in the module
def get_file_extension(filename: str) -> str:
return filename.rsplit('.', 1)[-1].lower() if '.' in filename else ''
# Example usage
mime_type = get_mime_type('document.pdf')
print(mime_type) # Output: 'application/pdf'
mime_type = get_mime_type('/path/to/image.jpg')
print(mime_type) # Output: 'image/jpeg'
mime_type = get_mime_type('spreadsheet.xlsx')
print(mime_type) # Output: 'application/octet-stream' (unknown extension)
mime_type = get_mime_type('report.docx')
print(mime_type) # Output: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
Best Practices
- This function depends on get_file_extension being available in scope - ensure it's defined or imported before using get_mime_type
- The function only supports a limited set of file extensions - consider extending the mime_types dictionary for additional file types if needed
- Returns 'application/octet-stream' as a safe default for unknown extensions, which is appropriate for binary data but may need customization for specific use cases
- For production use, consider using Python's built-in mimetypes module (import mimetypes; mimetypes.guess_type()) which provides more comprehensive coverage
- The function assumes get_file_extension returns lowercase extensions - verify this behavior for case-sensitive scenarios
- Does not validate if the file actually exists or if its content matches the extension - this is purely extension-based detection
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_file_extension 63.2% similar
-
function is_valid_document_file 60.4% similar
-
function sanitize_filename 43.2% similar
-
function allowed_file 41.5% similar
-
function extract_metadata 41.1% similar