Ejemplo n.º 1
0
    def get_image(self, transformations=None):
        cache_filename = self.cache_filename
        logger.debug('Page cache filename: %s', cache_filename)

        if not setting_disable_base_image_cache.value and cache_storage_backend.exists(
                cache_filename):
            logger.debug('Page cache file "%s" found', cache_filename)
            converter = converter_class(
                file_object=cache_storage_backend.open(cache_filename))

            converter.seek(0)
        else:
            logger.debug('Page cache file "%s" not found', cache_filename)

            try:
                converter = converter_class(
                    file_object=self.document_version.get_intermidiate_file())
                converter.seek(page_number=self.page_number - 1)

                page_image = converter.get_page()

                with cache_storage_backend.open(cache_filename,
                                                'wb+') as file_object:
                    file_object.write(page_image.getvalue())
            except Exception as exception:
                # Cleanup in case of error
                logger.error('Error creating page cache file "%s"; %s',
                             cache_filename, exception)
                cache_storage_backend.delete(cache_filename)
                raise

        for transformation in transformations:
            converter.transform(transformation=transformation)

        return converter.get_page()
Ejemplo n.º 2
0
    def get_intermidiate_file(self):
        cache_filename = self.cache_filename
        logger.debug('Intermidiate filename: %s', cache_filename)

        if storage_documentimagecache.exists(cache_filename):
            logger.debug('Intermidiate file "%s" found.', cache_filename)

            return storage_documentimagecache.open(cache_filename)
        else:
            logger.debug('Intermidiate file "%s" not found.', cache_filename)

            try:
                converter = converter_class(file_object=self.open())
                pdf_file_object = converter.to_pdf()

                with storage_documentimagecache.open(cache_filename, mode='wb+') as file_object:
                    for chunk in pdf_file_object:
                        file_object.write(chunk)

                return storage_documentimagecache.open(cache_filename)
            except InvalidOfficeFormat:
                return self.open()
            except Exception as exception:
                # Cleanup in case of error
                logger.error(
                    'Error creating intermediate file "%s"; %s.',
                    cache_filename, exception
                )
                storage_documentimagecache.delete(cache_filename)
                raise
Ejemplo n.º 3
0
    def get_image(self, transformations=None):
        cache_filename = self.cache_filename
        file_object = None

        try:
            file_object = open(self.get_full_path())
            converter = converter_class(file_object=file_object)

            page_image = converter.get_page()

            # Since open "wb+" doesn't create files, check if the file
            # exists, if not then create it
            if not storage_staging_file_image_cache.exists(cache_filename):
                storage_staging_file_image_cache.save(name=cache_filename, content=ContentFile(content=''))

            with storage_staging_file_image_cache.open(cache_filename, 'wb+') as file_object:
                file_object.write(page_image.getvalue())
        except Exception as exception:
            # Cleanup in case of error
            logger.error(
                'Error creating staging file cache "%s"; %s',
                cache_filename, exception
            )
            storage_staging_file_image_cache.delete(cache_filename)
            if file_object:
                file_object.close()
            raise

        for transformation in transformations:
            converter.transform(transformation=transformation)

        result = converter.get_page()
        file_object.close()
        return result
Ejemplo n.º 4
0
    def update_page_count(self, save=True):
        try:
            with self.open() as file_object:
                converter = converter_class(
                    file_object=file_object, mime_type=self.mimetype
                )
                detected_pages = converter.get_page_count()
        except PageCountError:
            # If converter backend doesn't understand the format,
            # use 1 as the total page count
            pass
        else:
            with transaction.atomic():
                self.pages.all().delete()

                for page_number in range(detected_pages):
                    DocumentPage.objects.create(
                        document_version=self, page_number=page_number + 1
                    )

            # TODO: is this needed anymore
            if save:
                self.save()

            return detected_pages
    def update_page_count(self, save=True):
        try:
            with self.open() as file_object:
                converter = converter_class(
                    file_object=file_object, mime_type=self.mimetype
                )
                detected_pages = converter.get_page_count()
        except PageCountError:
            # If converter backend doesn't understand the format,
            # use 1 as the total page count
            pass
        else:
            with transaction.atomic():
                self.pages.all().delete()

                for page_number in range(detected_pages):
                    DocumentPage.objects.create(
                        document_version=self, page_number=page_number + 1
                    )

            # TODO: is this needed anymore
            if save:
                self.save()

            return detected_pages
    def get_intermidiate_file(self):
        cache_filename = self.cache_filename
        logger.debug('Intermidiate filename: %s', cache_filename)

        if cache_storage_backend.exists(cache_filename):
            logger.debug('Intermidiate file "%s" found.', cache_filename)

            return cache_storage_backend.open(cache_filename)
        else:
            logger.debug('Intermidiate file "%s" not found.', cache_filename)

            try:
                converter = converter_class(file_object=self.open())
                pdf_file_object = converter.to_pdf()

                with cache_storage_backend.open(cache_filename, 'wb+') as file_object:
                    for chunk in pdf_file_object:
                        file_object.write(chunk)

                return cache_storage_backend.open(cache_filename)
            except InvalidOfficeFormat:
                return self.open()
            except Exception as exception:
                # Cleanup in case of error
                logger.error(
                    'Error creating intermediate file "%s"; %s.',
                    cache_filename, exception
                )
                cache_storage_backend.delete(cache_filename)
                raise
