Skip to content

Commit

Permalink
feat(utils): update mimetypes definition (#1445)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunato authored Dec 13, 2024
1 parent 132ff00 commit c44a5b8
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions eodag/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@
# default product-types start date
DEFAULT_MISSION_START_DATE = "2015-01-01T00:00:00Z"

# update missing mimetypes
mimetypes.add_type("text/xml", ".xsd")
mimetypes.add_type("application/x-grib", ".grib")
mimetypes.add_type("application/x-grib2", ".grib2")
# jp2 is missing on windows
mimetypes.add_type("image/jp2", ".jp2")


def _deprecated(reason: str = "", version: Optional[str] = None) -> Callable[..., Any]:
"""Simple decorator to mark functions/methods/classes as deprecated.
Expand Down Expand Up @@ -1412,17 +1419,32 @@ class StreamResponse:


def guess_file_type(file: str) -> Optional[str]:
"""guess the mime type of a file or URL based on its extension"""
mimetypes.add_type("text/xml", ".xsd")
mimetypes.add_type("application/x-grib", ".grib")
"""Guess the mime type of a file or URL based on its extension,
using eodag extended mimetypes definition
>>> guess_file_type('foo.tiff')
'image/tiff'
>>> guess_file_type('foo.grib')
'application/x-grib'
:param file: file url or path
:returns: guessed mime type
"""
mime_type, _ = mimetypes.guess_type(file, False)
return mime_type


def guess_extension(type: str) -> Optional[str]:
"""guess extension from mime type"""
mimetypes.add_type("text/xml", ".xsd")
mimetypes.add_type("application/x-grib", ".grib")
"""Guess extension from mime type, using eodag extended mimetypes definition
>>> guess_extension('image/tiff')
'.tiff'
>>> guess_extension('application/x-grib')
'.grib'
:param type: mime type
:returns: guessed file extension
"""
return mimetypes.guess_extension(type, strict=False)


Expand Down

0 comments on commit c44a5b8

Please sign in to comment.