diff --git a/mayan/apps/sources/api_views.py b/mayan/apps/sources/api_views.py index 62fa794d17..512927e3ab 100644 --- a/mayan/apps/sources/api_views.py +++ b/mayan/apps/sources/api_views.py @@ -6,6 +6,7 @@ from converter.exceptions import UnkownConvertError, UnknownFileFormat from converter.literals import ( DEFAULT_PAGE_NUMBER, DEFAULT_ROTATION, DEFAULT_ZOOM_LEVEL ) +from converter.models import Transformation from rest_framework import generics from rest_framework.response import Response @@ -64,22 +65,10 @@ class APIStagingSourceFileImageView(generics.GenericAPIView): size = request.GET.get('size', DISPLAY_SIZE) - page = int(request.GET.get('page', DEFAULT_PAGE_NUMBER)) - - zoom = int(request.GET.get('zoom', DEFAULT_ZOOM_LEVEL)) - - if zoom < ZOOM_MIN_LEVEL: - zoom = ZOOM_MIN_LEVEL - - if zoom > ZOOM_MAX_LEVEL: - zoom = ZOOM_MAX_LEVEL - - rotation = int(request.GET.get('rotation', DEFAULT_ROTATION)) % 360 - try: return Response({ 'status': 'success', - 'data': staging_file.get_image(size=size, page=page, zoom=zoom, rotation=rotation, as_base64=True) + 'data': staging_file.get_image(as_base64=True, size=size, transformations=Transformation.objects.get_for_model(staging_folder, as_classes=True)) }) except UnknownFileFormat as exception: return Response({'status': 'error', 'detail': 'unknown_file_format', 'message': unicode(exception)}) diff --git a/mayan/apps/sources/classes.py b/mayan/apps/sources/classes.py index c3bab64fd4..6d1d41717d 100644 --- a/mayan/apps/sources/classes.py +++ b/mayan/apps/sources/classes.py @@ -11,7 +11,7 @@ except ImportError: from django.core.files import File -from converter import converter_class +from converter import TransformationResize, converter_class from mimetype.api import get_mimetype @@ -60,18 +60,23 @@ class StagingFile(object): def get_full_path(self): return os.path.join(self.staging_folder.folder_path, self.filename) - def get_image(self, size, page, zoom, rotation, as_base64=True): - # TODO: add support for transformations - converted_file_path = convert(self.get_full_path(), size=size) + 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: - mimetype = get_mimetype(open(converted_file_path, 'r'), converted_file_path, mimetype_only=True)[0] - image = open(converted_file_path, 'r') - base64_data = base64.b64encode(image.read()) - image.close() - return 'data:%s;base64,%s' % (mimetype, base64_data) + base64_data = base64.b64encode(image_data.read()) + return 'data:%s;base64,%s' % ('image/png', base64_data) else: - return converted_file_path + return image_data def delete(self): os.unlink(self.get_full_path())