def process_zipfile(self): if os.path.isfile(self.zip_file.path): # TODO: implement try-except here zip = zipfile.ZipFile(self.zip_file.path) bad_file = zip.testzip() if bad_file: raise Exception('"%s" in the .zip archive is corrupt.' % bad_file) count = 1 album = self.album if not album: album = Album.objects.create(name=self.new_album_name) from cStringIO import StringIO for filename in sorted(zip.namelist()): if filename.startswith('__'): # do not process meta files continue data = zip.read(filename) if len(data): try: # the following is taken from django.newforms.fields.ImageField: # load() is the only method that can spot a truncated JPEG, # but it cannot be called sanely after verify() trial_image = PILImage.open(StringIO(data)) trial_image.load() # verify() is the only method that can spot a corrupt PNG, # but it must be called immediately after the constructor trial_image = PILImage.open(StringIO(data)) trial_image.verify() except Exception: # if a "bad" file is found we just skip it. continue img = Image(album=album) img.image.save(filename, ContentFile(data)) img.save() zip.close() return album
def setUp(self): self.plugin = ImagesiftPlugin() self.image_file = open( os.path.join(os.path.dirname(__file__), 'test_img.jpg')) details = ( ('i1', 'alpha rho'), ('i2', ' beta rho'), ('i3', ' gamma rho'), ) self.image_count = len(details) for title, tags in details: Image(title=title, image=File(self.image_file), tags=tags).save()
def process_zipfile(uploaded_album): if os.path.isfile(uploaded_album.zip_file.path): # TODO: implement try-except here zip = zipfile.ZipFile(uploaded_album.zip_file.path) bad_file = zip.testzip() if bad_file: raise Exception('"%s" in the .zip archive is corrupt.' % bad_file) if not uploaded_album.album: uploaded_album.album = Album.objects.create( name=uploaded_album.new_album_name) from cStringIO import StringIO for filename in sorted(zip.namelist()): if filename.startswith('__'): # do not process meta files continue print filename.encode('ascii', errors='replace') data = zip.read(filename) if len(data): try: # the following is taken from django.forms.fields.ImageField: # load() could spot a truncated JPEG, but it loads the entire # image in memory, which is a DoS vector. See #3848 and #18520. # verify() must be called immediately after the constructor. PILImage.open(StringIO(data)).verify() except Exception, ex: # if a "bad" file is found we just skip it. print('Error verify image: %s' % ex.message) continue if hasattr(data, 'seek') and callable(data.seek): print 'seeked' data.seek(0) try: img = Image(album=uploaded_album.album) img.image.save(filename, ContentFile(data)) img.save() except Exception, ex: print('error create Image: %s' % ex.message)
def process_zipfile(uploaded_album): if default_storage.exists(uploaded_album.zip_file.name): # TODO: implement try-except here zip = zipfile.ZipFile(uploaded_album.zip_file) bad_file = zip.testzip() if bad_file: raise Exception('"%s" in the .zip archive is corrupt.' % bad_file) if not uploaded_album.album: uploaded_album.album = Album.objects.create(name=uploaded_album.new_album_name) for filename in sorted(zip.namelist()): if filename.startswith('__'): # do not process meta files continue print(filename.encode('ascii', errors='replace')) data = zip.read(filename) if len(data): try: # the following is taken from django.forms.fields.ImageField: # load() could spot a truncated JPEG, but it loads the entire # image in memory, which is a DoS vector. See #3848 and #18520. # verify() must be called immediately after the constructor. PILImage.open(six.moves.cStringIO(data)).verify() except Exception as ex: # if a "bad" file is found we just skip it. print(('Error verify image: %s' % ex.message)) continue if hasattr(data, 'seek') and isinstance(data.seek, collections.Callable): print('seeked') data.seek(0) try: img = Image(album=uploaded_album.album) img.image.save(filename, ContentFile(data)) img.save() except Exception as ex: print(('error create Image: %s' % ex.message)) zip.close() uploaded_album.delete()
def setUp(self): self.image_file = open( os.path.join(os.path.dirname(__file__), 'test_img.jpg')) self.album = Album(name='test album') self.album.save() details = ( ('i1', 'alpha rho'), ('i2', ' beta rho'), ('i3', ' gamma rho'), ) self.image_count = len(details) for title, tags in details: Image(title=title, image=File(self.image_file), tags=tags).save() self.image1 = Image.objects.get(title__exact='i1')
def process_zipfile(uploaded_album): if default_storage.exists(uploaded_album.zip_file.name): # TODO: implement try-except here zip = zipfile.ZipFile(uploaded_album.zip_file) bad_file = zip.testzip() if bad_file: raise Exception('"%s" in the .zip archive is corrupt.' % bad_file) if not uploaded_album.album: try: uploaded_album.album = Album.objects.get(name=uploaded_album.new_album_name, user=uploaded_album.user) except Album.DoesNotExist: uploaded_album.album = Album.objects.create(name=uploaded_album.new_album_name, user=uploaded_album.user) from cStringIO import StringIO for filename in sorted(zip.namelist()): if filename.startswith('__'): # do not process meta files continue # print filename.encode('ascii', errors='replace') data = zip.read(filename) if len(data): try: # the following is taken from django.forms.fields.ImageField: # load() could spot a truncated JPEG, but it loads the entire # image in memory, which is a DoS vector. See #3848 and #18520. # verify() must be called immediately after the constructor. PILImage.open(StringIO(data)).verify() except Exception, ex: # if a "bad" file is found we just skip it. print('Error verify image: %s' % ex.message) continue if hasattr(data, 'seek') and callable(data.seek): print 'seeked' data.seek(0) try: img = Image() img.image.save(filename, ContentFile(data)) img.user = uploaded_album.album.user img.title = reverse_slug(filename, remove_extension=True, title=True) img.tags = uploaded_album.tags img.save() AlbumImage.objects.create(album=uploaded_album.album, image=img, order=0) except Exception, ex: print('error create Image: %s' % ex.message)