I know that I can accept image uploads in the form of a POST for App Engine, for example:
<form action="/upload_received" enctype="multipart/form-data" method="post"> <div><input type="file" name="img"/></div> <div><input type="submit" value="Upload Image"></div> </form>
Then in Python code I can do something like
image = self.request.get("img")
But how can I figure out what the content type of this image should be when it is later shown to the user? It seems the most reliable way is to figure it out from the image data itself, but how to get it easy? I did not see anything suitable in the google.appengine.api image package.
Should I just look for the magic image headers in my own code, or is there some kind of method already?
Edit:
Here, the simplified solution that I ended up using seems to work well enough for my purposes and eliminates the need to save the image type as a separate field in the data warehouse:
# Given an image, returns the mime type or None if could not detect. def detect_mime_from_image_data(self, image): if image[1:4] == 'PNG': return 'image/png' if image[0:3] == 'GIF': return 'image/gif' if image[6:10] == 'JFIF': return 'image/jpeg' return None
source share