Search code examples
pythongoogle-app-enginemimewebapp2

mimetypes.mime_guess() on google app engine behaves strange


In my python shell, i can do

>>> import mimetypes
>>> mimetypes.guess_type("ulla.svg")
('image/svg+xml', None)

And it behaves as expected, however, running the same code (or at least, this equal example) on google app engine, it returns (None, None)

class TestHandler(webapp2.RequestHandler):
    def get(self):
        import mimetypes
        self.response.out.write(mimetypes.guess_type("ulla.svg"))

Am i doing it wrong? :)

BTW - It's python 2.7 in my macbooks shell, and also 2.7 on app-engine


Solution

  • .svg is not included in the default types_map embbeded in the mimetypes module:

    >>> import mimetypes
    >>> print '.svg' in mimetypes.types_map
    False
    

    mimetypes module add additional extension/mimetypes from system files, and svg is defined on most distribution in /etc/mime.types

    $ cat /etc/mime.types  | grep svg
    image/svg+xml                   svg svgz
    

    But unfortunately it is not defined in the App Engine sandbox.

    You should fill a defect on the public issue tracker

    As a workaround you can register the mimetype yourself with mimetypes.add_type

    >>> import mimetypes
    >>> mimetypes.guess_type("ulla.svg")
    (None, None)
    >>> mimetypes.add_type("image/svg+xml", ".svg")
    >>> mimetypes.guess_type("ulla.svg")
    ('image/svg+xml', None)