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.
This commit is contained in:
parent
ef1566d20b
commit
3812d675fc
3 changed files with 16 additions and 6 deletions
|
@ -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"],
|
||||
}
|
||||
)
|
||||
|
|
|
@ -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()
|
||||
raise BadRequest()
|
||||
|
|
|
@ -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():
|
||||
|
|
Loading…
Reference in a new issue