Ejemplo n.º 7
0
    def get_image(self, transformations=None):
        cache_filename = self.cache_filename
        file_object = None

        try:
            file_object = open(self.get_full_path(), mode='rb')
            converter = converter_class(file_object=file_object)

            page_image = converter.get_page()

            # Since open "wb+" doesn't create files, check if the file
            # exists, if not then create it
            if not storage_staging_file_image_cache.exists(cache_filename):
                storage_staging_file_image_cache.save(name=cache_filename, content=ContentFile(content=''))

            with storage_staging_file_image_cache.open(cache_filename, 'wb+') as file_object:
                file_object.write(page_image.getvalue())
        except Exception as exception:
            # Cleanup in case of error
            logger.error(
                'Error creating staging file cache "%s"; %s',
                cache_filename, exception
            )
            storage_staging_file_image_cache.delete(cache_filename)
            if file_object:
                file_object.close()
            raise

        for transformation in transformations:
            converter.transform(transformation=transformation)

        result = converter.get_page()
        file_object.close()
        return result
Ejemplo n.º 8
0
 def detect_orientation(self):
     with self.document_version.open() as file_object:
         converter = converter_class(
             file_object=file_object,
             mime_type=self.document_version.mimetype
         )
         return converter.detect_orientation(
             page_number=self.page_number
         )
 def detect_orientation(self):
     with self.document_version.open() as file_object:
         converter = converter_class(
             file_object=file_object,
             mime_type=self.document_version.mimetype
         )
         return converter.detect_orientation(
             page_number=self.page_number
         )
Ejemplo n.º 10
0
    def execute(self, file_object, language=None, transformations=None):
        self.language = language

        if not transformations:
            transformations = []

        self.converter = converter_class(file_object=file_object)

        for transformation in transformations:
            self.converter.transform(transformation=transformation)
Ejemplo n.º 11
0
    def execute(self, file_object, language=None, transformations=None):
        self.language = language

        if not transformations:
            transformations = []

        self.converter = converter_class(file_object=file_object)

        for transformation in transformations:
            self.converter.transform(transformation=transformation)
Ejemplo n.º 12
0
    def get_image(self, transformations=None):
        cache_filename = self.cache_filename
        logger.debug('Page cache filename: %s', cache_filename)

        if not setting_disable_base_image_cache.value and storage_documentimagecache.exists(cache_filename):
            logger.debug('Page cache file "%s" found', cache_filename)
            converter = converter_class(
                file_object=storage_documentimagecache.open(cache_filename)
            )

            converter.seek(0)
        else:
            logger.debug('Page cache file "%s" not found', cache_filename)

            try:
                converter = converter_class(
                    file_object=self.document_version.get_intermidiate_file()
                )
                converter.seek(page_number=self.page_number - 1)

                page_image = converter.get_page()

                # Since open "wb+" doesn't create files, check if the file
                # exists, if not then create it
                if not storage_documentimagecache.exists(cache_filename):
                    storage_documentimagecache.save(name=cache_filename, content=ContentFile(content=''))

                with storage_documentimagecache.open(cache_filename, 'wb+') as file_object:
                    file_object.write(page_image.getvalue())
            except Exception as exception:
                # Cleanup in case of error
                logger.error(
                    'Error creating page cache file "%s"; %s',
                    cache_filename, exception
                )
                storage_documentimagecache.delete(cache_filename)
                raise

        for transformation in transformations:
            converter.transform(transformation=transformation)

        return converter.get_page()
