From 48d5757d2b95ae8965666c46357bd75d19435bde Mon Sep 17 00:00:00 2001 From: Christian Bastian Date: Mon, 19 Feb 2024 20:28:55 -0500 Subject: [PATCH] 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.) --- __init__.py | 85 +++++++++++++++++++++----------------------- web/model-manager.js | 34 ++++++++---------- 2 files changed, 54 insertions(+), 65 deletions(-) diff --git a/__init__.py b/__init__.py index 416ee96..16a49b1 100644 --- a/__init__.py +++ b/__init__.py @@ -70,28 +70,43 @@ def folder_paths_get_supported_pt_extensions(folder_name, refresh = False): # Mi return model_extensions -def search_path_to_system_path(model_path, model_path_type): - # TODO: return model type (since it is bakedi into the search path anyways; simplifies other code) - model_path = model_path.replace("/", os.path.sep) - regex_result = re.search(r'\d+', model_path) - if regex_result is None: - return None - try: - model_path_index = int(regex_result.group()) - except: - return None +def search_path_to_system_path(model_path): + sep = os.path.sep + model_path = os.path.normpath(model_path.replace("/", sep)) + + isep0 = 0 if model_path[0] == sep else -1 + + isep1 = model_path.find(sep, isep0 + 1) + if isep1 == -1 or isep1 == len(model_path): + return (None, 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) + 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): - return None - model_path_span = regex_result.span() - return os.path.join( - comfyui_model_uri, - ( - paths[model_path_index] + - model_path[model_path_span[1]:] - ) + return (None, None) + + system_path = os.path.normpath( + paths[model_path_index] + + sep + + model_path[isep2:] ) + return (system_path, model_path_type) + +print() + def get_safetensor_header(path): try: @@ -418,13 +433,7 @@ async def get_model_info(request): return web.json_response({}) model_path = urllib.parse.unquote(model_path) - model_type = request.query.get("type") # TODO: in the searchPath? - 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) + file, _ = search_path_to_system_path(model_path) if file is None: return web.json_response({}) @@ -496,16 +505,11 @@ async def download_model(request): "success": False, "invalid": None, } - + 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") - 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: result["invalid"] = "path" return web.json_response(result) @@ -556,14 +560,11 @@ async def download_model(request): @server.PromptServer.instance.routes.post("/model-manager/model/move") async def move_model(request): 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) if old_file is None: 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): return web.json_response({ "success": False }) _, filename = os.path.split(old_file) @@ -571,7 +572,7 @@ async def move_model(request): new_path = body.get("newDirectory", None) if new_path is None: 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): return web.json_response({ "success": False }) @@ -604,13 +605,7 @@ async def delete_model(request): return web.json_response(result) model_path = urllib.parse.unquote(model_path) - model_type = request.query.get("type") # TODO: in the searchPath? - 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) + file, model_type = search_path_to_system_path(model_path) if file is None: return web.json_response(result) diff --git a/web/model-manager.js b/web/model-manager.js index 4d3ad3a..41ad3c9 100644 --- a/web/model-manager.js +++ b/web/model-manager.js @@ -640,7 +640,7 @@ class DirectoryDropdown { for (let i = 0; i < items.length; i++) { const child = items[i]; const grandChildCount = child["childCount"]; - const isDir = grandChildCount !== undefined && grandChildCount !== null && grandChildCount > 0; + const isDir = grandChildCount !== undefined && grandChildCount !== null; const itemName = child["name"]; if (itemName.startsWith(lastWord) && (!showDirectoriesOnly || (showDirectoriesOnly && isDir))) { options.push(itemName + (isDir ? searchSeparator : "")); @@ -1156,7 +1156,7 @@ class ModelGrid { $el("button.icon-button.model-button", { type: "button", textContent: "ⓘ", - onclick: async() => modelInfoCallback(modelType, searchPath), + onclick: async() => modelInfoCallback(searchPath), draggable: false, }), ]), @@ -1366,9 +1366,8 @@ class ModelManager extends ComfyDialog { if (confirmation === affirmation) { const container = this.#el.modelInfoContainer; const path = encodeURIComponent(container.dataset.path); - const type = encodeURIComponent(this.#el.modelTypeSelect.value); await request( - `/model-manager/model/delete?path=${path}&type=${type}`, + `/model-manager/model/delete?path=${path}`, { method: "POST", } @@ -1402,18 +1401,14 @@ class ModelManager extends ComfyDialog { textContent: "Move", onclick: async(e) => { const container = this.#el.modelInfoContainer; - const path = container.dataset.path; - const type = this.#el.modelTypeSelect.value; - const destination = moveDestination.value; let moved = false; await request( `/model-manager/model/move`, { method: "POST", body: JSON.stringify({ - "type": type, - "oldFile": path, - "newDirectory": destination, + "oldFile": container.dataset.path, + "newDirectory": moveDestination.value, }), } ) @@ -1608,13 +1603,11 @@ class ModelManager extends ComfyDialog { } /** - * @param {string} modelType * @param {string} searchPath */ - #modelTab_showModelInfo = async(modelType, searchPath) => { - const type = encodeURIComponent(modelType); + #modelTab_showModelInfo = async(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 => { console.log(err); return null; @@ -1936,8 +1929,8 @@ class ModelManager extends ComfyDialog { $el("input.search-text-area", { $: (el) => (els.saveDirectoryPath = el), type: "text", - placeholder: "/0", - value: "/0", + placeholder: this.#searchSeparator + "0", + value: this.#searchSeparator + "0", }); $el("select.model-select-dropdown", { @@ -2075,10 +2068,11 @@ class ModelManager extends ComfyDialog { onclick: async (e) => { const record = {}; record["download"] = info["downloadUrl"]; - record["type"] = els.modelTypeSelect.value; - if (record["type"] === "") { return; } // TODO: notify user in app - record["path"] = els.saveDirectoryPath.value; - if (record["path"] === "/") { return; } // TODO: notify user in app + record["path"] = ( + els.modelTypeSelect.value + + this.#searchSeparator + // NOTE: this may add multiple separators (server should handle carefully) + els.saveDirectoryPath.value + ); record["name"] = (() => { const filename = info["fileName"]; const name = els.filename.value;