Skip to content

Commit c2e2636

Browse files
committed
Add output path validation to PDF processing methods
1 parent d81cf4f commit c2e2636

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

main.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ def process_pdf(self):
424424

425425
def _process_selection(self, page_input):
426426
"""Process PDF in selection mode."""
427+
self._ensure_output_path()
427428
try:
428429
page_numbers = parse_page_ranges(page_input)
429430
except (ValueError, AttributeError) as e:
@@ -438,6 +439,7 @@ def _process_selection(self, page_input):
438439

439440
def _process_split(self, chunk_input):
440441
"""Process PDF in split mode."""
442+
self._ensure_output_path()
441443
try:
442444
chunk_size = int(chunk_input)
443445
if chunk_size <= 0:
@@ -476,6 +478,21 @@ def _show_success(self, message):
476478
self.status_label.setStyleSheet("color: #4caf50;")
477479
QMessageBox.information(self, "Success", message)
478480

481+
def _ensure_output_path(self):
482+
"""Validate the output path before writing files."""
483+
if not self.output_path:
484+
raise ValueError("Please choose an output location before processing.")
485+
486+
if not self.input_path:
487+
return
488+
489+
input_path = Path(self.input_path).resolve()
490+
output_path = Path(self.output_path).resolve()
491+
492+
# Prevent overwriting the source file, which can corrupt the PDF while reading.
493+
if input_path == output_path:
494+
raise ValueError("Output file must be different from the input file.")
495+
479496

480497
if __name__ == "__main__":
481498
app = QApplication(sys.argv)

0 commit comments

Comments
 (0)