Ejemplo n.º 13
0
    def get_image(self, transformations=None):
        cache_filename = self.cache_filename
        logger.debug('Page cache filename: %s', cache_filename)

        if not setting_disable_base_image_cache.value and storage_documentimagecache.exists(cache_filename):
            logger.debug('Page cache file "%s" found', cache_filename)
            converter = converter_class(
                file_object=storage_documentimagecache.open(cache_filename)
            )

            converter.seek(0)
        else:
            logger.debug('Page cache file "%s" not found', cache_filename)

            try:
                converter = converter_class(
                    file_object=self.document_version.get_intermidiate_file()
                )
                converter.seek(page_number=self.page_number - 1)

                page_image = converter.get_page()

                # Since open "wb+" doesn't create files, check if the file
                # exists, if not then create it
                if not storage_documentimagecache.exists(cache_filename):
                    storage_documentimagecache.save(name=cache_filename, content=ContentFile(content=''))

                with storage_documentimagecache.open(cache_filename, 'wb+') as file_object:
                    file_object.write(page_image.getvalue())
            except Exception as exception:
                # Cleanup in case of error
                logger.error(
                    'Error creating page cache file "%s"; %s',
                    cache_filename, exception
                )
                storage_documentimagecache.delete(cache_filename)
                raise

        for transformation in transformations:
            converter.transform(transformation=transformation)

        return converter.get_page()
Ejemplo n.º 14
0
    def get_image(self, size=None, as_base64=False, transformations=None):
        converter = converter_class(file_object=open(self.get_full_path()))

        if size:
            converter.transform(transformation=TransformationResize(
                **dict(zip(('width', 'height'), (size.split('x'))))))

        # Interactive transformations
        for transformation in transformations:
            converter.transform(transformation=transformation)

        return converter.get_page(as_base64=as_base64)
Ejemplo n.º 15
0
    def get_image(self, transformations=None):
        cache_filename = self.cache_filename
        logger.debug('Page cache filename: %s', cache_filename)

        if not setting_disable_base_image_cache.value and cache_storage_backend.exists(cache_filename):
            logger.debug('Page cache file "%s" found', cache_filename)
            converter = converter_class(
                file_object=cache_storage_backend.open(cache_filename)
            )

            converter.seek(0)
        else:
            logger.debug('Page cache file "%s" not found', cache_filename)

            try:
                converter = converter_class(
                    file_object=self.document_version.get_intermidiate_file()
                )
                converter.seek(page_number=self.page_number - 1)

                page_image = converter.get_page()

                with cache_storage_backend.open(cache_filename, 'wb+') as file_object:
                    file_object.write(page_image.getvalue())
            except Exception as exception:
                # Cleanup in case of error
                logger.error(
                    'Error creating page cache file "%s"; %s',
                    cache_filename, exception
                )
                cache_storage_backend.delete(cache_filename)
                raise

        for transformation in transformations:
            converter.transform(transformation=transformation)

        return converter.get_page()
Ejemplo n.º 16
0
    def get_image(self, size=None, as_base64=True, transformations=None):
        converter = converter_class(file_object=open(self.get_full_path()))

        if size:
            converter.transform(transformation=TransformationResize(
                **dict(zip(('width', 'height'), (size.split('x'))))))

        # Interactive transformations
        for transformation in transformations:
            converter.transform(transformation=transformation)

        image_data = converter.get_page()

        if as_base64:
            base64_data = base64.b64encode(image_data.read())
            return 'data:%s;base64,%s' % ('image/png', base64_data)
        else:
            return image_data
Ejemplo n.º 17
0
    def get_image(self, size=None, as_base64=True, transformations=None):
        converter = converter_class(file_object=open(self.get_full_path()))

        if size:
            converter.transform(
                transformation=TransformationResize(
                    **dict(zip(('width', 'height'), (size.split('x'))))
                )
            )

        # Interactive transformations
        for transformation in transformations:
            converter.transform(transformation=transformation)

        image_data = converter.get_page()

        if as_base64:
            base64_data = base64.b64encode(image_data.read())
            return 'data:%s;base64,%s' % ('image/png', base64_data)
        else:
            return image_data
