Janusz Skonieczny
Janusz Skonieczny

Reputation: 18982

How to detect if a Blobstore entry is an image so get_serving_url would work?

I have a general purpose file storage backed by Google App Engine Blobstore, when I show users it's contents I would like to differentiate images from other files — I would like to show thumbnail for each image.

Python get_serving_url function does not care (at least at dev server) if given blob is in fact an image, java's getServingUrl throws en exception...

So my question, is: How to detect in python if a blob store entry is an image, so I could get a serving_url and use it in the UI?

EDIT:

On production python is throwing NotImageError on get_serving_url call with not supported blob—it's just not documented and it does not do that on dev server.

Upvotes: 1

Views: 941

Answers (2)

Matthew H
Matthew H

Reputation: 5879

You could putting the call inside a try...except block, catching the exception which is thrown when the object is found to not be an image.

Upvotes: 0

Andrew
Andrew

Reputation: 13191

Depending on how the images were uploaded to your Blobstore, they may all contain their MIME types, which you could try to use as a method of determining which items are most likely to contain valid image data using BlobInfo:

blob_info = BlobInfo.get(blob_image_key)

# All valid image formats for the GAE Images service.
image_types = ('image/bmp', 'image/jpeg', 'image/png', 
    'image/gif', 'image/tiff', 'image/x-icon')

if blob_info.content_type in image_types:
    # Obtain your serving URL.

Upvotes: 3

Related Questions