diff --git a/javascript/_utils.js b/javascript/_utils.js index b9894cf..5600a1a 100644 --- a/javascript/_utils.js +++ b/javascript/_utils.js @@ -84,10 +84,18 @@ async function fetchAPI(url, json = true, cache = false) { // Extra network preview thumbnails async function getExtraNetworkPreviewURL(filename, type) { const previewJSON = await fetchAPI(`tacapi/v1/thumb-preview/${filename}?type=${type}`, true, true); - if (previewJSON?.url) - return `file=${previewJSON.url}`; - else + if (previewJSON?.url) { + const properURL = `sd_extra_networks/thumb?filename=${previewJSON.url}`; + if ((await fetch(properURL)).status == 200) { + return properURL; + } else { + // create blob url + const blob = await (await fetch(`tacapi/v1/thumb-preview-blob/${filename}?type=${type}`)).blob(); + return URL.createObjectURL(blob); + } + } else { return null; + } } // Debounce function to prevent spamming the autocomplete function diff --git a/scripts/tag_autocomplete_helper.py b/scripts/tag_autocomplete_helper.py index 3928bdd..2d71dde 100644 --- a/scripts/tag_autocomplete_helper.py +++ b/scripts/tag_autocomplete_helper.py @@ -459,15 +459,18 @@ async def get_json_info(base_path: Path, filename: str = None): except Exception as e: return json.dumps({"error": e}) - async def get_preview_thumbnail(base_path: Path, filename: str = None): + async def get_preview_thumbnail(base_path: Path, filename: str = None, blob: bool = False): if base_path is None or (not base_path.exists()): return json.dumps({}) try: img_glob = glob.glob(base_path.as_posix() + f"/**/{filename}.*", recursive=True) - img_candidates = [img for img in img_glob if Path(img).suffix in [".png", ".jpg", ".jpeg", ".webp"]] + img_candidates = [img for img in img_glob if Path(img).suffix in [".png", ".jpg", ".jpeg", ".webp", ".gif"]] if img_candidates is not None and len(img_candidates) > 0: - return JSONResponse({"url": urllib.parse.quote(img_candidates[0])}) + if blob: + return FileResponse(img_candidates[0]) + else: + return JSONResponse({"url": urllib.parse.quote(img_candidates[0])}) except Exception as e: return json.dumps({"error": e}) @@ -479,18 +482,27 @@ async def get_lora_info(lora_name): async def get_lyco_info(lyco_name): return await get_json_info(LYCO_PATH, lyco_name) - @app.get("/tacapi/v1/thumb-preview/{filename}") - async def get_thumb_preview(filename, type): + def get_path_for_type(type): if type == "lora": - return await get_preview_thumbnail(LORA_PATH, filename) + return LORA_PATH elif type == "lyco": - return await get_preview_thumbnail(LYCO_PATH, filename) + return LYCO_PATH elif type == "hyper": - return await get_preview_thumbnail(HYP_PATH, filename) + return HYP_PATH elif type == "embed": - return await get_preview_thumbnail(EMB_PATH, filename) + return EMB_PATH else: - return "Invalid type" + return None + + @app.get("/tacapi/v1/thumb-preview/{filename}") + async def get_thumb_preview(filename, type): + return await get_preview_thumbnail(get_path_for_type(type), filename, False) + + @app.get("/tacapi/v1/thumb-preview-blob/{filename}") + async def get_thumb_preview_blob(filename, type): + return await get_preview_thumbnail(get_path_for_type(type), filename, True) + + script_callbacks.on_app_started(api_tac) \ No newline at end of file