Ticket #8204: filestorage.diff

File filestorage.diff, 1.3 KB (added by flosch, 7 years ago)

maybe this will work, cannot test it yet, comments?

  • django/core/files/storage.py

     
    1111
    1212__all__ = ('Storage', 'FileSystemStorage', 'DefaultStorage', 'default_storage')
    1313
     14try:
     15    from PIL import Image
     16    has_pil = True
     17except ImportError:
     18    has_pil = False
     19
    1420class Storage(object):
    1521    """
    1622    A base storage class, providing some default behaviors that all other
     
    140146            # This file has a file path that we can move.
    141147            file_move_safe(content.temporary_file_path(), full_path)
    142148            content.close()
    143         else:
     149        elif hasattr(content, 'chunks'):
    144150            # This is a normal uploadedfile that we can stream.
    145151            fp = open(full_path, 'wb')
    146152            locks.lock(fp, locks.LOCK_EX)
     
    148154                fp.write(chunk)
    149155            locks.unlock(fp)
    150156            fp.close()
     157        elif has_pil and isinstance(content, Image.Image):
     158            content.save(full_path)
     159        else:
     160            raise SuspiciousOperation("Unknown file content type.")
    151161
     162
    152163    def delete(self, name):
    153164        name = self.path(name)
    154165        # If the file exists, delete it from the filesystem.
Back to Top