diff --git a/dojo/forms.py b/dojo/forms.py index 3577a9ff2ab..d56cd1ebad2 100644 --- a/dojo/forms.py +++ b/dojo/forms.py @@ -4,6 +4,7 @@ import re import warnings from datetime import date, datetime +from pathlib import Path import tagulous from crispy_forms.bootstrap import InlineCheckboxes, InlineRadios @@ -754,7 +755,8 @@ class UploadThreatForm(forms.Form): def clean(self): if (file := self.cleaned_data.get("file", None)) is not None: - ext = os.path.splitext(file.name)[1] # [0] returns path+filename + path = Path(file.name) + ext = path.suffix valid_extensions = [".jpg", ".png", ".pdf"] if ext.lower() not in valid_extensions: if accepted_extensions := f"{', '.join(valid_extensions)}": @@ -872,7 +874,8 @@ def clean(self): for form in self.forms: file = form.cleaned_data.get("file", None) if file: - ext = os.path.splitext(file.name)[1] # [0] returns path+filename + path = Path(file.name) + ext = path.suffix valid_extensions = settings.FILE_UPLOAD_TYPES if ext.lower() not in valid_extensions: if accepted_extensions := f"{', '.join(valid_extensions)}": diff --git a/dojo/models.py b/dojo/models.py index 037b22b919f..61bd6622fc6 100644 --- a/dojo/models.py +++ b/dojo/models.py @@ -141,7 +141,9 @@ def __init__(self, directory=None, keep_basename=False, keep_ext=True): self.keep_ext = keep_ext def __call__(self, model_instance, filename): - base, ext = os.path.splitext(filename) + path = Path(filename) + base = path.parent / path.stem + ext = path.suffix filename = f"{base}_{uuid4()}" if self.keep_basename else str(uuid4()) if self.keep_ext: filename += ext diff --git a/dojo/utils.py b/dojo/utils.py index d30ef1ea63c..c19da8945be 100644 --- a/dojo/utils.py +++ b/dojo/utils.py @@ -1381,7 +1381,8 @@ def get_page_items_and_count(request, items, page_size, prefix="", do_count=True def handle_uploaded_threat(f, eng): - _name, extension = os.path.splitext(f.name) + path = Path(f.name) + extension = path.suffix # Check if threat folder exist. if not Path(settings.MEDIA_ROOT + "/threat/").is_dir(): # Create the folder @@ -1395,7 +1396,8 @@ def handle_uploaded_threat(f, eng): def handle_uploaded_selenium(f, cred): - _name, extension = os.path.splitext(f.name) + path = Path(f.name) + extension = path.suffix with open(settings.MEDIA_ROOT + f"/selenium/{cred.id}{extension}", "wb+") as destination: for chunk in f.chunks(): @@ -2699,7 +2701,9 @@ def generate_file_response_from_file_path( ) -> FileResponse: """Serve an local file in a uniformed way.""" # Determine the file path - file_path_without_extension, file_extension = os.path.splitext(file_path) + path = Path(file_path) + file_path_without_extension = path.parent / path.stem + file_extension = path.suffix # Determine the file name if not supplied if file_name is None: file_name = file_path_without_extension.rsplit("/")[-1] diff --git a/ruff.toml b/ruff.toml index c78256a9d2e..cface3f73fa 100644 --- a/ruff.toml +++ b/ruff.toml @@ -65,7 +65,7 @@ select = [ "TCH", "INT", "ARG003", "ARG004", "ARG005", - "PTH2", "PTH101", "PTH102", "PTH103", "PTH104", "PTH105", "PTH106", "PTH107", "PTH108", "PTH109", "PTH110", "PTH111", "PTH112", "PTH113", "PTH114", "PTH115", "PTH116", "PTH117", "PTH119", "PTH120", "PTH121", "PTH124", + "PTH2", "PTH101", "PTH102", "PTH103", "PTH104", "PTH105", "PTH106", "PTH107", "PTH108", "PTH109", "PTH110", "PTH111", "PTH112", "PTH113", "PTH114", "PTH115", "PTH116", "PTH117", "PTH119", "PTH120", "PTH121", "PTH122", "PTH124", "TD001", "TD004", "TD005", "PD", "PGH",