Small mediafile fixes

This commit is contained in:
Finn Stutzenstein 2021-03-18 13:52:51 +01:00
parent 365d0d55ea
commit e45d83de5a
No known key found for this signature in database
GPG Key ID: 9042F605C6324654
3 changed files with 18 additions and 7 deletions

View File

@ -20,9 +20,14 @@ def get_pdf_information(mediafile):
try:
pdf = PdfFileReader(mediafile)
result["pages"] = pdf.getNumPages()
except (PdfReadError, KeyError):
except PdfReadError:
# File could be encrypted but not be detected by PyPDF.
# KeyError: https://github.com/mstamy2/PyPDF2/issues/353 Very rare to occur, but do not raise a 500
result["pages"] = 0
result["encrypted"] = True
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

View File

@ -301,14 +301,19 @@ def get_mediafile(request, path):
can_see = has_perm(request.user, "mediafiles.can_see")
for i, part in enumerate(parts):
is_directory = i < len(parts) - 1
# A .get would be sufficient, but sometimes someone has uploaded a file twice due to complicated
# transaction management of two databases during create. So instead of returning a 500er (since
# .get returned multiple objects) we deliver the first file.
if is_directory:
mediafile = Mediafile.objects.get(
mediafile = Mediafile.objects.filter(
parent=parent, is_directory=is_directory, title=part
)
).first()
else:
mediafile = Mediafile.objects.get(
mediafile = Mediafile.objects.filter(
parent=parent, is_directory=is_directory, original_filename=part
)
).first()
if mediafile is None:
raise Mediafile.DoesNotExist()
if mediafile.access_groups.exists() and not in_some_groups(
request.user.id, [group.id for group in mediafile.access_groups.all()]
):

View File

@ -38,6 +38,7 @@ class TestFunctions(TestCase):
@patch("openslides.utils.main.detect_openslides_type")
@patch("openslides.utils.main.os.path.expanduser")
def test_get_default_settings_dir_unix(self, mock_expanduser, mock_detect):
os.environ.pop("XDG_CONFIG_HOME", None)
mock_expanduser.return_value = "/home/test/.config"
self.assertEqual(
main.get_default_settings_dir(main.UNIX_VERSION),