mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-09-22 00:52:42 -05:00
Fix: add extra error handling to _consume for file checks (#10897)
This commit is contained in:
@@ -82,6 +82,13 @@ def _is_ignored(filepath: Path) -> bool:
|
|||||||
|
|
||||||
|
|
||||||
def _consume(filepath: Path) -> None:
|
def _consume(filepath: Path) -> None:
|
||||||
|
# Check permissions early
|
||||||
|
try:
|
||||||
|
filepath.stat()
|
||||||
|
except (PermissionError, OSError):
|
||||||
|
logger.warning(f"Not consuming file {filepath}: Permission denied.")
|
||||||
|
return
|
||||||
|
|
||||||
if filepath.is_dir() or _is_ignored(filepath):
|
if filepath.is_dir() or _is_ignored(filepath):
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -323,7 +330,12 @@ class Command(BaseCommand):
|
|||||||
|
|
||||||
# Also make sure the file exists still, some scanners might write a
|
# Also make sure the file exists still, some scanners might write a
|
||||||
# temporary file first
|
# temporary file first
|
||||||
|
try:
|
||||||
file_still_exists = filepath.exists() and filepath.is_file()
|
file_still_exists = filepath.exists() and filepath.is_file()
|
||||||
|
except (PermissionError, OSError): # pragma: no cover
|
||||||
|
# If we can't check, let it fail in the _consume function
|
||||||
|
file_still_exists = True
|
||||||
|
continue
|
||||||
|
|
||||||
if waited_long_enough and file_still_exists:
|
if waited_long_enough and file_still_exists:
|
||||||
_consume(filepath)
|
_consume(filepath)
|
||||||
|
@@ -209,6 +209,26 @@ class TestConsumer(DirectoriesMixin, ConsumerThreadMixin, TransactionTestCase):
|
|||||||
# assert that we have an error logged with this invalid file.
|
# assert that we have an error logged with this invalid file.
|
||||||
error_logger.assert_called_once()
|
error_logger.assert_called_once()
|
||||||
|
|
||||||
|
@mock.patch("documents.management.commands.document_consumer.logger.warning")
|
||||||
|
def test_permission_error_on_prechecks(self, warning_logger):
|
||||||
|
filepath = Path(self.dirs.consumption_dir) / "selinux.txt"
|
||||||
|
filepath.touch()
|
||||||
|
|
||||||
|
original_stat = Path.stat
|
||||||
|
|
||||||
|
def raising_stat(self, *args, **kwargs):
|
||||||
|
if self == filepath:
|
||||||
|
raise PermissionError("Permission denied")
|
||||||
|
return original_stat(self, *args, **kwargs)
|
||||||
|
|
||||||
|
with mock.patch("pathlib.Path.stat", new=raising_stat):
|
||||||
|
document_consumer._consume(filepath)
|
||||||
|
|
||||||
|
warning_logger.assert_called_once()
|
||||||
|
(args, _) = warning_logger.call_args
|
||||||
|
self.assertIn("Permission denied", args[0])
|
||||||
|
self.consume_file_mock.assert_not_called()
|
||||||
|
|
||||||
@override_settings(CONSUMPTION_DIR="does_not_exist")
|
@override_settings(CONSUMPTION_DIR="does_not_exist")
|
||||||
def test_consumption_directory_invalid(self):
|
def test_consumption_directory_invalid(self):
|
||||||
self.assertRaises(CommandError, call_command, "document_consumer", "--oneshot")
|
self.assertRaises(CommandError, call_command, "document_consumer", "--oneshot")
|
||||||
|
Reference in New Issue
Block a user