Skip to content

Commit

Permalink
Fix API endpoint to work with symlinks / external folders
Browse files Browse the repository at this point in the history
Fixes #217
  • Loading branch information
DominikDoom committed Aug 7, 2023
1 parent 16bc6d8 commit f63bbf9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
14 changes: 11 additions & 3 deletions javascript/_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 22 additions & 10 deletions scripts/tag_autocomplete_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand All @@ -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)

0 comments on commit f63bbf9

Please sign in to comment.