mirror of https://github.com/papermerge/mglib
add extra checks for mime type, inc version, fix failing tests
parent
fe20ddd72b
commit
06be42542a
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue