Files
JiDe Zhang aa232448a3 Fix path handling for trailing slashes in file save nodes
This commit fixes path handling issues in DCI File Saver and Binary File Saver
nodes when output directories contain trailing slashes or need normalization.

Key improvements:
1. Enhanced ensure_directory function
   - Added path normalization using os.path.normpath()
   - Strip whitespace from directory paths
   - Handle trailing slashes correctly on all platforms
   - Prevent creation of invalid directory paths

2. Enhanced DCI File Saver path handling
   - Normalize output directory paths before processing
   - Added debug logging for normalized paths
   - Improved error handling for path normalization failures
   - Support for Windows-style paths with trailing backslashes

3. Enhanced Binary File Saver path handling
   - Normalize output directory paths before processing
   - Added debug logging for normalized paths
   - Consistent path handling with DCI File Saver
   - Cross-platform path separator handling

4. Path normalization features
   - Handles trailing slashes: "D:\test\" → "D:\test"
   - Handles multiple slashes: "path\\\\" → "path"
   - Strips whitespace: " path\ " → "path"
   - Cross-platform compatibility for all path formats
   - Prevents os.makedirs errors with invalid paths

5. Documentation updates
   - Updated README.md for both English and Chinese sections
   - Added path handling information to node descriptions
   - Clarified support for trailing slashes and path normalization
   - Enhanced user guidance for path input formats

Technical implementation:
- Modified ensure_directory() to use os.path.normpath()
- Added path normalization in both file saver nodes
- Enhanced error handling for path processing
- Added debug logging for path normalization steps
- Maintained backward compatibility with existing workflows

User experience improvements:
- Users can now use paths with trailing slashes without errors
- Automatic path normalization prevents common path issues
- Clear error messages for path-related problems
- Consistent behavior across Windows and Linux platforms
- Support for various path input formats

修复文件保存节点中尾随斜杠的路径处理问题

此提交修复了DCI文件保存器和二进制文件保存器节点在输出目录包含尾随斜杠
或需要规范化时的路径处理问题。

主要改进:
1. 增强ensure_directory函数
   - 使用os.path.normpath()添加路径规范化
   - 去除目录路径中的空白字符
   - 在所有平台上正确处理尾随斜杠
   - 防止创建无效的目录路径

2. 增强DCI文件保存器路径处理
   - 在处理前规范化输出目录路径
   - 为规范化路径添加调试日志
   - 改进路径规范化失败的错误处理
   - 支持带尾随反斜杠的Windows风格路径

3. 增强二进制文件保存器路径处理
   - 在处理前规范化输出目录路径
   - 为规范化路径添加调试日志
   - 与DCI文件保存器保持一致的路径处理
   - 跨平台路径分隔符处理

4. 路径规范化功能
   - 处理尾随斜杠:"D:\test\" → "D:\test"
   - 处理多个斜杠:"path\\\\" → "path"
   - 去除空白字符:" path\ " → "path"
   - 所有路径格式的跨平台兼容性
   - 防止os.makedirs在无效路径上出错

5. 文档更新
   - 更新README.md中英文部分
   - 在节点描述中添加路径处理信息
   - 明确支持尾随斜杠和路径规范化
   - 增强用户路径输入格式指导

技术实现:
- 修改ensure_directory()使用os.path.normpath()
- 在两个文件保存器节点中添加路径规范化
- 增强路径处理的错误处理
- 为路径规范化步骤添加调试日志
- 保持与现有工作流的向后兼容性

用户体验改进:
- 用户现在可以使用带尾随斜杠的路径而不会出错
- 自动路径规范化防止常见路径问题
- 路径相关问题的清晰错误信息
- Windows和Linux平台的一致行为
- 支持各种路径输入格式
2025-06-11 13:32:38 +08:00

53 lines
1.6 KiB
Python

import os
import tempfile
from io import BytesIO
def get_output_directory():
"""Get the output directory for saving files"""
try:
import folder_paths
output_dir = folder_paths.get_output_directory()
except ImportError:
output_dir = tempfile.gettempdir()
except Exception as e:
print(f"Error accessing ComfyUI output directory: {e}")
output_dir = tempfile.gettempdir()
return output_dir
def clean_file_name(file_name):
"""Clean up file name by removing path separators"""
clean_name = os.path.basename(file_name) if file_name else "binary_file"
return clean_name if clean_name else "binary_file"
def ensure_directory(directory_path):
"""Ensure directory exists, create if not"""
if directory_path:
# Normalize the path and remove trailing separators
normalized_path = os.path.normpath(directory_path.strip())
if normalized_path and normalized_path != '.':
os.makedirs(normalized_path, exist_ok=True)
return True
return False
def save_binary_data(binary_data, file_path):
"""Save binary data to file"""
try:
with open(file_path, 'wb') as f:
bytes_written = f.write(binary_data)
return bytes_written
except Exception as e:
print(f"Error saving binary data: {e}")
return 0
def load_binary_data(file_path):
"""Load binary data from file"""
try:
if not os.path.exists(file_path):
return None
with open(file_path, 'rb') as f:
return f.read()
except Exception as e:
print(f"Error loading binary data: {e}")
return None