From e45d83de5a893a80d69111746251aa954aa491fa Mon Sep 17 00:00:00 2001 From: Finn Stutzenstein Date: Thu, 18 Mar 2021 13:52:51 +0100 Subject: [PATCH] Small mediafile fixes --- server/openslides/mediafiles/utils.py | 9 +++++++-- server/openslides/mediafiles/views.py | 15 ++++++++++----- server/tests/old/utils/test_main.py | 1 + 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/server/openslides/mediafiles/utils.py b/server/openslides/mediafiles/utils.py index c9aef9666..fceaed1cd 100644 --- a/server/openslides/mediafiles/utils.py +++ b/server/openslides/mediafiles/utils.py @@ -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 diff --git a/server/openslides/mediafiles/views.py b/server/openslides/mediafiles/views.py index e03b6bc99..ce3365eec 100644 --- a/server/openslides/mediafiles/views.py +++ b/server/openslides/mediafiles/views.py @@ -292,7 +292,7 @@ def get_mediafile(request, path): A user must have all access permissions for all folders the the file itself, or the file is a special file (logo or font), then it is always returned. - If the mediafile cannot be found, a Mediafile.DoesNotExist will be raised. + If the mediafile cannot be found, a Mediafile.DoesNotExist will be raised. """ if not path: raise Mediafile.DoesNotExist() @@ -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()] ): diff --git a/server/tests/old/utils/test_main.py b/server/tests/old/utils/test_main.py index 5561b7259..a4cea042c 100644 --- a/server/tests/old/utils/test_main.py +++ b/server/tests/old/utils/test_main.py @@ -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),