def get_thumbnail(self, file_, geometry_string, **options): """ Returns thumbnail as an ImageFile instance for file with geometry and options given. First it will try to get it from the key value store, secondly it will create it. """ source = ImageFile(file_) for key, value in self.default_options.iteritems(): options.setdefault(key, value) name = self._get_thumbnail_filename(source, geometry_string, options) thumbnail = ImageFile(name, default.storage) cached = default.kvstore.get(thumbnail) if cached: return cached if not thumbnail.exists(): # We have to check exists() because the Storage backend does not # overwrite in some implementations. source_image = default.engine.get_image(source) # We might as well set the size since we have the image in memory size = default.engine.get_image_size(source_image) source.set_size(size) self._create_thumbnail(source_image, geometry_string, options, thumbnail) # If the thumbnail exists we don't create it, the other option is # to delete and write but this could lead to race conditions so I # will just leave that out for now. default.kvstore.get_or_set(source) default.kvstore.set(thumbnail, source) return thumbnail
def get_thumbnail(self, file_, geometry_string, **options): """ Returns thumbnail as an ImageFile instance for file with geometry and options given. First it will try to get it from the key value store, secondly it will create it. """ source = ImageFile(file_) for key, value in self.default_options.iteritems(): options.setdefault(key, value) name = self._get_thumbnail_filename(source, geometry_string, options) thumbnail = ImageFile(name, default.storage) cached = default.kvstore.get(thumbnail) if cached: return cached if not thumbnail.exists(): # We have to check exists() because the Storage backend does not # overwrite in some implementations. source_image = default.engine.get_image(source) # We might as well set the size since we have the image in memory size = default.engine.get_image_size(source_image) source.set_size(size) self._create_thumbnail(source_image, geometry_string, options, thumbnail) # If the thumbnail exists we don't create it, the other option is # to delete and write but this could lead to race conditions so I # will just leave that out for now. default.kvstore.get_or_set(source) default.kvstore.set(thumbnail, source) return thumbnail
def __init__(self, *args, **kwargs): super(CachedImageFile, self).__init__(*args, **kwargs) self._exists = ImageFile.exists(self) try: self._content = ImageFile.read(self) except Exception as exception: self._exception = exception else: self._exception = None
def create_thumbnail(file_, geometry_string, options, name): thumbnail = ImageFile(name, default.storage) if not thumbnail.exists(): source = ImageFile(file_) source_image = default.engine.get_image(source) size = default.engine.get_image_size(source_image) source.set_size(size) default.backend._create_thumbnail(source_image, geometry_string, options, thumbnail) # Need to update both the source and the thumbnail with correct sizing default.kvstore.set(source) default.kvstore.set(thumbnail, source)
def get_thumbnail(self, file_, geometry_string, **options): """ Returns thumbnail as an ImageFile instance for file with geometry and options given. First it will try to get it from the key value store, secondly it will create it. """ if not options.get('format'): ext = str(file_).split('.')[-1].lower() options['format'] = ext.upper() if ext in AUTO_FORMATS else settings.THUMBNAIL_FORMAT source = ImageFile(file_) for key, value in self.default_options.items(): options.setdefault(key, value) # For the future I think it is better to add options only if they # differ from the default settings as below. This will ensure the same # filenames beeing generated for new options at default. for key, attr in self.extra_options: value = getattr(settings, attr) if value != getattr(default_settings, attr): options.setdefault(key, value) name = self._get_thumbnail_filename(source, geometry_string, options) thumbnail = ImageFile(name, default.storage) cached = default.kvstore.get(thumbnail) if cached: return cached if not thumbnail.exists(): # We have to check exists() because the Storage backend does not # overwrite in some implementations. try: source_image = default.engine.get_image(source) except IOError: if settings.THUMBNAIL_IMAGE_MISSING_DUMMY: return DummyImageFile(geometry_string) else: raise # We might as well set the size since we have the image in memory size = default.engine.get_image_size(source_image) source.set_size(size) self._create_thumbnail(source_image, geometry_string, options, thumbnail) self._create_alternative_resolutions(source, geometry_string, options, thumbnail.name) # If the thumbnail exists we don't create it, the other option is # to delete and write but this could lead to race conditions so I # will just leave that out for now. default.kvstore.get_or_set(source) default.kvstore.set(thumbnail, source) return thumbnail
def get_thumbnail(self, file_, geometry_string, force_create=True, **options): """ Returns thumbnail as an ImageFile instance for file with geometry and options given. First it will try to get it from the key value store, secondly it will create it. """ source = ImageFile(file_) if settings.THUMBNAIL_PRESERVE_FORMAT: format = self._get_format(file_) if format == 'GIF': options.setdefault('format', format) for key, value in self.default_options.iteritems(): options.setdefault(key, value) # For the future I think it is better to add options only if they # differ from the default settings as below. This will ensure the same # filenames beeing generated for new options at default. for key, attr in self.extra_options: value = getattr(settings, attr) if value != getattr(default_settings, attr): options.setdefault(key, value) name = self._get_thumbnail_filename(source, geometry_string, options) thumbnail = ImageFile(name, default.storage) cached = default.kvstore.get(thumbnail) if cached: return cached if not force_create: return source if not thumbnail.exists(): # We have to check exists() because the Storage backend does not # overwrite in some implementations. source_image = default.engine.get_image(source) # We might as well set the size since we have the image in memory size = default.engine.get_image_size(source_image) source.set_size(size) self._create_thumbnail(source_image, geometry_string, options, thumbnail) # If the thumbnail exists we don't create it, the other option is # to delete and write but this could lead to race conditions so I # will just leave that out for now. default.kvstore.get_or_set(source) default.kvstore.set(thumbnail, source) return thumbnail
def get_thumbnail(self, file_, geometry_string, **options): """ Returns thumbnail as an ImageFile instance for file with geometry and options given. First it will try to get it from the key value store, secondly it will create it. """ source = ImageFile(file_) for key, value in self.default_options.iteritems(): options.setdefault(key, value) # For the future I think it is better to add options only if they # differ from the default settings as below. This will ensure the same # filenames beeing generated for new options at default. for key, attr in self.extra_options: value = getattr(settings, attr) if value != getattr(default_settings, attr): options.setdefault(key, value) name = self._get_thumbnail_filename(source, geometry_string, options) thumbnail = ImageFile(name, default.storage) cached = default.kvstore.get(thumbnail) if cached: return cached if not thumbnail.exists(): # We have to check exists() because the Storage backend does not # overwrite in some implementations. # so we make the assumption that if the thumbnail is not cached, it doesn't exist try: source_image = default.engine.get_image(source) except IOError: # if S3Storage says file doesn't exist remotely, don't try to # create it, exit early # Will return working empty image type; 404'd image logger.warn('Remote file [%s] at [%s] does not exist', file_, geometry_string) return thumbnail # We might as well set the size since we have the image in memory image_info = default.engine.get_image_info(source_image) options['image_info'] = image_info size = default.engine.get_image_size(source_image) source.set_size(size) self._create_thumbnail(source_image, geometry_string, options, thumbnail) # If the thumbnail exists we don't create it, the other option is # to delete and write but this could lead to race conditions so I # will just leave that out for now. default.kvstore.get_or_set(source) default.kvstore.set(thumbnail, source) return thumbnail
def get_thumbnail(self, file_, geometry_string, **options): """ Returns thumbnail as an ImageFile instance for file with geometry and options given. First it will try to get it from the key value store, secondly it will create it. """ source = ImageFile(file_) for key, value in self.default_options.iteritems(): options.setdefault(key, value) # For the future I think it is better to add options only if they # differ from the default settings as below. This will ensure the same # filenames beeing generated for new options at default. for key, attr in self.extra_options: value = getattr(settings, attr) if value != getattr(default_settings, attr): options.setdefault(key, value) name = self._get_thumbnail_filename(source, geometry_string, options) thumbnail = ImageFile(name, default.storage) cached = default.kvstore.get(thumbnail) if cached: return cached if not thumbnail.exists(): # We have to check exists() because the Storage backend does not # overwrite in some implementations. # so we make the assumption that if the thumbnail is not cached, it doesn't exist try: source_image = default.engine.get_image(source) except IOError: # if S3Storage says file doesn't exist remotely, don't try to # create it, exit early # Will return working empty image type; 404'd image logger.warn('Remote file [%s] at [%s] does not exist', file_, geometry_string) return thumbnail # We might as well set the size since we have the image in memory image_info = default.engine.get_image_info(source_image) options['image_info'] = image_info size = default.engine.get_image_size(source_image) source.set_size(size) self._create_thumbnail(source_image, geometry_string, options, thumbnail) # If the thumbnail exists we don't create it, the other option is # to delete and write but this could lead to race conditions so I # will just leave that out for now. default.kvstore.get_or_set(source) default.kvstore.set(thumbnail, source) return thumbnail
def create_thumbnail(file_, geometry_string, options, name, force=False): if file_: source = ImageFile(file_) else: return thumbnail = ImageFile(name, default.storage) # We have to check exists() because the Storage backend does not # overwrite in some implementations. if settings.THUMBNAIL_FORCE_OVERWRITE or not thumbnail.exists() or force: try: source_image = default.engine.get_image(source) except IOError as e: logger.exception(e) # if S3Storage says file doesn't exist remotely, don't try to # create it and exit early. # Will return working empty image type; 404'd image logger.warn( text_type('Remote file [%s] at [%s] does not exist'), file_, geometry_string) return # We might as well set the size since we have the image in memory try: image_info = default.engine.get_image_info(source_image) options['image_info'] = image_info except AttributeError: options['image_info'] = {} size = default.engine.get_image_size(source_image) source.set_size(size) try: default.backend._create_thumbnail( source_image, geometry_string, options, thumbnail) default.backend._create_alternative_resolutions( source_image, geometry_string, options, thumbnail.name) finally: default.engine.cleanup(source_image) # If the thumbnail exists we don't create it, the other option is # to delete and write but this could lead to race conditions so I # will just leave that out for now. default.kvstore.get_or_set(source) default.kvstore.set(thumbnail, source)
def create_thumbnail(file_, geometry_string, options, name, force=False): if file_: source = ImageFile(file_) else: return thumbnail = ImageFile(name, default.storage) # We have to check exists() because the Storage backend does not # overwrite in some implementations. if settings.THUMBNAIL_FORCE_OVERWRITE or not thumbnail.exists() or force: try: source_image = default.engine.get_image(source) except IOError as e: logger.exception(e) # if S3Storage says file doesn't exist remotely, don't try to # create it and exit early. # Will return working empty image type; 404'd image logger.warn( "Remote file [{}}] at [{}}] does not exist".format(file_, geometry_string) ) return # We might as well set the size since we have the image in memory try: image_info = default.engine.get_image_info(source_image) options['image_info'] = image_info except AttributeError: options['image_info'] = {} size = default.engine.get_image_size(source_image) source.set_size(size) try: default.backend._create_thumbnail( source_image, geometry_string, options, thumbnail) default.backend._create_alternative_resolutions( source_image, geometry_string, options, thumbnail.name) finally: default.engine.cleanup(source_image) # If the thumbnail exists we don't create it, the other option is # to delete and write but this could lead to race conditions so I # will just leave that out for now. default.kvstore.get_or_set(source) default.kvstore.set(thumbnail, source)
def get_thumbnail(self, file_, geometry_string, **options): """ Returns thumbnail as an ImageFile instance for file with geometry and options given. First it will try to get it from the key value store, secondly it will create it. """ source = ImageFile(file_) for key, value in self.default_options.iteritems(): options.setdefault(key, value) options['mtime'] = os.path.getmtime(source.storage.path(source))###customization name = self._get_thumbnail_filename(source, geometry_string, options) thumbnail = ImageFile(name, default.storage) cached = default.kvstore.get(thumbnail) if cached and cached.exists():###customization return cached if not thumbnail.exists(): # We have to check exists() because the Storage backend does not # overwrite in some implementations. source_image = default.engine.get_image(source) size = default.engine.get_image_size(source_image) if options.get('autocrop', None): source_image = autocrop(source_image, geometry_string, options) # We might as well set the size since we have the image in memory size = default.engine.get_image_size(source_image) source.set_size(size) ### customization: race condition, do not raise an OSError when the dir exists. # see sorl.thumbnail.images.ImageFile.write, it's not safe to simply throw # /sub/dir/name.jpg to django.core.files.storage.FileSystemStorage._save full_path = thumbnail.storage.path(name) directory = os.path.dirname(full_path) if not os.path.exists(directory): try: os.makedirs(directory) except OSError: pass ### end of customization self._create_thumbnail(source_image, geometry_string, options, thumbnail) # If the thumbnail exists we don't create it, the other option is # to delete and write but this could lead to race conditions so I # will just leave that out for now. default.kvstore.get_or_set(source) default.kvstore.set(thumbnail, source) return thumbnail
def get_thumbnail(self, file_, geometry_string, force_create=True, **options): """ Returns thumbnail as an ImageFile instance for file with geometry and options given. First it will try to get it from the key value store, secondly it will create it. """ source = ImageFile(file_) if settings.THUMBNAIL_PRESERVE_FORMAT: format = self._get_format(file_) if format == "GIF": options.setdefault("format", format) for key, value in self.default_options.iteritems(): options.setdefault(key, value) # For the future I think it is better to add options only if they # differ from the default settings as below. This will ensure the same # filenames beeing generated for new options at default. for key, attr in self.extra_options: value = getattr(settings, attr) if value != getattr(default_settings, attr): options.setdefault(key, value) name = self._get_thumbnail_filename(source, geometry_string, options) thumbnail = ImageFile(name, default.storage) cached = default.kvstore.get(thumbnail) if cached: return cached if not force_create: return source if not thumbnail.exists(): # We have to check exists() because the Storage backend does not # overwrite in some implementations. source_image = default.engine.get_image(source) # We might as well set the size since we have the image in memory size = default.engine.get_image_size(source_image) source.set_size(size) self._create_thumbnail(source_image, geometry_string, options, thumbnail) # If the thumbnail exists we don't create it, the other option is # to delete and write but this could lead to race conditions so I # will just leave that out for now. default.kvstore.get_or_set(source) default.kvstore.set(thumbnail, source) return thumbnail
def get_thumbnail(self, file_, geometry_string, **options): """ Returns thumbnail as an ImageFile instance for file with geometry and options given. First it will try to get it from the key value store, secondly it will create it. """ source = ImageFile(file_) for key, value in self.default_options.iteritems(): options.setdefault(key, value) # For the future I think it is better to add options only if they # differ from the default settings as below. This will ensure the same # filenames beeing generated for new options at default. for key, attr in self.extra_options: value = getattr(settings, attr) if value != getattr(default_settings, attr): options.setdefault(key, value) name = self._get_thumbnail_filename(source, geometry_string, options) thumbnail = ImageFile(name, default.storage) cached = default.kvstore.get(thumbnail) if cached: return cached if not thumbnail.exists(): # We have to check exists() because the Storage backend does not # overwrite in some implementations. try: source_image = default.engine.get_image(source) # We might as well set the size since we have the image in memory size = default.engine.get_image_size(source_image) source.set_size(size) self._create_thumbnail(source_image, geometry_string, options, thumbnail) self._create_alternative_resolutions(source_image, geometry_string, options, thumbnail.name) except IOError, e: if settings.THUMBNAIL_DUMMY: return DummyImageFile(geometry_string) else: raise e
def get_thumbnail(self, file_, geometry_string, **options): """ Returns thumbnail as an ImageFile instance for file with geometry and options given. First it will try to get it from the key value store, secondly it will create it. """ logger.debug('Getting thumbnail for file [%s] at [%s]', file_, geometry_string) if file_: source = ImageFile(file_) elif settings.THUMBNAIL_DUMMY: return DummyImageFile(geometry_string) else: return None # preserve image filetype if settings.THUMBNAIL_PRESERVE_FORMAT: options.setdefault('format', self._get_format(source)) for key, value in self.default_options.items(): options.setdefault(key, value) # For the future I think it is better to add options only if they # differ from the default settings as below. This will ensure the same # filenames being generated for new options at default. for key, attr in self.extra_options: value = getattr(settings, attr) if value != getattr(default_settings, attr): options.setdefault(key, value) name = self._get_thumbnail_filename(source, geometry_string, options) thumbnail = ImageFile(name, default.storage) cached = default.kvstore.get(thumbnail) if cached: return cached # We have to check exists() because the Storage backend does not # overwrite in some implementations. if settings.THUMBNAIL_FORCE_OVERWRITE or not thumbnail.exists(): try: source_image = default.engine.get_image(source) except IOError as e: logger.exception(e) if settings.THUMBNAIL_DUMMY: return DummyImageFile(geometry_string) else: # if S3Storage says file doesn't exist remotely, don't try to # create it and exit early. # Will return working empty image type; 404'd image logger.warning( 'Remote file [%s] at [%s] does not exist', file_, geometry_string, ) return thumbnail # We might as well set the size since we have the image in memory image_info = default.engine.get_image_info(source_image) options['image_info'] = image_info size = default.engine.get_image_size(source_image) source.set_size(size) try: self._create_thumbnail(source_image, geometry_string, options, thumbnail) self._create_alternative_resolutions(source_image, geometry_string, options, thumbnail.name) finally: default.engine.cleanup(source_image) # If the thumbnail exists we don't create it, the other option is # to delete and write but this could lead to race conditions so I # will just leave that out for now. default.kvstore.get_or_set(source) default.kvstore.set(thumbnail, source) return thumbnail
def get_thumbnail(self, file_, geometry_string, **options): """ Returns thumbnail as an ImageFile instance for file with geometry and options given. First it will try to get it from the key value store, secondly it will create it. """ logger.debug('Getting thumbnail for file [%s] at [%s]', file_, geometry_string) if file_: source = ImageFile(file_) else: raise ValueError('falsey file_ argument in get_thumbnail()') # preserve image filetype if settings.THUMBNAIL_PRESERVE_FORMAT: options.setdefault('format', self._get_format(source)) for key, value in self.default_options.items(): options.setdefault(key, value) # For the future I think it is better to add options only if they # differ from the default settings as below. This will ensure the same # filenames being generated for new options at default. for key, attr in self.extra_options: value = getattr(settings, attr) if value != getattr(default_settings, attr): options.setdefault(key, value) name = self._get_thumbnail_filename(source, geometry_string, options) thumbnail = ImageFile(name, default.storage) cached = default.kvstore.get(thumbnail) if cached: return cached # We have to check exists() because the Storage backend does not # overwrite in some implementations. if settings.THUMBNAIL_FORCE_OVERWRITE or not thumbnail.exists(): try: source_image = default.engine.get_image(source) except IOError as e: logger.exception(e) if settings.THUMBNAIL_DUMMY: return DummyImageFile(geometry_string) else: # if S3Storage says file doesn't exist remotely, don't try to # create it and exit early. # Will return working empty image type; 404'd image logger.warning( 'Remote file [%s] at [%s] does not exist', file_, geometry_string, ) return thumbnail # We might as well set the size since we have the image in memory image_info = default.engine.get_image_info(source_image) options['image_info'] = image_info size = default.engine.get_image_size(source_image) source.set_size(size) try: self._create_thumbnail(source_image, geometry_string, options, thumbnail) self._create_alternative_resolutions(source_image, geometry_string, options, thumbnail.name) finally: default.engine.cleanup(source_image) # If the thumbnail exists we don't create it, the other option is # to delete and write but this could lead to race conditions so I # will just leave that out for now. default.kvstore.get_or_set(source) default.kvstore.set(thumbnail, source) return thumbnail
def get_thumbnail_custom(self, file_, geometry_string, **options): """ Returns thumbnail as an ImageFile instance for file with geometry and options given. First it will try to get it from the key value store, secondly it will create it. """ from sorl.thumbnail import delete dummy_source = settings.THUMBNAIL_DUMMY_SOURCE if 'dummy_source_size' in options: if options['dummy_source_size'] == 'small': dummy_source = settings.THUMBNAIL_DUMMY_SOURCE_SMALL if file_: source = ImageFile(file_) elif settings.THUMBNAIL_DUMMY: source = ImageFile(dummy_source) #return DummyImageFile(geometry_string) else: return None # preserve image filetype if settings.THUMBNAIL_PRESERVE_FORMAT: options.setdefault('format', self._get_format(source)) for key, value in self.default_options.items(): options.setdefault(key, value) # For the future I think it is better to add options only if they # differ from the default settings as below. This will ensure the same # filenames being generated for new options at default. for key, attr in self.extra_options: value = getattr(settings, attr) if value != getattr(default_settings, attr): options.setdefault(key, value) cache_name = self._get_thumbnail_filename(source, geometry_string, options) thumbnail = ImageFile(cache_name, default.storage) cached = default.kvstore.get(thumbnail) if cached: if not thumbnail.exists(): # CHECK IF CACHED FILE WAS DELETED MANUAL delete(cached) return self.get_thumbnail_custom(file_, geometry_string, **options) return cached media_folder = settings.MEDIA_URL # We have to check exists() because the Storage backend does not # overwrite in some implementations. if not self.check_exists(thumbnail): # TRY WITH SORL default.engine if source.name.startswith(media_folder): source.name = source.name.replace(media_folder, '') try: source_image = default.engine.get_image(source) except (IOError, UnicodeEncodeError) as error: if type(error) == UnicodeEncodeError: thumbnail = self.create_translit_cache( source, geometry_string, options, cache_name) if thumbnail: return thumbnail #print "GET_THUMBNAIL[1]: %s, URL= '%s', CREATING CACHE FILE" % (error, source.url) #return self.create_dummy_cache(source, cache_name, media_folder, dummy_source) options[ 'level'] = options['level'] if 'level' in options else 1 if options['level'] == 1: options['level'] += 1 return self.get_thumbnail_custom(dummy_source, geometry_string, **options) else: return DummyImageFile(geometry_string) # We might as well set the size since we have the image in memory image_info = default.engine.get_image_info(source_image) options['image_info'] = image_info size = default.engine.get_image_size(source_image) source.set_size(size) try: self._create_thumbnail(source_image, geometry_string, options, thumbnail) self._create_alternative_resolutions(source_image, geometry_string, options, thumbnail.name) except SystemError as error: # when image transparent, can't create thumbnail for it print(error) # return DummyImageFile(geometry_string) finally: default.engine.cleanup(source_image) # If the thumbnail exists we don't create it, the other option is # to delete and write but this could lead to race conditions so I # will just leave that out for now. try: default.kvstore.get_or_set(source) except IOError as error: #print "GET_THUMBNAIL[2]: %s, URL= '%s'. CREATING CACHE FILE" % (error, cache_name) #return self.create_dummy_cache(source, cache_name, media_folder, dummy_source) #return self.get_thumbnail_custom(dummy_source, geometry_string, **options) return DummyImageFile(geometry_string) try: default.kvstore.set(thumbnail, source) except IOError as error: #print "GET_THUMBNAIL[3]: %s, URL= '%s'. CREATING CACHE FILE" % (error, cache_name) #return self.create_dummy_cache(source, cache_name, media_folder, dummy_source) #return self.get_thumbnail_custom(dummy_source, geometry_string, **options) return DummyImageFile(geometry_string) return thumbnail