Removed redundant information.

- Search path is deconstructed on the server to extract model type. Front and back end changed.
- Show directory even if empty. (Prevented moving a file to an empty directory.)
This commit is contained in:
Christian Bastian
2024-02-19 20:28:55 -05:00
parent 7b98d810ba
commit 48d5757d2b
2 changed files with 54 additions and 65 deletions

View File

@@ -70,28 +70,43 @@ def folder_paths_get_supported_pt_extensions(folder_name, refresh = False): # Mi
return model_extensions return model_extensions
def search_path_to_system_path(model_path, model_path_type): def search_path_to_system_path(model_path):
# TODO: return model type (since it is bakedi into the search path anyways; simplifies other code) sep = os.path.sep
model_path = model_path.replace("/", os.path.sep) model_path = os.path.normpath(model_path.replace("/", sep))
regex_result = re.search(r'\d+', model_path)
if regex_result is None: isep0 = 0 if model_path[0] == sep else -1
return None
try: isep1 = model_path.find(sep, isep0 + 1)
model_path_index = int(regex_result.group()) if isep1 == -1 or isep1 == len(model_path):
except: return (None, None)
return None
isep2 = model_path.find(sep, isep1 + 1)
if isep2 == -1 or isep2 - isep1 == 1:
isep2 = len(model_path)
model_path_type = model_path[isep0 + 1:isep1]
paths = folder_paths_get_folder_paths(model_path_type) paths = folder_paths_get_folder_paths(model_path_type)
if len(paths) == 0:
return (None, None)
model_path_index = model_path[isep1 + 1:isep2]
try:
model_path_index = int(model_path_index)
except:
return (None, None)
if model_path_index < 0 or model_path_index >= len(paths): if model_path_index < 0 or model_path_index >= len(paths):
return None return (None, None)
model_path_span = regex_result.span()
return os.path.join( system_path = os.path.normpath(
comfyui_model_uri, paths[model_path_index] +
( sep +
paths[model_path_index] + model_path[isep2:]
model_path[model_path_span[1]:]
)
) )
return (system_path, model_path_type)
print()
def get_safetensor_header(path): def get_safetensor_header(path):
try: try:
@@ -418,13 +433,7 @@ async def get_model_info(request):
return web.json_response({}) return web.json_response({})
model_path = urllib.parse.unquote(model_path) model_path = urllib.parse.unquote(model_path)
model_type = request.query.get("type") # TODO: in the searchPath? file, _ = search_path_to_system_path(model_path)
if model_type is None:
return web.json_response({})
model_type = urllib.parse.unquote(model_type)
model_path_type = model_type_to_dir_name(model_type)
file = search_path_to_system_path(model_path, model_path_type)
if file is None: if file is None:
return web.json_response({}) return web.json_response({})
@@ -499,13 +508,8 @@ async def download_model(request):
overwrite = body.get("overwrite", False) overwrite = body.get("overwrite", False)
model_type = body.get("type")
model_path_type = model_type_to_dir_name(model_type)
if model_path_type is None or model_path_type == "":
result["invalid"] = "type"
return web.json_response(result)
model_path = body.get("path", "/0") model_path = body.get("path", "/0")
directory = search_path_to_system_path(model_path, model_path_type) directory, model_type = search_path_to_system_path(model_path)
if directory is None: if directory is None:
result["invalid"] = "path" result["invalid"] = "path"
return web.json_response(result) return web.json_response(result)
@@ -556,14 +560,11 @@ async def download_model(request):
@server.PromptServer.instance.routes.post("/model-manager/model/move") @server.PromptServer.instance.routes.post("/model-manager/model/move")
async def move_model(request): async def move_model(request):
body = await request.json() body = await request.json()
model_type = body.get("type", None)
if model_type is None:
return web.json_response({ "success": False })
old_file = body.get("oldFile", None) old_file = body.get("oldFile", None)
if old_file is None: if old_file is None:
return web.json_response({ "success": False }) return web.json_response({ "success": False })
old_file = search_path_to_system_path(old_file, model_type) old_file, _ = search_path_to_system_path(old_file)
if not os.path.isfile(old_file): if not os.path.isfile(old_file):
return web.json_response({ "success": False }) return web.json_response({ "success": False })
_, filename = os.path.split(old_file) _, filename = os.path.split(old_file)
@@ -571,7 +572,7 @@ async def move_model(request):
new_path = body.get("newDirectory", None) new_path = body.get("newDirectory", None)
if new_path is None: if new_path is None:
return web.json_response({ "success": False }) return web.json_response({ "success": False })
new_path = search_path_to_system_path(new_path, model_type) new_path, _ = search_path_to_system_path(new_path)
if not os.path.isdir(new_path): if not os.path.isdir(new_path):
return web.json_response({ "success": False }) return web.json_response({ "success": False })
@@ -604,13 +605,7 @@ async def delete_model(request):
return web.json_response(result) return web.json_response(result)
model_path = urllib.parse.unquote(model_path) model_path = urllib.parse.unquote(model_path)
model_type = request.query.get("type") # TODO: in the searchPath? file, model_type = search_path_to_system_path(model_path)
if model_type is None:
return web.json_response(result)
model_type = urllib.parse.unquote(model_type)
model_path_type = model_type_to_dir_name(model_type)
file = search_path_to_system_path(model_path, model_path_type)
if file is None: if file is None:
return web.json_response(result) return web.json_response(result)

