Improved image cache filename creation

This commit is contained in:
Roberto Rosario
2011-02-10 01:29:39 -04:00
parent adf4608a0c
commit a1da232ee9
2 changed files with 20 additions and 19 deletions

View File

@@ -36,36 +36,36 @@ def execute_convert(input_filepath, arguments, output_filepath):
proc = subprocess.Popen(command, stderr=subprocess.PIPE)
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_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
return '%s_%s%s%s' % (temp_path, size, os.extsep, format)
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:
return None
def convert(input_filepath, size, cache=True, page=0, format='jpg'):
#TODO: generate output file using lightweight hash function on
#file name or file content
temp_filename, separator = os.path.splitext(os.path.basename(input_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
output_filepath = create_image_cache_filename(input_filepath, size, page, format)
if os.path.exists(output_filepath):
return output_filepath
#TODO: Check mimetype and use corresponding utility
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:
errors = get_errors(error_string)
raise ConvertError(status, errors)
finally:
return output_arg
return output_filepath
#TODO: slugify OCR_OPTIONS and add to file name to cache

View File

@@ -15,7 +15,7 @@ from django.utils.http import urlencode
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 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):
document = get_object_or_404(Document, pk=document_id)
filepath = in_cache(document.checksum, size)
filepath = in_image_cache(document.checksum, size)
if filepath:
return serve_file(request, File(file=open(filepath, 'r')))
else:
try:
#Save to a temporary location
document.file.open()
desc = document.file.storage.open(document.file.path)
filepath = from_descriptor_to_tempfile(desc, document.checksum)