When deleting staging file, it's cached preview is also deleted
This commit is contained in:
@@ -74,14 +74,15 @@ def execute_unoconv(input_filepath, output_filepath, arguments=''):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def cache_cleanup(input_filepath, size, page=0, format='jpg'):
|
def cache_cleanup(input_filepath, size, quality=QUALITY_DEFAULT, page=0, format='jpg', extra_options=''):
|
||||||
filepath = create_image_cache_filename(input_filepath, size, page, format)
|
filepath = create_image_cache_filename(input_filepath, size=size, page=page, format=format, quality=quality, extra_options=extra_options)
|
||||||
try:
|
try:
|
||||||
os.remove(filepath)
|
os.remove(filepath)
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def create_image_cache_filename(input_filepath, quality=QUALITY_DEFAULT, extra_options='', *args, **kwargs):
|
|
||||||
|
def create_image_cache_filename(input_filepath, *args, **kwargs):
|
||||||
if input_filepath:
|
if input_filepath:
|
||||||
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)
|
||||||
@@ -89,8 +90,6 @@ def create_image_cache_filename(input_filepath, quality=QUALITY_DEFAULT, extra_o
|
|||||||
final_filepath = []
|
final_filepath = []
|
||||||
[final_filepath.append(str(arg)) for arg in args]
|
[final_filepath.append(str(arg)) for arg in args]
|
||||||
final_filepath.extend(['%s_%s' % (key, value) for key, value in kwargs.items()])
|
final_filepath.extend(['%s_%s' % (key, value) for key, value in kwargs.items()])
|
||||||
final_filepath.append(QUALITY_SETTINGS[quality])
|
|
||||||
final_filepath.append(extra_options)
|
|
||||||
|
|
||||||
temp_path += slugify('_'.join(final_filepath))
|
temp_path += slugify('_'.join(final_filepath))
|
||||||
|
|
||||||
|
|||||||
+19
-13
@@ -10,7 +10,7 @@ from documents.conf.settings import STAGING_DIRECTORY
|
|||||||
from documents.conf.settings import DEFAULT_TRANSFORMATIONS
|
from documents.conf.settings import DEFAULT_TRANSFORMATIONS
|
||||||
from documents.conf.settings import STAGING_FILES_PREVIEW_SIZE
|
from documents.conf.settings import STAGING_FILES_PREVIEW_SIZE
|
||||||
from converter import TRANFORMATION_CHOICES
|
from converter import TRANFORMATION_CHOICES
|
||||||
from converter.api import convert#, in_image_cache, QUALITY_DEFAULT
|
from converter.api import convert, cache_cleanup
|
||||||
|
|
||||||
HASH_FUNCTION = lambda x: hashlib.sha256(x).hexdigest()
|
HASH_FUNCTION = lambda x: hashlib.sha256(x).hexdigest()
|
||||||
#TODO: Do benchmarks
|
#TODO: Do benchmarks
|
||||||
@@ -71,6 +71,8 @@ class StagingFile(object):
|
|||||||
raise Exception(ugettext(u'Unable to upload staging file: %s') % exc)
|
raise Exception(ugettext(u'Unable to upload staging file: %s') % exc)
|
||||||
|
|
||||||
def delete(self):
|
def delete(self):
|
||||||
|
tranformation_string, errors = get_transformation_string(DEFAULT_TRANSFORMATIONS)
|
||||||
|
cache_cleanup(self.filepath, size=STAGING_FILES_PREVIEW_SIZE, extra_options=tranformation_string)
|
||||||
try:
|
try:
|
||||||
os.unlink(self.filepath)
|
os.unlink(self.filepath)
|
||||||
except OSError, exc:
|
except OSError, exc:
|
||||||
@@ -80,17 +82,21 @@ class StagingFile(object):
|
|||||||
raise OSError(ugettext(u'Unable to delete staging file: %s') % exc)
|
raise OSError(ugettext(u'Unable to delete staging file: %s') % exc)
|
||||||
|
|
||||||
def preview(self):
|
def preview(self):
|
||||||
transformation_list = []
|
tranformation_string, errors = get_transformation_string(DEFAULT_TRANSFORMATIONS)
|
||||||
errors = []
|
|
||||||
for transformation in DEFAULT_TRANSFORMATIONS:
|
|
||||||
try:
|
|
||||||
if transformation['name'] in TRANFORMATION_CHOICES:
|
|
||||||
output = TRANFORMATION_CHOICES[transformation['name']] % eval(transformation['arguments'])
|
|
||||||
transformation_list.append(output)
|
|
||||||
except Exception, e:
|
|
||||||
errors.append(e)
|
|
||||||
|
|
||||||
tranformation_string = ' '.join(transformation_list)
|
|
||||||
|
|
||||||
output_file = convert(self.filepath, size=STAGING_FILES_PREVIEW_SIZE, extra_options=tranformation_string, cleanup_files=False)
|
output_file = convert(self.filepath, size=STAGING_FILES_PREVIEW_SIZE, extra_options=tranformation_string, cleanup_files=False)
|
||||||
return output_file, errors
|
return output_file, errors
|
||||||
|
|
||||||
|
|
||||||
|
def get_transformation_string(transformations):
|
||||||
|
transformation_list = []
|
||||||
|
errors = []
|
||||||
|
for transformation in transformations:
|
||||||
|
try:
|
||||||
|
if transformation['name'] in TRANFORMATION_CHOICES:
|
||||||
|
output = TRANFORMATION_CHOICES[transformation['name']] % eval(transformation['arguments'])
|
||||||
|
transformation_list.append(output)
|
||||||
|
except Exception, e:
|
||||||
|
errors.append(e)
|
||||||
|
|
||||||
|
tranformation_string = ' '.join(transformation_list)
|
||||||
|
return tranformation_string, errors
|
||||||
|
|||||||
Reference in New Issue
Block a user