Merge pull request #5966 from FinnStutzenstein/smallMediafileFixes

Small mediafile fixes
This commit is contained in:
Finn Stutzenstein 2021-03-19 07:34:02 +01:00 committed by GitHub
commit 63132fdbc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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

@ -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()]
):

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),