Files
zccrs-comfyui-dci/py/utils/file_utils.py
T
JiDe Zhang 62f7c0d2a9 refactor: Refactor nodes.py to separate node implementations into independent files
1. Create utility modules (py/utils/):
   - image_utils.py: Image processing utilities
   - file_utils.py: File operation utilities
   - ui_utils.py: UI-related utilities

2. Create node modules (py/nodes/):
   - base_node.py: Base node class with error handling
   - preview_node.py: DCI preview node implementation
   - image_node.py: DCI image creation node implementation
   - debug_node.py: DCI debug node implementation
   - file_node.py: DCI file and binary file operation nodes

3. Main improvements:
   - Cleaner code organization with single responsibility modules
   - Better code reusability to avoid code duplication
   - Easier maintenance and extensibility
   - Better error handling and logging
   - Improved modularity and encapsulation

refactor: 重构nodes.py,将不同的节点实现分离到独立文件中

1. 创建工具模块 (py/utils/):
   - image_utils.py:图像处理工具
   - file_utils.py:文件操作工具
   - ui_utils.py:UI相关工具

2. 创建节点模块 (py/nodes/):
   - base_node.py:带错误处理的节点基类
   - preview_node.py:DCI预览节点实现
   - image_node.py:DCI图像创建节点实现
   - debug_node.py:DCI调试节点实现
   - file_node.py:DCI文件和二进制文件操作节点

3. 主要改进:
   - 代码组织更清晰,每个模块职责单一
   - 代码复用性更好,避免重复代码
   - 更容易维护和扩展
   - 更好的错误处理和日志记录
   - 更好的模块化和封装
2025-06-10 12:58:18 +08:00

50 lines
1.4 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:
os.makedirs(directory_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