Consolidate document page image generation as binary or base64 to a single place at the converter class level.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import base64
|
||||
import logging
|
||||
import os
|
||||
import tempfile
|
||||
@@ -168,7 +169,7 @@ class ConverterBase(object):
|
||||
|
||||
fs_cleanup(input_filepath)
|
||||
|
||||
def get_page(self, output_format=DEFAULT_FILE_FORMAT):
|
||||
def get_page(self, output_format=DEFAULT_FILE_FORMAT, as_base64=False):
|
||||
if not self.image:
|
||||
self.seek(0)
|
||||
|
||||
@@ -183,9 +184,13 @@ class ConverterBase(object):
|
||||
new_mode = 'RGBA'
|
||||
|
||||
self.image.convert(new_mode).save(image_buffer, format=output_format)
|
||||
image_buffer.seek(0)
|
||||
|
||||
return image_buffer
|
||||
if as_base64:
|
||||
return 'data:{};base64,{}'.format(Image.MIME[output_format], base64.b64encode(image_buffer.getvalue()))
|
||||
else:
|
||||
image_buffer.seek(0)
|
||||
|
||||
return image_buffer
|
||||
|
||||
def convert(self, page_number=DEFAULT_PAGE_NUMBER):
|
||||
self.page_number = page_number
|
||||
|
||||
@@ -3,7 +3,6 @@ from __future__ import unicode_literals
|
||||
DEFAULT_ZOOM_LEVEL = 100
|
||||
DEFAULT_ROTATION = 0
|
||||
DEFAULT_PAGE_NUMBER = 1
|
||||
DEFAULT_FILE_FORMAT = 'jpeg'
|
||||
DEFAULT_FILE_FORMAT_MIMETYPE = 'image/jpeg'
|
||||
DEFAULT_FILE_FORMAT = 'JPEG'
|
||||
|
||||
DIMENSION_SEPARATOR = 'x'
|
||||
|
||||
@@ -753,15 +753,7 @@ class DocumentPage(models.Model):
|
||||
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
|
||||
return converter.get_page(as_base64=as_base64)
|
||||
|
||||
def invalidate_cache(self):
|
||||
cache_storage_backend.delete(self.cache_filename)
|
||||
|
||||
@@ -27,8 +27,6 @@ class DocumentPageImageSerializer(serializers.Serializer):
|
||||
rotation=rotation, as_base64=True
|
||||
)
|
||||
)
|
||||
# TODO: prepend 'data:%s;base64,%s' based on format specified in
|
||||
# async call
|
||||
return task.get(timeout=DOCUMENT_IMAGE_TASK_TIMEOUT)
|
||||
|
||||
|
||||
|
||||
@@ -729,7 +729,7 @@ def get_document_image(request, document_id, size=setting_preview_size.value):
|
||||
|
||||
task = task_get_document_page_image.apply_async(kwargs=dict(document_page_id=document_page.pk, size=size, zoom=zoom, rotation=rotation, as_base64=True, version=version))
|
||||
data = task.get(timeout=DOCUMENT_IMAGE_TASK_TIMEOUT)
|
||||
return HttpResponse(base64.b64decode(data[21:]), content_type='image')
|
||||
return HttpResponse(base64.b64decode(data[data.find('base64,') + 6:]), content_type='image')
|
||||
|
||||
|
||||
def document_download(request, document_id=None, document_id_list=None, document_version_pk=None):
|
||||
|
||||
@@ -85,13 +85,7 @@ class StagingFile(object):
|
||||
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
|
||||
return converter.get_page(as_base64=as_base64)
|
||||
|
||||
def delete(self):
|
||||
os.unlink(self.get_full_path())
|
||||
|
||||
Reference in New Issue
Block a user