def test_unicode(self): """ Make sure that sha1 isn't choking on unicode characters. """ hash_arg = u'banni\xe8re' make_hash(hash_arg)
def download_thumbnail(url, instance, field_name): """ Downloads a thumbnail and stores it in the given instance field. Returns final storage path. """ url = iri_to_uri(url) response = requests.get(url, stream=True) if response.status_code != 200: raise Exception temp = NamedTemporaryFile() # May raise IOError. temp.write(response.raw.read()) temp.seek(0) # May raise various Exceptions. im = Image.open(temp) im.verify() ext = os.path.splitext(urlparse.urlsplit(url).path)[1] args = (url, now().isoformat()) filename = ''.join((make_hash(*args, step=2), ext)) f = instance._meta.get_field(field_name) storage_path = f.generate_filename(instance, filename) return default_storage.save(storage_path, File(temp))
def _generate(self, storage_path): # May raise IOError if the file doesn't exist or isn't a valid image. # If we're here, we can assume that the adjustment doesn't already # exist. Try to create one from the storage path. Raises IOError if # something goes wrong. kwargs = { 'requested': self.requested, 'storage_path': storage_path } with default_storage.open(storage_path, 'rb') as im_file: im = Image.open(im_file) try: im.verify() except Exception: # Raise an IOError if the image isn't valid. raise IOError im_file.seek(0) im = Image.open(im_file) im.load() format = im.format if im.format in KEEP_FORMATS else DEFAULT_FORMAT if self.adjust_uses_areas: areas = self.get_areas(storage_path) else: areas = None for adjustment in self.adjustments: im = adjustment.adjust(im, areas=areas) adjusted = AdjustedImage(**kwargs) f = adjusted._meta.get_field('adjusted') args = (six.text_type(kwargs), datetime.datetime.now().isoformat()) filename = '.'.join((make_hash(*args, step=2), format.lower())) storage_path = f.generate_filename(adjusted, filename) final_path = save_image(im, storage_path, format=format, storage=default_storage) # Try to handle race conditions gracefully. try: adjusted = AdjustedImage.objects.filter(**kwargs ).only('adjusted')[:1][0] except IndexError: adjusted.adjusted = final_path adjusted.save() else: default_storage.delete(final_path) return adjusted
def _generate(self, storage_path): # May raise IOError if the file doesn't exist or isn't a valid image. # If we're here, we can assume that the adjustment doesn't already # exist. Try to create one from the storage path. Raises IOError if # something goes wrong. kwargs = { 'requested': self.requested, 'storage_path': storage_path } with default_storage.open(storage_path, 'rb') as im_file: im = Image.open(im_file) try: im.verify() except (IndexError, struct.error, SyntaxError): # Raise an IOError if the image isn't valid. raise IOError im_file.seek(0) im = Image.open(im_file) im.load() format = im.format if im.format in KEEP_FORMATS else DEFAULT_FORMAT if self.adjust_uses_areas: areas = self.get_areas(storage_path) else: areas = None for adjustment in self.adjustments: im = adjustment.adjust(im, areas=areas) adjusted = AdjustedImage(**kwargs) f = adjusted._meta.get_field('adjusted') args = (six.text_type(kwargs), datetime.datetime.now().isoformat()) filename = '.'.join((make_hash(*args, step=2), format.lower())) storage_path = f.generate_filename(adjusted, filename) final_path = save_image(im, storage_path, format=format, storage=default_storage) # Try to handle race conditions gracefully. try: adjusted = AdjustedImage.objects.filter(**kwargs ).only('adjusted')[:1][0] except IndexError: adjusted.adjusted = final_path adjusted.save() else: default_storage.delete(final_path) return adjusted
def adjust(self): # May raise IOError. # First try to fetch a version that already exists. kwargs = self.get_query_kwargs() try: return AdjustedImage.objects.filter(**kwargs)[:1][0] except IndexError: pass # If that fails, try to create one from the storage path. # Raises IOError if something goes wrong. adjustment = self.adjustment_for_path(self.storage_path) im = adjustment.adjust() creation_kwargs = {} for k, v in kwargs.iteritems(): if k.endswith('__isnull'): creation_kwargs[k[:-len('__isnull')]] = None else: creation_kwargs[k] = v adjusted = AdjustedImage(**creation_kwargs) f = adjusted._meta.get_field('adjusted') format = (adjustment.format if adjustment.format in KEEP_FORMATS else DEFAULT_FORMAT) args = (unicode(creation_kwargs), datetime.datetime.now().isoformat()) filename = '.'.join((make_hash(*args, step=2), format.lower())) storage_path = f.generate_filename(adjusted, filename) final_path = save_image(im, storage_path, format=format, storage=default_storage) # Try to handle race conditions gracefully. try: adjusted = AdjustedImage.objects.filter(**kwargs)[:1][0] except IndexError: adjusted.adjusted = final_path adjusted.save() else: default_storage.delete(final_path) return adjusted
def make_security_hash(cls, kwargs): kwargs = OrderedDict(sorted(kwargs.items(), key=lambda item: item[0], reverse=False)) args = list(itertools.chain(kwargs.keys(), kwargs.values())) return make_hash(settings.SECRET_KEY, step=2, *args)
def make_security_hash(cls, kwargs): kwargs = SortedDict(kwargs) kwargs.keyOrder.sort() args = kwargs.keys() + kwargs.values() return make_hash(settings.SECRET_KEY, step=2, *args)
def make_security_hash(cls, kwargs): keys_sorted = sorted(kwargs.keys()) values = [kwargs[key] for key in keys_sorted] args = list(itertools.chain(keys_sorted, values)) return make_hash(settings.SECRET_KEY, step=2, *args)
def make_security_hash(cls, kwargs): kwargs = SortedDict(kwargs) kwargs.keyOrder.sort() args = list(itertools.chain(kwargs.keys(), kwargs.values())) return make_hash(settings.SECRET_KEY, step=2, *args)
def video_save_thumbnail(video_pk): try: video = Video.objects.get(pk=video_pk) except Video.DoesNotExist: logging.warn( 'video_save_thumbnail(%s) could not find video', video_pk) return if not video.thumbnail_url: return thumbnail_url = quote_unicode_url(video.thumbnail_url) try: remote_file = urllib.urlopen(thumbnail_url) except httplib.InvalidURL: # If the URL isn't valid, erase it. Video.objects.filter(pk=video.pk ).update(thumbnail_url='') return if remote_file.getcode() != 200: logging.info("Code %i when getting %r, retrying", remote_file.getcode(), video.thumbnail_url) video_save_thumbnail.retry() temp = NamedTemporaryFile() try: temp.write(remote_file.read()) except IOError: # Could be a temporary disruption - try again later if this was # a task. Otherwise reraise. if video_save_thumbnail.request.called_directly: raise video_save_thumbnail.retry() temp.seek(0) try: im = Image.open(temp) im.verify() except Exception: # If the file isn't valid, erase the url. Video.objects.filter(pk=video.pk ).update(thumbnail_url='') return f = video._meta.get_field('thumbnail') format = im.format if im.format in KEEP_FORMATS else DEFAULT_FORMAT args = (video.thumbnail_url, video.pk, datetime.datetime.now().isoformat()) filename = '.'.join((make_hash(*args, step=2), format.lower())) storage_path = f.generate_filename(video, filename) # We save the thumbnail file and then update the path on the instance # to avoid overwriting other changes that might have happened # simultaneously. final_path = default_storage.save(storage_path, File(temp)) Video.objects.filter(pk=video.pk ).update(thumbnail=final_path) remote_file.close() temp.close()