def generate_image(self, *args, **kwargs):
        transformation_list = self.get_combined_transformation_list(*args, **kwargs)

        cache_filename = '{}-{}'.format(
            self.cache_filename, BaseTransformation.combine(transformation_list)
        )

        # Check is transformed image is available
        logger.debug('transformations cache filename: %s', cache_filename)

        if not setting_disable_transformed_image_cache.value and storage_documentimagecache.exists(cache_filename):
            logger.debug(
                'transformations cache file "%s" found', cache_filename
            )
        else:
            logger.debug(
                'transformations cache file "%s" not found', cache_filename
            )
            image = self.get_image(transformations=transformation_list)
            with storage_documentimagecache.open(cache_filename, 'wb+') as file_object:
                file_object.write(image.getvalue())

            self.cached_images.create(filename=cache_filename)

        return cache_filename
    def get_api_image_url(self, *args, **kwargs):
        """
        Create an unique URL combining:
        - the page's image URL
        - the interactive argument
        - a hash from the server side and interactive transformations
        The purpose of this unique URL is to allow client side caching
        if document page images.
        """
        transformations_hash = BaseTransformation.combine(
            self.get_combined_transformation_list(*args, **kwargs)
        )

        kwargs.pop('transformations', None)

        final_url = furl()
        final_url.args = kwargs
        final_url.path = reverse(
            viewname='rest_api:documentpage-image', kwargs={
                'pk': self.document.pk, 'version_pk': self.document_version.pk,
                'page_pk': self.pk
            }
        )
        final_url.args['_hash'] = transformations_hash

        return final_url.tostr()
    def generate_image(self, user=None, **kwargs):
        transformation_list = self.get_combined_transformation_list(user=user, **kwargs)
        combined_cache_filename = BaseTransformation.combine(transformation_list)

        # Check is transformed image is available
        logger.debug('transformations cache filename: %s', combined_cache_filename)

        try:
            lock = LockingBackend.get_instance().acquire_lock(
                name='document_page_generate_image_{}_{}'.format(
                    self.pk, combined_cache_filename
                )
            )
        except Exception:
            raise
        else:
            # Second try block to release the lock even on fatal errors inside
            # the block.
            try:
                if self.cache_partition.get_file(filename=combined_cache_filename):
                    logger.debug(
                        'transformations cache file "%s" found', combined_cache_filename
                    )
                else:
                    logger.debug(
                        'transformations cache file "%s" not found', combined_cache_filename
                    )
                    image = self.get_image(transformations=transformation_list)
                    with self.cache_partition.create_file(filename=combined_cache_filename) as file_object:
                        file_object.write(image.getvalue())
                return combined_cache_filename
            finally:
                lock.release()
    def generate_image(self, user=None, **kwargs):
        transformation_list = self.get_combined_transformation_list(user=user, **kwargs)
        combined_cache_filename = BaseTransformation.combine(transformation_list)

        # Check is transformed image is available
        logger.debug('transformations cache filename: %s', combined_cache_filename)

        if not setting_disable_transformed_image_cache.value and self.cache_partition.get_file(filename=combined_cache_filename):
            logger.debug(
                'transformations cache file "%s" found', combined_cache_filename
            )
        else:
            logger.debug(
                'transformations cache file "%s" not found', combined_cache_filename
            )
            image = self.get_image(transformations=transformation_list)
            with self.cache_partition.create_file(filename=combined_cache_filename) as file_object:
                file_object.write(image.getvalue())

        return combined_cache_filename
Exemple #5
0
from django.utils.translation import ugettext_lazy as _

from mayan.apps.converter.transformations import (
    BaseTransformation, TransformationDrawRectanglePercent
)

from .layers import layer_redactions


class TransformationRedactionPercent(TransformationDrawRectanglePercent):
    arguments = ('left', 'top', 'right', 'bottom')
    label = _('Redaction')
    name = 'redaction_percent'
    template_name = 'redactions/cropper.html'


BaseTransformation.register(
    layer=layer_redactions, transformation=TransformationRedactionPercent
)