Minor bug fixes
This commit is contained in:
63
oai.py
63
oai.py
@@ -4650,25 +4650,55 @@ def chat():
|
||||
file_attachments = []
|
||||
content_blocks = []
|
||||
|
||||
# Smart file detection with extension whitelist
|
||||
# Only matches files with known extensions or clear path prefixes
|
||||
#
|
||||
# Valid matches:
|
||||
# @/path/to/file
|
||||
# @~/file.txt
|
||||
# @./script.py
|
||||
# @report.pdf
|
||||
#
|
||||
# Excludes:
|
||||
# @domain.com (no whitelisted extension)
|
||||
# @mail.server.eu (TLD not in whitelist)
|
||||
# user@domain.com (email pattern)
|
||||
# Diagnostic-Code: @server.eu (TLD not in whitelist)
|
||||
# Smart file detection: Simple pattern + extension validation
|
||||
# This avoids extremely long regex that can cause binary signing issues
|
||||
|
||||
file_pattern = r'(?:^|\s)@((?:[~/][\S]+)|(?:\.[\S]+)|(?:[\w][\w-]*\.(?:py|txt|md|log|json|csv|pdf|png|jpg|jpeg|gif|bmp|webp|svg|ico|zip|tar|gz|bz2|7z|rar|xz|js|ts|jsx|tsx|vue|html|css|scss|sass|less|xml|yaml|yml|toml|ini|conf|cfg|env|properties|sh|bash|zsh|fish|bat|cmd|ps1|c|cpp|cc|cxx|h|hpp|hxx|java|class|jar|war|rb|go|rs|swift|kt|kts|php|sql|db|sqlite|sqlite3|lock|gitignore|dockerignore|editorconfig|eslintrc|prettierrc|babelrc|nvmrc|npmrc|pyc|pyo|pyd|so|dll|dylib|exe|app|dmg|pkg|deb|rpm|apk|ipa|wasm|proto|graphql|graphqls|grpc|avro|parquet|orc|feather|arrow|hdf5|h5|mat|r|rdata|rds|pkl|pickle|joblib|npy|npz|safetensors|onnx|pt|pth|ckpt|pb|tflite|mlmodel|coreml|rknn)))(?=\s|$)'
|
||||
# Common file extensions we support
|
||||
ALLOWED_FILE_EXTENSIONS = {
|
||||
# Code
|
||||
'.py', '.js', '.ts', '.jsx', '.tsx', '.vue', '.java', '.c', '.cpp', '.cc', '.cxx',
|
||||
'.h', '.hpp', '.hxx', '.rb', '.go', '.rs', '.swift', '.kt', '.kts', '.php',
|
||||
'.sh', '.bash', '.zsh', '.fish', '.bat', '.cmd', '.ps1',
|
||||
# Data
|
||||
'.json', '.csv', '.yaml', '.yml', '.toml', '.xml', '.sql', '.db', '.sqlite', '.sqlite3',
|
||||
# Documents
|
||||
'.txt', '.md', '.log', '.conf', '.cfg', '.ini', '.env', '.properties',
|
||||
# Images
|
||||
'.png', '.jpg', '.jpeg', '.gif', '.bmp', '.webp', '.svg', '.ico',
|
||||
# Archives
|
||||
'.zip', '.tar', '.gz', '.bz2', '.7z', '.rar', '.xz',
|
||||
# Config files
|
||||
'.lock', '.gitignore', '.dockerignore', '.editorconfig', '.eslintrc',
|
||||
'.prettierrc', '.babelrc', '.nvmrc', '.npmrc',
|
||||
# Binary/Compiled
|
||||
'.pyc', '.pyo', '.pyd', '.so', '.dll', '.dylib', '.exe', '.app',
|
||||
'.dmg', '.pkg', '.deb', '.rpm', '.apk', '.ipa',
|
||||
# ML/AI
|
||||
'.pkl', '.pickle', '.joblib', '.npy', '.npz', '.safetensors', '.onnx',
|
||||
'.pt', '.pth', '.ckpt', '.pb', '.tflite', '.mlmodel', '.coreml', '.rknn',
|
||||
# Data formats
|
||||
'.wasm', '.proto', '.graphql', '.graphqls', '.grpc', '.avro', '.parquet',
|
||||
'.orc', '.feather', '.arrow', '.hdf5', '.h5', '.mat', '.r', '.rdata', '.rds',
|
||||
# Other
|
||||
'.pdf', '.class', '.jar', '.war'
|
||||
}
|
||||
|
||||
# Simple pattern: @filepath where filepath starts with ~, /, . or has an extension
|
||||
# Much shorter than listing all extensions in the regex
|
||||
file_pattern = r'(?:^|\s)@([~/\.][\S]+|[\w][\w-]*\.[\w]+)(?=\s|$)'
|
||||
|
||||
for match in re.finditer(file_pattern, user_input, re.IGNORECASE):
|
||||
file_path = match.group(1)
|
||||
|
||||
# Get the extension
|
||||
file_ext = os.path.splitext(file_path)[1].lower()
|
||||
|
||||
# Skip if it doesn't start with a path char and has no allowed extension
|
||||
if not file_path.startswith(('/', '~', '.', '\\')):
|
||||
if not file_ext or file_ext not in ALLOWED_FILE_EXTENSIONS:
|
||||
# This looks like a domain name, not a file
|
||||
continue
|
||||
|
||||
expanded_path = os.path.expanduser(os.path.abspath(file_path))
|
||||
|
||||
if not os.path.exists(expanded_path) or os.path.isdir(expanded_path):
|
||||
@@ -4681,7 +4711,6 @@ def chat():
|
||||
continue
|
||||
|
||||
mime_type, _ = mimetypes.guess_type(expanded_path)
|
||||
file_ext = os.path.splitext(expanded_path)[1].lower()
|
||||
|
||||
try:
|
||||
with open(expanded_path, 'rb') as f:
|
||||
@@ -4729,7 +4758,7 @@ def chat():
|
||||
app_logger.error(f"File read error for {expanded_path}: {e}")
|
||||
continue
|
||||
|
||||
# Remove file attachments from text (use same pattern as file detection)
|
||||
# Remove file attachments from text (use same simple pattern)
|
||||
text_part = re.sub(file_pattern, lambda m: m.group(0)[0] if m.group(0)[0].isspace() else '', text_part).strip()
|
||||
|
||||
# Build message content
|
||||
|
||||
Reference in New Issue
Block a user