Skip to content

Commit cd64f77

Browse files
authored
Merge pull request #1428 from qylf0000/fix_1418-file-name
Fix #1418: OSError: [Errno 36] File name too long with long Chinese prompt
2 parents ad63add + c75a4be commit cd64f77

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

backend/server/server_utils.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,20 @@ def sanitize_filename(filename: str) -> str:
110110
# 255 - len(os.getcwd()) - len("\\gpt-researcher\\outputs\\") - len("task_") - len(timestamp) - len("_.json") - safety_margin
111111
max_task_length = 255 - len(os.getcwd()) - 24 - 5 - 10 - 6 - 5 # ~189 chars for task
112112

113-
# Truncate task if needed
114-
truncated_task = task[:max_task_length] if len(task) > max_task_length else task
115-
113+
# Truncate task if needed (by bytes)
114+
truncated_task = ""
115+
byte_count = 0
116+
for char in task:
117+
char_bytes = len(char.encode('utf-8'))
118+
if byte_count + char_bytes <= max_task_length:
119+
truncated_task += char
120+
byte_count += char_bytes
121+
else:
122+
break
123+
116124
# Reassemble and clean the filename
117125
sanitized = f"{prefix}_{timestamp}_{truncated_task}"
118-
return re.sub(r"[^\w\s-]", "", sanitized).strip()
126+
return re.sub(r"[^\w-]", "", sanitized).strip()
119127

120128

121129
async def handle_start_command(websocket, data: str, manager):

0 commit comments

Comments
 (0)