duck.http.mimes¶
Module Contents¶
Functions¶
Determine the MIME type of the provided content or file based on its initial bytes. |
|
Determine the MIME type of a file based on its filename or content. |
API¶
- duck.http.mimes.guess_data_mimetype(data: bytes) str[source]¶
Determine the MIME type of the provided content or file based on its initial bytes.
This function checks the initial bytes of the data to infer its MIME type by matching known signatures for various file types (e.g., images, text, compressed files, etc.). If no known signature is detected, it defaults to ‘application/octet-stream’.
- Parameters:
data – The input data for which the MIME type needs to be determined.
- Returns:
The determined MIME type of the input data.
- Return type:
str
- duck.http.mimes.guess_file_mimetype(filename: str) str[source]¶
Determine the MIME type of a file based on its filename or content.
This function first attempts to guess the MIME type using the file’s extension by leveraging the
mimetypesmodule. If the MIME type could not be determined from the filename, it reads the file content and attempts to infer the MIME type from the data.- Parameters:
filename – The path to the file for which the MIME type needs to be determined.
- Returns:
The determined MIME type of the file. Defaults to ‘application/octet-stream’ if the type cannot be determined.
- Return type:
str