From e845caff0e3b560eb0bdff0d849d22a558249695 Mon Sep 17 00:00:00 2001 From: Kumi Date: Mon, 29 Jul 2024 17:06:24 +0200 Subject: [PATCH] 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. --- src/structables/routes/main.py | 79 ++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/src/structables/routes/main.py b/src/structables/routes/main.py index d6441d9..a7e3ad5 100644 --- a/src/structables/routes/main.py +++ b/src/structables/routes/main.py @@ -158,49 +158,54 @@ def init_main_routes(app): for file in step["files"]: if file["image"] and "embedType" not in "file": 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"]: - step_downloads.append( - { - "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}", + if "downloadUrl" in file.keys(): + step_downloads.append( + { + "src": proxy(file["downloadUrl"], file["name"]), + "name": file["name"], + } ) - elif app.config["INVIDIOUS"] and src.startswith( - "https://www.youtube.com" - ): - src = src.replace( - "https://www.youtube.com", app.config["INVIDIOUS"] + 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( + "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_text.replace( "https://content.instructables.com", @@ -360,7 +365,7 @@ def init_main_routes(app): if path.endswith(".md"): content = Markdown().convert(content) - + except OSError: pass