View File

@@ -640,7 +640,7 @@ class DirectoryDropdown {
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
const child = items[i]; const child = items[i];
const grandChildCount = child["childCount"]; const grandChildCount = child["childCount"];
const isDir = grandChildCount !== undefined && grandChildCount !== null && grandChildCount > 0; const isDir = grandChildCount !== undefined && grandChildCount !== null;
const itemName = child["name"]; const itemName = child["name"];
if (itemName.startsWith(lastWord) && (!showDirectoriesOnly || (showDirectoriesOnly && isDir))) { if (itemName.startsWith(lastWord) && (!showDirectoriesOnly || (showDirectoriesOnly && isDir))) {
options.push(itemName + (isDir ? searchSeparator : "")); options.push(itemName + (isDir ? searchSeparator : ""));
@@ -1156,7 +1156,7 @@ class ModelGrid {
$el("button.icon-button.model-button", { $el("button.icon-button.model-button", {
type: "button", type: "button",
textContent: "ⓘ", textContent: "ⓘ",
onclick: async() => modelInfoCallback(modelType, searchPath), onclick: async() => modelInfoCallback(searchPath),
draggable: false, draggable: false,
}), }),
]), ]),
@@ -1366,9 +1366,8 @@ class ModelManager extends ComfyDialog {
if (confirmation === affirmation) { if (confirmation === affirmation) {
const container = this.#el.modelInfoContainer; const container = this.#el.modelInfoContainer;
const path = encodeURIComponent(container.dataset.path); const path = encodeURIComponent(container.dataset.path);
const type = encodeURIComponent(this.#el.modelTypeSelect.value);
await request( await request(
`/model-manager/model/delete?path=${path}&type=${type}`, `/model-manager/model/delete?path=${path}`,
{ {
method: "POST", method: "POST",
} }
@@ -1402,18 +1401,14 @@ class ModelManager extends ComfyDialog {
textContent: "Move", textContent: "Move",
onclick: async(e) => { onclick: async(e) => {
const container = this.#el.modelInfoContainer; const container = this.#el.modelInfoContainer;
const path = container.dataset.path;
const type = this.#el.modelTypeSelect.value;
const destination = moveDestination.value;
let moved = false; let moved = false;
await request( await request(
`/model-manager/model/move`, `/model-manager/model/move`,
{ {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
"type": type, "oldFile": container.dataset.path,
"oldFile": path, "newDirectory": moveDestination.value,
"newDirectory": destination,
}), }),
} }
) )
@@ -1608,13 +1603,11 @@ class ModelManager extends ComfyDialog {
} }
/** /**
* @param {string} modelType
* @param {string} searchPath * @param {string} searchPath
*/ */
#modelTab_showModelInfo = async(modelType, searchPath) => { #modelTab_showModelInfo = async(searchPath) => {
const type = encodeURIComponent(modelType);
const path = encodeURIComponent(searchPath); const path = encodeURIComponent(searchPath);
const info = await request(`/model-manager/model/info?path=${path}&type=${type}`) const info = await request(`/model-manager/model/info?path=${path}`)
.catch(err => { .catch(err => {
console.log(err); console.log(err);
return null; return null;
@@ -1936,8 +1929,8 @@ class ModelManager extends ComfyDialog {
$el("input.search-text-area", { $el("input.search-text-area", {
$: (el) => (els.saveDirectoryPath = el), $: (el) => (els.saveDirectoryPath = el),
type: "text", type: "text",
placeholder: "/0", placeholder: this.#searchSeparator + "0",
value: "/0", value: this.#searchSeparator + "0",
}); });
$el("select.model-select-dropdown", { $el("select.model-select-dropdown", {
@@ -2075,10 +2068,11 @@ class ModelManager extends ComfyDialog {
onclick: async (e) => { onclick: async (e) => {
const record = {}; const record = {};
record["download"] = info["downloadUrl"]; record["download"] = info["downloadUrl"];
record["type"] = els.modelTypeSelect.value; record["path"] = (
if (record["type"] === "") { return; } // TODO: notify user in app els.modelTypeSelect.value +
record["path"] = els.saveDirectoryPath.value; this.#searchSeparator + // NOTE: this may add multiple separators (server should handle carefully)
if (record["path"] === "/") { return; } // TODO: notify user in app els.saveDirectoryPath.value
);
record["name"] = (() => { record["name"] = (() => {
const filename = info["fileName"]; const filename = info["fileName"];
const name = els.filename.value; const name = els.filename.value;