From 3812d675fcf16909416aa39589ac80b7ef9e1014 Mon Sep 17 00:00:00 2001 From: Kumi Date: Sun, 26 May 2024 08:50:22 +0200 Subject: [PATCH] feat: enhance file download handling Improved the file download process by appending filenames to the proxy URLs, facilitating proper naming of downloaded files. Updated the proxy function to accept an optional filename parameter, which is then used to set the 'Content-Disposition' header for file downloads. This ensures that files downloaded through the proxy are saved with their original names, improving user experience and file management. Additionally, modified the main route to pass file names to the proxy URLs for both image and non-image files, aligning with the enhanced proxy functionality. This change addresses user feedback regarding difficulties in identifying and organizing downloaded files, by ensuring files retain their names post-download, thus making it easier for users to recognize and manage their downloads effectively. --- src/structables/routes/main.py | 4 ++-- src/structables/routes/proxy.py | 14 ++++++++++++-- src/structables/utils/helpers.py | 4 ++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/structables/routes/main.py b/src/structables/routes/main.py index 1160062..2a8dda4 100644 --- a/src/structables/routes/main.py +++ b/src/structables/routes/main.py @@ -157,13 +157,13 @@ 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"]), "alt": file["name"]} + {"src": proxy(file["downloadUrl"], file["name"]), "alt": file["name"]} ) elif not file["image"]: step_downloads.append( { - "src": proxy(file["downloadUrl"]), + "src": proxy(file["downloadUrl"], file["name"]), "name": file["name"], } ) diff --git a/src/structables/routes/proxy.py b/src/structables/routes/proxy.py index 5471573..92087c9 100644 --- a/src/structables/routes/proxy.py +++ b/src/structables/routes/proxy.py @@ -4,10 +4,13 @@ from urllib.parse import unquote from urllib.error import HTTPError from urllib.request import urlopen + def init_proxy_routes(app): @app.route("/proxy/") def route_proxy(): url = request.args.get("url") + filename = request.args.get("filename") + if url is not None: if url.startswith("https://cdn.instructables.com/") or url.startswith( "https://content.instructables.com/" @@ -34,7 +37,14 @@ def init_proxy_routes(app): except KeyError: raise InternalServerError() - return Response(generate(), content_type=content_type) + headers = dict() + + if filename is not None: + headers["Content-Disposition"] = ( + f'attachment; filename="{filename}"' + ) + + return Response(generate(), content_type=content_type, headers=headers) else: raise BadRequest() else: @@ -47,4 +57,4 @@ def init_proxy_routes(app): if url is not None: return render_template("iframe.html", url=url) else: - raise BadRequest() \ No newline at end of file + raise BadRequest() diff --git a/src/structables/utils/helpers.py b/src/structables/utils/helpers.py index a6399de..6aa0759 100644 --- a/src/structables/utils/helpers.py +++ b/src/structables/utils/helpers.py @@ -9,9 +9,9 @@ from flask import request, render_template, abort logging.basicConfig(level=logging.DEBUG) -def proxy(url): +def proxy(url, filename=None): logging.debug(f"Generating proxy URL for {url}") - return f"/proxy/?url={url}" + return f"/proxy/?url={url}" + (f"&filename={filename}" if filename else "") def get_typesense_api_key():