def process_file(path): """Process single file""" # failsafe copy of file copy = default_storage.open(path, 'rb') default_storage.save(path + timestamp, copy) copy.close() try: path = default_storage.path(path) squeeze(path, backup=False, flag_processed_file=False, new_only=False) except NotImplementedError: if path[-1:] != os.sep: pf = default_storage.open(path, 'rwb') image = pf.read() tmpfilehandle, tmpfilepath = tempfile.mkstemp() tmpfilehandle = os.fdopen(tmpfilehandle, 'wb') tmpfilehandle.write(image) tmpfilehandle.close() squeeze(tmpfilepath) tmpfilehandle = open(tmpfilepath) pf.close() default_storage.save(path, tmpfilehandle) os.remove(tmpfilepath)
def test_squeeze(self): self.assertEqual(diet.squeeze('/tmp/bla'), None) old_size = os.stat(self.test_image_path).st_size new_path = diet.squeeze(self.test_image_path) new_size = os.stat(new_path).st_size if self.have_jpeg_tools(): self.assertEqual(new_path, self.test_image_path) self.assertTrue(new_size < old_size) else: print "Install jpegtran or jpegoptim to test shrinking"
def post_process(self, files, *args, **kwargs): results = [] if settings.DIET_COMPRESS_STATIC_IMAGES: if 'image_diet' not in main_settings.INSTALLED_APPS: raise NotImplementedError( "You need to install django_image_diet to use DIET_COMPRESS_STATIC_IMAGES" ) from image_diet.diet import squeeze for f in files: processed_file = squeeze( os.path.join(main_settings.STATIC_ROOT, f)) results.append([ f, processed_file, True if processed_file is not None else False ]) return results
def optimize_thumbnail(sender, **kwargs): squeeze(sender.path)
def optimize_file(sender, fieldfile, **kwargs): squeeze(fieldfile.path)
def handle(self, *args, **options): for dirname in args: for (root, dirs, files) in os.walk(dirname): for filename in files: filepath = join(root, filename) squeeze(filepath)
def squeeze_task(path): squeeze(path)