Changeset 0a16e59b8359f2f6c6ebc082679c103d1817eb2b

Show
Ignore:
Timestamp:
10/22/09 20:06:19 (9 months ago)
Author:
Lincoln de Sousa <lincoln@…>
Parents:
ab4d5f5ab5431aa9f3164d7bbe3129d4a3733059
git-committer:
Lincoln de Sousa <lincoln@minaslivre.org> / 2009-10-22T20:06:19Z-0200
Message:

Implementing a stupid cache system to our thumbnails.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • contents/views.py

    rab4d5f5 r0a16e59  
    4949def picture_thumb(request, pid): 
    5050    obj = get_object_or_404(Picture, pk=pid) 
    51     img = Image.open(obj.image.path) 
    52     img.thumbnail(THUMB_SIZE, Image.ANTIALIAS) 
    53     buf = StringIO() 
    54     img.save(buf, 'PNG') 
    55     return HttpResponse(buf.getvalue(), content_type='image/png') 
     51    name, ext = os.path.splitext(obj.image.path) 
     52    nname = '%s.thumb%s' % (name, ext) 
     53 
     54    # Disk cache lookup 
     55    if os.path.exists(nname): 
     56        data = open(nname).read() 
     57    else: 
     58        img = Image.open(obj.image.path) 
     59        img.thumbnail(THUMB_SIZE, Image.ANTIALIAS) 
     60        buf = StringIO() 
     61        img.save(buf, 'PNG') 
     62        data = buf.getvalue() 
     63        open(nname, 'wb').write(data) 
     64    return HttpResponse(data, content_type='image/png') 
    5665 
    5766@login_required