Ejemplo n.º 18
0
    def get_image(self, *args, **kwargs):
        as_base64 = kwargs.pop('as_base64', False)
        transformations = kwargs.pop('transformations', [])
        size = kwargs.pop('size', setting_display_size.value)
        rotation = int(
            kwargs.pop('rotation', DEFAULT_ROTATION) or DEFAULT_ROTATION
        )
        zoom_level = int(
            kwargs.pop('zoom', DEFAULT_ZOOM_LEVEL) or DEFAULT_ZOOM_LEVEL
        )

        if zoom_level < setting_zoom_min_level.value:
            zoom_level = setting_zoom_min_level.value

        if zoom_level > setting_zoom_max_level.value:
            zoom_level = setting_zoom_max_level.value

        rotation = rotation % 360

        cache_filename = self.cache_filename
        logger.debug('Page cache filename: %s', cache_filename)

        if cache_storage_backend.exists(cache_filename):
            logger.debug('Page cache file "%s" found', cache_filename)
            converter = converter_class(
                file_object=cache_storage_backend.open(cache_filename)
            )

            converter.seek(0)
        else:
            logger.debug('Page cache file "%s" not found', cache_filename)

            try:
                converter = converter_class(
                    file_object=self.document_version.get_intermidiate_file()
                )
                converter.seek(page_number=self.page_number - 1)

                page_image = converter.get_page()

                with cache_storage_backend.open(cache_filename, 'wb+') as file_object:
                    file_object.write(page_image.getvalue())
            except Exception as exception:
                # Cleanup in case of error
                logger.error(
                    'Error creating page cache file "%s"; %s',
                    cache_filename, exception
                )
                cache_storage_backend.delete(cache_filename)
                raise

        # Stored transformations
        for stored_transformation in Transformation.objects.get_for_model(self, as_classes=True):
            converter.transform(transformation=stored_transformation)

        # Interactive transformations
        for transformation in transformations:
            converter.transform(transformation=transformation)

        if rotation:
            converter.transform(transformation=TransformationRotate(
                degrees=rotation)
            )

        if size:
            converter.transform(transformation=TransformationResize(
                **dict(zip(('width', 'height'), (size.split('x')))))
            )

        if zoom_level:
            converter.transform(
                transformation=TransformationZoom(percent=zoom_level)
            )

        page_image = converter.get_page()

        if as_base64:
            # TODO: don't prepend 'data:%s;base64,%s' part
            return 'data:%s;base64,%s' % (
                'image/png', base64.b64encode(page_image.getvalue())
            )
        else:
            return page_image
Ejemplo n.º 19
0
    def get_image(self, *args, **kwargs):
        as_base64 = kwargs.pop('as_base64', False)
        transformations = kwargs.pop('transformations', [])
        size = kwargs.pop('size', setting_display_size.value)
        rotation = int(
            kwargs.pop('rotation', DEFAULT_ROTATION) or DEFAULT_ROTATION)
        zoom_level = int(
            kwargs.pop('zoom', DEFAULT_ZOOM_LEVEL) or DEFAULT_ZOOM_LEVEL)

        if zoom_level < setting_zoom_min_level.value:
            zoom_level = setting_zoom_min_level.value

        if zoom_level > setting_zoom_max_level.value:
            zoom_level = setting_zoom_max_level.value

        rotation = rotation % 360

        cache_filename = self.cache_filename
        logger.debug('Page cache filename: %s', cache_filename)

        if cache_storage_backend.exists(cache_filename):
            logger.debug('Page cache file "%s" found', cache_filename)
            converter = converter_class(
                file_object=cache_storage_backend.open(cache_filename))

            converter.seek(0)
        else:
            logger.debug('Page cache file "%s" not found', cache_filename)

            try:
                converter = converter_class(
                    file_object=self.document_version.get_intermidiate_file())
                converter.seek(page_number=self.page_number - 1)

                page_image = converter.get_page()

                with cache_storage_backend.open(cache_filename,
                                                'wb+') as file_object:
                    file_object.write(page_image.getvalue())
            except Exception as exception:
                # Cleanup in case of error
                logger.error('Error creating page cache file "%s"; %s',
                             cache_filename, exception)
                cache_storage_backend.delete(cache_filename)
                raise

        # Stored transformations
        for stored_transformation in Transformation.objects.get_for_model(
                self, as_classes=True):
            converter.transform(transformation=stored_transformation)

        # Interactive transformations
        for transformation in transformations:
            converter.transform(transformation=transformation)

        if rotation:
            converter.transform(transformation=TransformationRotate(
                degrees=rotation))

        if size:
            converter.transform(transformation=TransformationResize(
                **dict(zip(('width', 'height'), (size.split('x'))))))

        if zoom_level:
            converter.transform(transformation=TransformationZoom(
                percent=zoom_level))

        page_image = converter.get_page()

        if as_base64:
            # TODO: don't prepend 'data:%s;base64,%s' part
            return 'data:%s;base64,%s' % (
                'image/png', base64.b64encode(page_image.getvalue()))
        else:
            return page_image