4BY
4BY

Reputation: 77

Web2py type error retrieving image

Usually Web2py errors are easily understandable but this has me stumped.

i used an example from the web2py cookbook to make a thumbnail of an uploaded image. it generates a thumbnail but i can not retrieve the file. i get the following error:

<type 'exceptions.TypeError'>(Can't retrieve auth_user.thumb.a7433c1cbe652f44.jpg)

The file is in the uploads directory and can be viewed with an image viewer but i can not call it from the database. either from the Database admin or via a view.

Here is the Traceback when i click on the link from Database admin.

Traceback (most recent call last):
  File "/home/www-data/web2py/gluon/restricted.py", line 212, in restricted
    exec ccode in environment
  File "/home/www-data/web2py/applications/Urban_Gatherer/controllers/appadmin.py", line 464, in <module>
  File "/home/www-data/web2py/gluon/globals.py", line 194, in <lambda>
    self._caller = lambda f: f()
  File "/home/www-data/web2py/applications/Urban_Gatherer/controllers/appadmin.py", line 140, in download
    return response.download(request, db)
  File "/home/www-data/web2py/gluon/globals.py", line 407, in download
    (filename, stream) = field.retrieve(name,nameonly=True)
  File "/home/www-data/web2py/gluon/dal.py", line 9332, in retrieve
    raise TypeError('Can\'t retrieve %s' % name)
TypeError: Can't retrieve auth_user.thumb.a7433c1cbe652f44.jpg

I am not sure whats wrong.

Here is my controller

def make_thumb(table, image_id, size=(250, 250)):
  import os
  from PIL import Image
  this_image = table(image_id)
  im = Image.open(os.path.join(request.folder, 'uploads', this_image.avatar))
  im.thumbnail(size, Image.ANTIALIAS)
  thumb= 'auth_user.thumb.%s.jpg' % this_image.avatar.split('.')[2]
  im.save(os.path.join(request.folder, 'uploads', thumb), 'jpeg')
  this_image.update_record(thumb=thumb

)

i thought i was loosing something in the slice but i changed to 3 and i still seem to miss of the b16 part. Not sure why?

Upvotes: 0

Views: 334

Answers (1)

Anthony
Anthony

Reputation: 25536

auth_user.thumb.a7433c1cbe652f44.jpg isn't the proper format for an uploaded file. It is missing the b16encode'ed original filename, which should come right before the file extension. It should look something like this: auth_user.thumb.a7433c1cbe652f44.6d79207468756d626e61696c.jpg.

If this is the recipe you are using, try changing:

thumbnail = 'document.thumbnail.%s.jpg' % this_image.filename.split('.')[2]

to:

thumbnail = 'document.thumbnail.%s.%s.jpg' % tuple(this_image.filename.split('.')[2:4])

Upvotes: 1

Related Questions