def _image(self): if not hasattr(self, '_cached_image'): was_closed = self.closed #self.open() # TODO: Use different methods of generating the file, rather than # just relying on PIL. from main.storage import FTPStorage, FTPStorageFile import os try: from cStringIO import StringIO except ImportError: from StringIO import StringIO from django.core.files.base import ContentFile memory_file = StringIO() storage = FTPStorage() storage._start_connection() storage._connection.cwd(os.path.dirname(self.name)) storage._connection.retrbinary( 'RETR ' + os.path.basename(self.name), memory_file.write) storage.disconnect() cf = ContentFile(memory_file.getvalue()) self._cached_image = Image.open(cf) # Image.open() is a lazy operation, so force the load so we # can close this file again if appropriate. self._cached_image.load() #if was_closed: #self.close() return self._cached_image
def _image(self): if not hasattr(self, '_cached_image'): was_closed = self.closed #self.open() # TODO: Use different methods of generating the file, rather than # just relying on PIL. from main.storage import FTPStorage, FTPStorageFile import os try: from cStringIO import StringIO except ImportError: from StringIO import StringIO from django.core.files.base import ContentFile memory_file = StringIO() storage = FTPStorage() storage._start_connection() storage._connection.cwd(os.path.dirname(self.name)) storage._connection.retrbinary('RETR ' + os.path.basename(self.name), memory_file.write) storage.disconnect() cf = ContentFile(memory_file.getvalue()) self._cached_image = Image.open(cf) # Image.open() is a lazy operation, so force the load so we # can close this file again if appropriate. self._cached_image.load() #if was_closed: #self.close() return self._cached_image
def write_image_to_ftp(image, name): from main.storage import FTPStorage, FTPStorageFile import os try: from cStringIO import StringIO except ImportError: from StringIO import StringIO memory_file = StringIO() from main.storage import FTPStorageFile, FTPStorage image.save(memory_file, format='JPEG') storage = FTPStorage() storage._start_connection() storage._put_file(name, memory_file.getvalue()) memory_file.close()