kiwix: Don't leave invalid .zim in library after a failed attempt

Tests:

- Without patch, upload an invalid zim file, 'Failed to add content'... message
is shown. The library's content directory contains that invalid file. Try to add
the file again and the message shown is 'File already exists'.

- With patch, upload an invalid zim file, 'Failed to add content'... message is
shown. The library's content directory does not contain that file. Try to add
the file again and the same message is shown.

- Functional tests for kiwix pass. Repeating just the test
test_add_invalid_zim_file works.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
This commit is contained in:
Sunil Mohan Adapa 2024-10-02 16:04:26 -07:00 committed by Veiko Aasa
parent 876cace107
commit 2f33026577
No known key found for this signature in database
GPG Key ID: 478539CAE680674E

View File

@ -33,6 +33,7 @@ def add_package(file_name: str, temporary_file_path: str):
aria2c is used for both HTTP and Magnet downloads.
"""
kiwix.validate_file_name(file_name)
# file_name is verified further by move_uploaded_file()
# Moving files to the Kiwix library path ensures that
# they can't be removed by other apps or users.
@ -42,7 +43,13 @@ def add_package(file_name: str, temporary_file_path: str):
user='root', group='root',
permissions=0o644)
_kiwix_manage_add(str(CONTENT_DIR / file_name))
content_file = CONTENT_DIR / file_name
try:
_kiwix_manage_add(str(CONTENT_DIR / file_name))
except subprocess.CalledProcessError:
# Remove the uploaded file if we could not added it to library
content_file.unlink()
raise
def _kiwix_manage_add(zim_file: str):