I know that I can accept image uploads by having a form that POSTs to App Engine like so:
<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 the 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 later showing it to the user? It seems the most robust way would be to figure this out from the image data itself, but how to get that easily? I did not see anything suitable in the google.appengine.api images package.
Should I just look for the magic image headers in my own code, or is there already a method for that somewhere?
Edit:
Here's the simplistic solution I ended up using, seems to work well enough for my purposes and avoids having to store the image type as a separate field in the data store:
# 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