fix(routes): correct handling of embed files

Enhanced logic for handling file embeds to ensure proper processing of
both downloadable and embedded files. Fixed issues related to condition
checks and image embedding. This improves reliability and correctness
when rendering different types of content attachments.

Fixes issue with inconsistent file embed handling.
This commit is contained in:
Kumi 2024-07-29 17:06:24 +02:00
parent 20c03bb4e0
commit e845caff0e
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -158,49 +158,54 @@ def init_main_routes(app):
for file in step["files"]: for file in step["files"]:
if file["image"] and "embedType" not in "file": if file["image"] and "embedType" not in "file":
step_imgs.append( step_imgs.append(
{"src": proxy(file["downloadUrl"], file["name"]), "alt": file["name"]} {
"src": proxy(file["downloadUrl"], file["name"]),
"alt": file["name"],
}
) )
elif not file["image"]: elif not file["image"]:
step_downloads.append( if "downloadUrl" in file.keys():
{ step_downloads.append(
"src": proxy(file["downloadUrl"], file["name"]), {
"name": file["name"], "src": proxy(file["downloadUrl"], file["name"]),
} "name": file["name"],
) }
else: # Leaves us with embeds
embed_code = file["embedHtmlCode"]
soup = BeautifulSoup(embed_code, "html.parser")
iframe = soup.select("iframe")[0]
src = iframe.get("src")
if src.startswith("https://content.instructables.com"):
src = src.replace(
"https://content.instructables.com",
f"/proxy/?url={src}",
) )
elif app.config["INVIDIOUS"] and src.startswith( else: # Leaves us with embeds
"https://www.youtube.com" embed_code = file["embedHtmlCode"]
): soup = BeautifulSoup(embed_code, "html.parser")
src = src.replace(
"https://www.youtube.com", app.config["INVIDIOUS"] iframe = soup.select("iframe")[0]
src = iframe.get("src")
if src.startswith("https://content.instructables.com"):
src = src.replace(
"https://content.instructables.com",
f"/proxy/?url={src}",
)
elif app.config["INVIDIOUS"] and src.startswith(
"https://www.youtube.com"
):
src = src.replace(
"https://www.youtube.com",
app.config["INVIDIOUS"],
)
elif not app.config["UNSAFE"]:
src = "/iframe/?url=" + quote(src)
step_iframes.append(
{
"src": src,
"width": file.get("width"),
"height": file.get("height"),
}
) )
elif not app.config["UNSAFE"]:
src = "/iframe/?url=" + quote(src)
step_iframes.append(
{
"src": src,
"width": file.get("width"),
"height": file.get("height"),
}
)
step_text = step["body"] step_text = step["body"]
step_text = step_text.replace( step_text = step_text.replace(
"https://content.instructables.com", "https://content.instructables.com",
@ -360,7 +365,7 @@ def init_main_routes(app):
if path.endswith(".md"): if path.endswith(".md"):
content = Markdown().convert(content) content = Markdown().convert(content)
except OSError: except OSError:
pass pass