Convert the 'document_save_to_temp_dir' utility function into a method of the Document model

This commit is contained in:
Roberto Rosario
2014-10-13 17:51:28 -04:00
parent f3de7f84f2
commit 8200204ddc
3 changed files with 8 additions and 13 deletions

View File

@@ -20,6 +20,7 @@ from django.utils.translation import ugettext
from django.utils.translation import ugettext_lazy as _
from acls.utils import apply_default_acls
from common.settings import TEMPORARY_DIRECTORY
from converter.api import (convert, get_page_count,
get_available_transformations_choices)
from converter.exceptions import UnknownFileFormat
@@ -38,7 +39,6 @@ from .managers import (DocumentPageTransformationManager, DocumentTypeManager,
from .runtime import storage_backend
from .settings import (CACHE_PATH, CHECKSUM_FUNCTION, DISPLAY_SIZE,
UUID_FUNCTION, ZOOM_MAX_LEVEL, ZOOM_MIN_LEVEL)
from .utils import document_save_to_temp_dir
HASH_FUNCTION = lambda x: hashlib.sha256(x).hexdigest() # document image cache name hash function
logger = logging.getLogger(__name__)
@@ -138,7 +138,7 @@ class Document(models.Model):
return cache_file_path
else:
document_version = DocumentVersion.objects.get(pk=version)
document_file = document_save_to_temp_dir(document_version, document_version.checksum)
document_file = document_version.document.document_save_to_temp_dir(document_version.checksum)
return convert(input_filepath=document_file, output_filepath=cache_file_path, page=page, transformations=transformations, mimetype=self.file_mimetype)
def get_valid_image(self, size=DISPLAY_SIZE, page=DEFAULT_PAGE_NUMBER, zoom=DEFAULT_ZOOM_LEVEL, rotation=DEFAULT_ROTATION, version=None):
@@ -307,6 +307,10 @@ class Document(models.Model):
version.filename = value
return version.save()
def document_save_to_temp_dir(self, filename, buffer_size=1024 * 1024):
temporary_path = os.path.join(TEMPORARY_DIRECTORY, filename)
return self.save_to_file(temporary_path, buffer_size)
filename = property(_get_filename, _set_filename)

View File

@@ -1,8 +0,0 @@
import os
from common.settings import TEMPORARY_DIRECTORY
def document_save_to_temp_dir(document, filename, buffer_size=1024 * 1024):
temporary_path = os.path.join(TEMPORARY_DIRECTORY, filename)
return document.save_to_file(temporary_path, buffer_size)

View File

@@ -12,7 +12,6 @@ from converter import office_converter
from converter.exceptions import OfficeConversionError
from converter.office_converter import (CONVERTER_OFFICE_FILE_MIMETYPES,
OfficeConverter)
from documents.utils import document_save_to_temp_dir
from ..settings import PDFTOTEXT_PATH
@@ -103,7 +102,7 @@ class OfficeParser(Parser):
logger.debug('executing')
try:
office_converter = OfficeConverter()
document_file = document_save_to_temp_dir(document_page.document, document_page.document.checksum)
document_file = document_page.document.document_save_to_temp_dir(document_page.document.checksum)
logger.debug('document_file: %s', document_file)
office_converter.convert(document_file, mimetype=document_page.document.file_mimetype)
@@ -141,7 +140,7 @@ class PopplerParser(Parser):
copyfile(descriptor, temp_filepath)
document_file = temp_filepath
else:
document_file = document_save_to_temp_dir(document_page.document, document_page.document.checksum)
document_file = document_page.document.document_save_to_temp_dir(document_page.document.checksum)
logger.debug('document_file: %s', document_file)