-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtext_files.py
298 lines (250 loc) · 10.4 KB
/
text_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# Modified from the original pysssss text_files.py
# All credit and kudos to the original author
import os
import folder_paths
from server import PromptServer
import glob
from aiohttp import web
from .mng_json import json_manager, TroubleSgltn
from pathlib import Path
def get_allowed_dirs():
"""
Gets the allowed directories configuration using json_manager.
Returns:
dict: Dictionary of directory configurations from text_file_dirs.json
"""
j_mngr = json_manager()
user_dir = j_mngr.find_child_directory(j_mngr.script_dir, "user", create=True)
config_path = j_mngr.append_filename_to_path(user_dir, "text_file_dirs.json")
json_data = j_mngr.load_json(config_path, is_critical=True)
if not json_data:
msg = f"Failed to load configuration from: {config_path}"
j_mngr.log_events(msg, TroubleSgltn.Severity.ERROR, is_trouble=True)
raise FileNotFoundError(msg)
return json_data
def get_valid_dirs():
return get_allowed_dirs().keys()
def get_dir_from_name(name, j_mngr=None):
"""Get and validate directory path from configuration name"""
if j_mngr is None:
j_mngr = json_manager()
dirs = get_allowed_dirs()
if name not in dirs:
msg = f"Directory '{name}' not found in configuration"
j_mngr.log_events(msg, TroubleSgltn.Severity.ERROR, is_trouble=True)
raise KeyError(msg)
path = dirs[name]
path = path.replace("$input", folder_paths.get_input_directory())
path = path.replace("$output", folder_paths.get_output_directory())
path = path.replace("$temp", folder_paths.get_temp_directory())
return path
def is_child_dir(parent_path, child_path, j_mngr=None):
"""Verify child path is actually within parent path"""
if j_mngr is None:
j_mngr = json_manager()
parent_path = os.path.abspath(parent_path)
child_path = os.path.abspath(child_path)
is_child = os.path.commonpath([parent_path]) == os.path.commonpath([parent_path, child_path])
if not is_child:
msg = f"Security warning: Attempted access to path outside parent directory: {child_path}"
j_mngr.log_events(msg, TroubleSgltn.Severity.WARNING, is_trouble=True)
return is_child
def get_real_path(directory):
directory = directory.replace("/**/", "/")
directory = os.path.abspath(directory)
directory = os.path.split(directory)[0]
return directory
@PromptServer.instance.routes.get("/plush_for_comfy/text_file/{name}")
async def get_files(request):
j_mngr = json_manager()
name = request.match_info["name"]
try:
directory = get_dir_from_name(name, j_mngr)
recursive = "/**/" in directory
pre = get_real_path(directory)
files = list(map(lambda t: os.path.relpath(t, pre),
glob.glob(directory, recursive=recursive)))
if len(files) == 0:
j_mngr.log_events(f"No files found in directory: {directory}",
TroubleSgltn.Severity.INFO,
is_trouble=True)
files = ["[none]"]
return web.json_response(files)
except Exception as exc:
j_mngr.log_events(f"Error listing files: {str(exc)}",
TroubleSgltn.Severity.ERROR,
is_trouble=True)
raise
def get_file(root_dir, file, j_mngr=None):
"""Get and validate full file path"""
if j_mngr is None:
j_mngr = json_manager()
if file == "[none]" or not file or not file.strip():
msg = "No file selected"
j_mngr.log_events(msg, TroubleSgltn.Severity.WARNING, is_trouble=True)
raise ValueError(msg)
root_dir = get_dir_from_name(root_dir, j_mngr)
root_dir = get_real_path(root_dir)
# Use pathlib for directory creation
root_path = Path(root_dir)
if not root_path.exists():
j_mngr.log_events(f"Creating directory: {root_dir}")
root_path.mkdir(parents=True, exist_ok=True)
full_path = os.path.join(root_dir, file)
if not is_child_dir(root_dir, full_path, j_mngr):
msg = f"Security error: Attempted access to file outside root directory: {full_path}"
j_mngr.log_events(msg, TroubleSgltn.Severity.ERROR, is_trouble=True)
raise ReferenceError(msg)
return full_path
class TextFileNode:
"""Base class for text file operations"""
RETURN_TYPES = ("STRING", "STRING",) # (content, troubles)
RETURN_NAMES = ("saved_file", "troubleshooting")
CATEGORY = "Plush🧸/Utils"
file = None
@classmethod
def VALIDATE_INPUTS(cls, root_dir, file, **kwargs):
j_mngr = json_manager()
if file == "[none]" or not file or not file.strip():
return True
try:
get_file(root_dir, file, j_mngr)
return True
except Exception as exc:
j_mngr.log_events(f"Input validation error: {str(exc)}",
TroubleSgltn.Severity.ERROR,
is_trouble=True)
return False
def load_text(self, **kwargs):
j_mngr = json_manager()
j_mngr.trbl.reset("Text File Operations")
try:
self.file = get_file(kwargs["root_dir"], kwargs["file"], j_mngr)
content = j_mngr.read_file_contents(self.file, is_critical=False)
if content is None:
return ("", j_mngr.trbl.get_troubles())
j_mngr.log_events(f"Successfully read file: {self.file}", is_trouble=True)
return (content, j_mngr.trbl.get_troubles())
except Exception as exc:
j_mngr.log_events(f"Error reading file: {str(exc)}",
TroubleSgltn.Severity.ERROR,
is_trouble=True)
return ("", j_mngr.trbl.get_troubles())
class LoadText(TextFileNode):
@classmethod
def IS_CHANGED(cls, **kwargs):
if not cls.file:
return False
try:
return os.path.getmtime(cls.file)
except Exception:
return float("nan")
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"root_dir": (list(get_valid_dirs()), {}),
"file": (["[none]"], {
"plush.binding": [{
"source": "root_dir",
"callback": [{
"type": "set",
"target": "$this.disabled",
"value": True
}, {
"type": "fetch",
"url": "/plush_for_comfy/text_file/{$source.value}",
"then": [{
"type": "set",
"target": "$this.options.values",
"value": "$result"
}, {
"type": "validate-combo"
}, {
"type": "set",
"target": "$this.disabled",
"value": False
}]
}],
}]
})
},
}
FUNCTION = "load_text"
class SaveText(TextFileNode):
OUTPUT_NODE = True
@classmethod
def IS_CHANGED(cls, **kwargs):
return float("nan")
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"root_dir": (list(get_valid_dirs()), {}),
"file": ("STRING", {"default": "file.txt"}),
"append": (["append", "overwrite", "new only"], {}),
"insert": ("BOOLEAN", {
"default": True,
"label_on": "new line",
"label_off": "none",
"plush.binding": [{
"source": "append",
"callback": [{
"type": "if",
"condition": [{
"left": "$source.value",
"op": "eq",
"right": '"append"'
}],
"true": [{
"type": "set",
"target": "$this.disabled",
"value": False
}],
"false": [{
"type": "set",
"target": "$this.disabled",
"value": True
}],
}]
}]
}),
"text": ("STRING", {"forceInput": True, "multiline": True})
},
}
FUNCTION = "write_text"
def write_text(self, **kwargs):
j_mngr = json_manager()
j_mngr.trbl.reset("Save Text Operations")
try:
self.file = get_file(kwargs["root_dir"], kwargs["file"], j_mngr)
if kwargs["append"] == "new only" and os.path.exists(self.file):
msg = f"File {self.file} already exists and 'new only' is selected"
j_mngr.log_events(msg, TroubleSgltn.Severity.ERROR, is_trouble=True)
raise FileExistsError(msg)
text = kwargs["text"]
if kwargs["append"] and kwargs["insert"]:
text = f"\n{text}"
success = j_mngr.write_string_to_file(
text,
self.file,
is_critical=True,
append=(kwargs["append"] == "append")
)
if success:
j_mngr.log_events(f"Successfully wrote to file: {self.file}")
return super().load_text(**kwargs)
return ("", j_mngr.trbl.get_troubles())
except Exception as exc:
j_mngr.log_events(f"Error writing file: {str(exc)}",
TroubleSgltn.Severity.ERROR,
is_trouble=True)
return ("", j_mngr.trbl.get_troubles())
NODE_CLASS_MAPPINGS = {
"LoadText|plush": LoadText,
# "SaveText|plush": SaveText,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"LoadText|plush": "Load Saved Files 🧸",
# "SaveText|plush": "Save Files 🧸",
}