OpenSlides/server/openslides/mediafiles/utils.py

34 lines
1.0 KiB
Python
Raw Normal View History

from PyPDF2 import PdfFileReader
from PyPDF2.utils import PdfReadError
def bytes_to_human(size):
# TODO: Read http://stackoverflow.com/a/1094933 and think about it.
if size < 1024:
size_string = "< 1 kB"
elif size >= 1024 * 1024:
mB = size / 1024 / 1024
size_string = "%d MB" % mB
else:
kB = size / 1024
size_string = "%d kB" % kB
return size_string
def get_pdf_information(mediafile):
result = {}
try:
pdf = PdfFileReader(mediafile)
result["pages"] = pdf.getNumPages()
2021-03-18 13:52:51 +01:00
except PdfReadError:
# File could be encrypted but not be detected by PyPDF.
result["pages"] = 0
result["encrypted"] = True
2021-03-18 13:52:51 +01:00
except (KeyError, OSError):
# Other errors. Mostly very rare to occur, but do not raise a 500:
# KeyError: https://github.com/mstamy2/PyPDF2/issues/353
# OSError: https://github.com/mstamy2/PyPDF2/issues/530
result["pages"] = 0
result["read_error"] = True
return result