diff --git a/apps/converter/api.py b/apps/converter/api.py index 3a5b855ada..a4bb081845 100644 --- a/apps/converter/api.py +++ b/apps/converter/api.py @@ -5,7 +5,7 @@ import hashlib from common.conf.settings import TEMPORARY_DIRECTORY from converter.conf.settings import UNOCONV_PATH -from converter.exceptions import OfficeConversionError +from converter.exceptions import OfficeConversionError, UnknownFileFormat from converter.literals import DEFAULT_PAGE_NUMBER, \ DEFAULT_ZOOM_LEVEL, DEFAULT_ROTATION, DEFAULT_FILE_FORMAT @@ -119,7 +119,12 @@ def convert(input_filepath, output_filepath=None, cleanup_files=False, *args, ** def get_page_count(input_filepath): - return backend.get_page_count(input_filepath) + try: + return backend.get_page_count(input_filepath) + except UnknownFileFormat: + # If converter backend doesn't understand the format return + # 1 as the total page count + return 1 def get_document_dimensions(document, *args, **kwargs): diff --git a/apps/converter/backends/graphicsmagick/base.py b/apps/converter/backends/graphicsmagick/base.py index d4d720b894..559b31506d 100644 --- a/apps/converter/backends/graphicsmagick/base.py +++ b/apps/converter/backends/graphicsmagick/base.py @@ -3,7 +3,7 @@ import re from converter.conf.settings import GM_PATH from converter.conf.settings import GM_SETTINGS -from converter.exceptions import ConvertError, UnknownFormat, \ +from converter.exceptions import ConvertError, UnknownFileFormat, \ IdentifyError from converter.backends import ConverterBase from converter.literals import TRANSFORMATION_RESIZE, \ @@ -79,7 +79,7 @@ class ConverterClass(ConverterBase): error_line = proc.stderr.readline() if (CONVERTER_ERROR_STRING_NO_DECODER in error_line) or (CONVERTER_ERROR_STARTS_WITH in error_line): #Try to determine from error message which class of error is it - raise UnknownFormat + raise UnknownFileFormat else: raise ConvertError(error_line) @@ -116,6 +116,5 @@ class ConverterClass(ConverterBase): def get_page_count(self, input_filepath): try: return len(self.identify_file(unicode(input_filepath)).splitlines()) - except: - #TODO: send to other page number identifying program - return 1 + except IdentifyError: + raise UnknownFileFormat diff --git a/apps/converter/backends/imagemagick/base.py b/apps/converter/backends/imagemagick/base.py index e55923de2b..1b5ffbaa97 100644 --- a/apps/converter/backends/imagemagick/base.py +++ b/apps/converter/backends/imagemagick/base.py @@ -3,7 +3,7 @@ import re from converter.conf.settings import IM_IDENTIFY_PATH from converter.conf.settings import IM_CONVERT_PATH -from converter.exceptions import ConvertError, UnknownFormat, \ +from converter.exceptions import ConvertError, UnknownFileFormat, \ IdentifyError from converter.backends import ConverterBase from converter.literals import TRANSFORMATION_RESIZE, \ @@ -75,7 +75,7 @@ class ConverterClass(ConverterBase): error_line = proc.stderr.readline() if CONVERTER_ERROR_STRING_NO_DECODER in error_line: #Try to determine from error message which class of error is it - raise UnknownFormat + raise UnknownFileFormat else: raise ConvertError(error_line) @@ -114,6 +114,5 @@ class ConverterClass(ConverterBase): def get_page_count(self, input_filepath): try: return len(self.identify_file(unicode(input_filepath)).splitlines()) - except: - #TODO: send to other page number identifying program - return 1 + except IdentifyError: + raise UnknownFileFormat diff --git a/apps/converter/backends/python/base.py b/apps/converter/backends/python/base.py index 71386bf345..910a143437 100644 --- a/apps/converter/backends/python/base.py +++ b/apps/converter/backends/python/base.py @@ -14,7 +14,7 @@ from django.utils.translation import ugettext_lazy as _ from mimetype.api import get_mimetype -from converter.exceptions import ConvertError, UnknownFormat, IdentifyError +from converter.exceptions import ConvertError, UnknownFileFormat, IdentifyError from converter.backends import ConverterBase from converter.literals import TRANSFORMATION_RESIZE, \ TRANSFORMATION_ROTATE, TRANSFORMATION_ZOOM @@ -38,9 +38,7 @@ class ConverterClass(ConverterBase): try: im = Image.open(input_filepath) except IOError: #cannot identify image file - # Return a page count of 1, to atleast allow the document - # to be created - return 1 + return UnknownFileFormat try: while 1: @@ -87,7 +85,7 @@ class ConverterClass(ConverterBase): try: im = Image.open(input_filepath) except Exception: # Python Imaging Library doesn't recognize it as an image - raise UnknownFormat + raise UnknownFileFormat finally: if tmpfile: cleanup(tmpfile) diff --git a/apps/converter/exceptions.py b/apps/converter/exceptions.py index 1880f0ba39..e90fd4bb34 100644 --- a/apps/converter/exceptions.py +++ b/apps/converter/exceptions.py @@ -5,17 +5,16 @@ class ConvertError(Exception): pass -class UnknownFormat(ConvertError): +class UnknownFileFormat(ConvertError): """ - Raised when the converter backend can't understand or there - isn't an appropiate driver available + Raised when the converter backend can't understand a file """ pass class IdentifyError(ConvertError): """ - Raised by identify + Raised by the graphcismagick and imagemagics identify program """ pass diff --git a/apps/documents/models.py b/apps/documents/models.py index ea5faf1ba6..f5279137cf 100644 --- a/apps/documents/models.py +++ b/apps/documents/models.py @@ -16,7 +16,7 @@ from dynamic_search.api import register from converter.api import get_page_count from converter.api import get_available_transformations_choices from converter.api import convert -from converter.exceptions import UnknownFormat, UnkownConvertError +from converter.exceptions import UnknownFileFormat, UnkownConvertError from mimetype.api import get_document_mimetype, get_icon_file_path, \ get_error_icon_file_path @@ -245,7 +245,7 @@ class Document(models.Model): try: image_cache_name = self.get_image_cache_name(page=page) return convert(image_cache_name, cleanup_files=False, size=size, zoom=zoom, rotation=rotation) - except UnknownFormat: + except UnknownFileFormat: return get_icon_file_path(self.file_mimetype) except UnkownConvertError: return get_error_icon_file_path() diff --git a/apps/sources/staging.py b/apps/sources/staging.py index 230eb44dab..3c8935630c 100644 --- a/apps/sources/staging.py +++ b/apps/sources/staging.py @@ -10,7 +10,7 @@ from django.utils.translation import ugettext_lazy as _ from mimetype.api import get_icon_file_path, get_error_icon_file_path, \ get_mimetype from converter.api import convert, cache_cleanup -from converter.exceptions import UnknownFormat, UnkownConvertError +from converter.exceptions import UnknownFileFormat, UnkownConvertError DEFAULT_STAGING_DIRECTORY = u'/tmp' @@ -138,7 +138,7 @@ class StagingFile(object): def get_image(self, size, transformations): try: return convert(self.filepath, size=size, cleanup_files=False, transformations=transformations) - except UnknownFormat: + except UnknownFileFormat: return get_icon_file_path(get_mimetype(self.filepath)) except UnkownConvertError: return get_error_icon_file_path()