I currently generate thumbnails for image uploads by hooking the save method of the model the Filefield is in however, for numerous reasons, this really sucks. It does work though, unlike my earlier experiments with custom upload handlers…
This strike me as the appropriate juncture to create and update a thumbnail BUT I came unstuck trying to find out where the uploaded file would eventually be saved (so I can save the thumbnail to a path based on that). I can get the temp folder path easy enough but that’s no use, I need to know which folder the upload will finally uploaded to, is this possible from here?
class Thumbnailer(TemporaryFileUploadHandler):
def file_complete(self, file_size):
self.file.seek(0)
if not self.content_type is None and 'image' in self.content_type:
newfile = StringIO()
img = Image.open( self.file )
thumb_size = 400,300
img.thumbnail( thumb_size, Image.ANTIALIAS )
img.save( os.path.join("THIS PATH IS WHAT I NEED", "thumbs", self.file_name, 'JPEG', quality=80 )
return super(Thumbnailer, self).file_complete(file_size)
ps: please don’t answer by recommending plug ins and mix-ins for thumb-nailing – I know about and have disregarded all those, for this project at least..
file upload handler doesn’t know where the uploaded file will be stored, so if you really have to stick up with custom upload handler you will have to hardcode that path.
Otherwise, you can: