def test_file_save_and_load(self):
        storage = EncryptedPassthroughStorage(password='******',
                                              next_storage_backend_arguments={
                                                  'location':
                                                  self.temporary_directory,
                                              })

        test_file_name = storage.save(
            name=TEST_FILE_NAME,
            content=ContentFile(content=force_bytes(TEST_CONTENT)))

        path_file = Path(self.temporary_directory) / test_file_name

        with path_file.open(mode='rb') as file_object:
            self.assertEqual(get_mimetype(file_object=file_object),
                             ('application/octet-stream', 'binary'))

        with storage.open(name=TEST_FILE_NAME, mode='r') as file_object:
            self.assertEqual(file_object.read(), TEST_CONTENT)

        with storage.open(name=TEST_FILE_NAME, mode='r') as file_object:
            self.assertEqual(file_object.read(1), TEST_CONTENT[0:1])

        with storage.open(name=TEST_FILE_NAME, mode='r') as file_object:
            self.assertEqual(file_object.read(999), TEST_CONTENT)
示例#2
0
    def set_headers(self, filelike):
        """
        Set some common response headers (Content-Length, Content-Type, and
        Content-Disposition) based on the `filelike` response content.
        """
        encoding_map = {
            'bzip2': 'application/x-bzip',
            'gzip': 'application/gzip',
            'xz': 'application/x-xz',
        }
        filename = getattr(filelike, 'name', None)
        filename = filename if (isinstance(filename, str) and filename) else self.filename
        if os.path.isabs(filename):
            self['Content-Length'] = os.path.getsize(filelike.name)
        elif hasattr(filelike, 'getbuffer'):
            self['Content-Length'] = filelike.getbuffer().nbytes

        if self.get('Content-Type', '').startswith(settings.DEFAULT_CONTENT_TYPE):
            if self.file_to_stream:
                content_type, encoding = get_mimetype(
                    file_object=self.file_to_stream, mimetype_only=True
                )
                # Encoding isn't set to prevent browsers from automatically
                # uncompressing files.
                content_type = encoding_map.get(encoding, content_type)
                self['Content-Type'] = content_type or 'application/octet-stream'
            else:
                self['Content-Type'] = 'application/octet-stream'

        self._set_as_attachment(filename=filename)
示例#3
0
    def test_file_save_and_load(self):
        storage = ZipCompressedPassthroughStorage(
            next_storage_backend_arguments={
                'location': self.temporary_directory
            }
        )

        test_file_name = storage.save(
            name=TEST_FILE_NAME, content=ContentFile(content=TEST_CONTENT)
        )

        path_file = Path(self.temporary_directory) / test_file_name

        with path_file.open(mode='rb') as file_object:
            self.assertEqual(
                get_mimetype(file_object=file_object),
                ('application/zip', 'binary')
            )

        with path_file.open(mode='rb') as file_object:
            self.assertNotEqual(
                file_object.read(), force_bytes(TEST_CONTENT)
            )

        with storage.open(name=TEST_FILE_NAME, mode='r') as file_object:
            self.assertEqual(file_object.read(), TEST_CONTENT)
    def test_storage_processor_command_forwards(self):
        self._upload_and_call()

        with open(file=self.test_document.latest_version.file.path,
                  mode='rb') as file_object:
            self.assertEqual(get_mimetype(file_object=file_object),
                             ('application/zip', 'binary'))

        self.assertEqual(
            self.test_document.latest_version.checksum,
            self.test_document.latest_version.update_checksum(save=False))
示例#5
0
    def open(cls, file_object):
        mime_type = get_mimetype(file_object=file_object,
                                 mimetype_only=True)[0]

        try:
            for archive_class in cls._registry[mime_type]:
                instance = archive_class()
                instance._open(file_object=file_object)
                return instance
        except KeyError:
            raise NoMIMETypeMatch
示例#6
0
 def __init__(self, file_object, mime_type=None):
     self.file_object = file_object
     self.image = None
     self.mime_type = mime_type or get_mimetype(file_object=file_object,
                                                mimetype_only=False)[0]
     self.soffice_file = None
     Image.init()
     try:
         self.command_libreoffice = sh.Command(libreoffice_path).bake(
             '--headless', '--convert-to', 'pdf:writer_pdf_Export')
     except sh.CommandNotFound:
         self.command_libreoffice = None
示例#7
0
 def update_mimetype(self, save=True):
     """
     Read a document verions's file and determine the mimetype by calling
     the get_mimetype wrapper
     """
     if self.exists():
         try:
             with self.open() as file_object:
                 self.mimetype, self.encoding = get_mimetype(
                     file_object=file_object)
         except Exception:
             self.mimetype = ''
             self.encoding = ''
         finally:
             if save:
                 self.save()
    def test_processor_forwards_and_reverse(self):
        self._upload_and_call()

        self._call_command(reverse=True)

        self.defined_storage.dotted_path = 'django.core.files.storage.FileSystemStorage'
        self.defined_storage.kwargs = {
            'location': self.document_storage_kwargs['location']
        }

        with open(file=self.test_document.latest_version.file.path,
                  mode='rb') as file_object:
            self.assertNotEqual(get_mimetype(file_object=file_object),
                                ('application/zip', 'binary'))

        self.assertEqual(
            self.test_document.latest_version.checksum,
            self.test_document.latest_version.update_checksum(save=False))
示例#9
0
    def to_pdf(self):
        # Handle .msg files
        if self.mime_type in MSG_MIME_TYPES:
            archive = MsgArchive.open(file_object=self.file_object)
            members = archive.members()
            if len(members):
                if 'message.txt' in members:
                    self.file_object = archive.open_member(
                        filename='message.txt')
                else:
                    self.file_object = archive.open_member(filename=members[0])

                self.mime_type = get_mimetype(file_object=self.file_object,
                                              mimetype_only=True)[0]

        if self.mime_type in CONVERTER_OFFICE_FILE_MIMETYPES:
            return self.soffice()
        else:
            raise InvalidOfficeFormat(_('Not an office file format.'))