Skip to content

Commit

Permalink
Truncate file names
Browse files Browse the repository at this point in the history
I noticed that if you do a very long query the file names that get generated can be larger than the host permits. So I added some truncation that should work pretty universally to limit the file name to under <250 characters total including the important naming convention of the prefixes.
  • Loading branch information
kga245 committed Dec 20, 2024
1 parent aa371ca commit 70de3c4
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion backend/server/server_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,20 @@ async def research(self) -> dict:
}

def sanitize_filename(filename: str) -> str:
return re.sub(r"[^\w\s-]", "", filename).strip()
# Split into components
prefix, timestamp, *task_parts = filename.split('_')
task = '_'.join(task_parts)

# Calculate max length for task portion
# 255 - len("outputs/") - len("task_") - len(timestamp) - len("_.json") - safety_margin
max_task_length = 255 - 8 - 5 - 10 - 6 - 10 # ~216 chars for task

# Truncate task if needed
truncated_task = task[:max_task_length] if len(task) > max_task_length else task

# Reassemble and clean the filename
sanitized = f"{prefix}_{timestamp}_{truncated_task}"
return re.sub(r"[^\w\s-]", "", sanitized).strip()


async def handle_start_command(websocket, data: str, manager):
Expand Down

0 comments on commit 70de3c4

Please sign in to comment.