From 626da2d0400e29546faa37c3dfc6ee7af9828dd2 Mon Sep 17 00:00:00 2001 From: Krishn Parasar <76171905+Krishn1412@users.noreply.github.com> Date: Fri, 13 Dec 2024 19:32:53 +0530 Subject: [PATCH] Updated code to handle test cases and comment --- falcon/util/misc.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/falcon/util/misc.py b/falcon/util/misc.py index 62226831d..0a512197c 100644 --- a/falcon/util/misc.py +++ b/falcon/util/misc.py @@ -29,10 +29,10 @@ import functools import http import inspect +import os import re from typing import Any, Callable, Dict, List, Mapping, Optional, Tuple, Union import unicodedata -import os from falcon import status_codes from falcon.constants import PYPY @@ -337,7 +337,7 @@ def get_argnames(func: Callable[..., Any]) -> List[str]: return args -def secure_filename(filename: str, max_length: int = None) -> str: +def secure_filename(filename: str, max_length: Optional[int] = None) -> str: """Sanitize the provided `filename` to contain only ASCII characters. Only ASCII alphanumerals, ``'.'``, ``'-'`` and ``'_'`` are allowed for @@ -380,7 +380,10 @@ def secure_filename(filename: str, max_length: int = None) -> str: if max_length is not None and len(filename) > max_length: name, ext = os.path.splitext(filename) - filename = name[:max_length - len(ext)] + ext + if max_length < len(ext): + filename = ext[:max_length] + else: + filename = name[: max_length - len(ext)] + ext return filename