diff --git a/changelog.md b/changelog.md index 707f41e..f3365cd 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,12 @@ # Changelog +## [1.3.2] - 1 December 2020 + +### Changed + + - mglib.pdfinfo.get_pagecount use python magic + file extention to determine correct mime type (and thus page count) + ## [1.3.1] - 1 December 2020 ### Changed diff --git a/mglib/pdfinfo.py b/mglib/pdfinfo.py index ef82c6b..db720df 100644 --- a/mglib/pdfinfo.py +++ b/mglib/pdfinfo.py @@ -64,22 +64,42 @@ def get_pagecount(filepath): if os.path.isdir(filepath): raise ValueError("Filepath %s is a directory!" % filepath) + base, ext = os.path.splitext(filepath) mime_type = from_file(filepath, mime=True) - # pure images (png, jpeg) have only one page :) + if mime_type in ['image/png', 'image/jpeg', 'image/jpg']: # whatever png/jpg image is there - it is # considered by default one page document. return 1 + # In case of REST API upload (via PUT + form multipart) + # django saves temporary file as application/octet-stream + # Checking extentions is an extra method of finding out correct + # mime type + if ext and ext.lower() in ('.jpeg', '.png', '.jpg'): + return 1 + if mime_type == 'image/tiff': return get_tiff_pagecount(filepath) + # In case of REST API upload (via PUT + form multipart) + # django saves temporary file as application/octet-stream + # Checking extentions is an extra method of finding out correct + # mime type + if ext and ext.lower() in ('.tiff', ): + return get_tiff_pagecount(filepath) + if mime_type != 'application/pdf': - raise FileTypeNotSupported( - "Only jpeg, png, pdf and tiff are handled by this" - " method" - ) + # In case of REST API upload (via PUT + form multipart) + # django saves temporary file as application/octet-stream + # Checking extentions is an extra method of finding out correct + # mime type + if ext and ext.lower() != '.pdf': + raise FileTypeNotSupported( + "Only jpeg, png, pdf and tiff are handled by this" + " method" + ) # pdfinfo "${PDFFILE}" | grep Pages cmd = [ settings.BINARY_PDFINFO, diff --git a/setup.py b/setup.py index c8b481f..7f20ffc 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ with open("README.md", "r") as fh: setup( name="mglib", - version="1.3.1", + version="1.3.2", author="Eugen Ciur", author_email="eugen@papermerge.com", url="https://github.com/papermerge/mglib",