Improved image cache filename creation
This commit is contained in:
+17
-17
@@ -36,36 +36,36 @@ def execute_convert(input_filepath, arguments, output_filepath):
|
|||||||
|
|
||||||
proc = subprocess.Popen(command, stderr=subprocess.PIPE)
|
proc = subprocess.Popen(command, stderr=subprocess.PIPE)
|
||||||
return (proc.wait(), proc.stderr.read())
|
return (proc.wait(), proc.stderr.read())
|
||||||
|
|
||||||
#TODO: merge w/ convert
|
|
||||||
def in_cache(input_filepath, size, page=0, format='jpg'):
|
def create_image_cache_filename(input_filepath, size, page=0, format='jpg'):
|
||||||
temp_filename, separator = os.path.splitext(os.path.basename(input_filepath))
|
temp_filename, separator = os.path.splitext(os.path.basename(input_filepath))
|
||||||
temp_path = os.path.join(TEMPORARY_DIRECTORY, temp_filename)
|
temp_path = os.path.join(TEMPORARY_DIRECTORY, temp_filename)
|
||||||
output_arg = '%s_%s%s%s' % (temp_path, size, os.extsep, format)
|
return '%s_%s%s%s' % (temp_path, size, os.extsep, format)
|
||||||
input_arg = '%s[%s]' % (input_filepath, page)
|
|
||||||
if os.path.exists(output_arg):
|
|
||||||
return output_arg
|
def in_image_cache(input_filepath, size, page=0, format='jpg'):
|
||||||
|
output_filepath = create_image_cache_filename(input_filepath, size, page, format)
|
||||||
|
if os.path.exists(output_filepath):
|
||||||
|
return output_filepath
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def convert(input_filepath, size, cache=True, page=0, format='jpg'):
|
def convert(input_filepath, size, cache=True, page=0, format='jpg'):
|
||||||
#TODO: generate output file using lightweight hash function on
|
output_filepath = create_image_cache_filename(input_filepath, size, page, format)
|
||||||
#file name or file content
|
if os.path.exists(output_filepath):
|
||||||
temp_filename, separator = os.path.splitext(os.path.basename(input_filepath))
|
return output_filepath
|
||||||
temp_path = os.path.join(TEMPORARY_DIRECTORY, temp_filename)
|
|
||||||
output_arg = '%s_%s%s%s' % (temp_path, size, os.extsep, format)
|
|
||||||
input_arg = '%s[%s]' % (input_filepath, page)
|
|
||||||
if os.path.exists(output_arg):
|
|
||||||
return output_arg
|
|
||||||
#TODO: Check mimetype and use corresponding utility
|
#TODO: Check mimetype and use corresponding utility
|
||||||
try:
|
try:
|
||||||
status, error_string = execute_convert(input_arg, '-resize %s' % size, output_arg)
|
input_arg = '%s[%s]' % (input_filepath, page)
|
||||||
|
status, error_string = execute_convert(input_arg, '-resize %s' % size, output_filepath)
|
||||||
if status:
|
if status:
|
||||||
errors = get_errors(error_string)
|
errors = get_errors(error_string)
|
||||||
raise ConvertError(status, errors)
|
raise ConvertError(status, errors)
|
||||||
finally:
|
finally:
|
||||||
return output_arg
|
return output_filepath
|
||||||
|
|
||||||
|
|
||||||
#TODO: slugify OCR_OPTIONS and add to file name to cache
|
#TODO: slugify OCR_OPTIONS and add to file name to cache
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ from django.utils.http import urlencode
|
|||||||
|
|
||||||
|
|
||||||
from filetransfers.api import serve_file
|
from filetransfers.api import serve_file
|
||||||
from converter.api import convert, in_cache
|
from converter.api import convert, in_image_cache
|
||||||
from common.utils import pretty_size
|
from common.utils import pretty_size
|
||||||
|
|
||||||
from utils import from_descriptor_to_tempfile
|
from utils import from_descriptor_to_tempfile
|
||||||
@@ -360,12 +360,13 @@ def document_edit_metadata(request, document_id):
|
|||||||
def get_document_image(request, document_id, size=PREVIEW_SIZE):
|
def get_document_image(request, document_id, size=PREVIEW_SIZE):
|
||||||
document = get_object_or_404(Document, pk=document_id)
|
document = get_object_or_404(Document, pk=document_id)
|
||||||
|
|
||||||
filepath = in_cache(document.checksum, size)
|
filepath = in_image_cache(document.checksum, size)
|
||||||
|
|
||||||
if filepath:
|
if filepath:
|
||||||
return serve_file(request, File(file=open(filepath, 'r')))
|
return serve_file(request, File(file=open(filepath, 'r')))
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
|
#Save to a temporary location
|
||||||
document.file.open()
|
document.file.open()
|
||||||
desc = document.file.storage.open(document.file.path)
|
desc = document.file.storage.open(document.file.path)
|
||||||
filepath = from_descriptor_to_tempfile(desc, document.checksum)
|
filepath = from_descriptor_to_tempfile(desc, document.checksum)
|
||||||
|
|||||||
Reference in New Issue
Block a user