Fix web UI cache download logic

This commit is contained in:
Théophile Diot 2024-03-19 14:35:11 +00:00
parent 5e5a900d4f
commit 27d1c5f21c
No known key found for this signature in database
GPG key ID: 248FEA4BAE400D06
4 changed files with 60 additions and 29 deletions

View file

@ -52,6 +52,7 @@ from src.User import AnonymousUser, User
from utils import check_settings, get_b64encoded_qr_image, path_to_dict, get_remain
from Database import Database # type: ignore
from logger import setup_logger # type: ignore
from logging import getLogger
@ -87,9 +88,14 @@ app.config["SECRET_KEY"] = getenv("FLASK_SECRET", urandom(32))
PROXY_NUMBERS = int(getenv("PROXY_NUMBERS", "1"))
app.wsgi_app = ReverseProxied(app.wsgi_app, x_for=PROXY_NUMBERS, x_proto=PROXY_NUMBERS, x_host=PROXY_NUMBERS, x_prefix=PROXY_NUMBERS)
app.logger = setup_logger("UI")
gunicorn_access_logger = getLogger("gunicorn.access")
gunicorn_access_logger.setLevel(app.logger.level)
gunicorn_logger = getLogger("gunicorn.error")
app.logger = gunicorn_logger
app.logger.setLevel(gunicorn_logger.level)
gunicorn_logger.setLevel(app.logger.level)
werkzeug_logger = getLogger("werkzeug")
werkzeug_logger.setLevel(app.logger.level)
login_manager = LoginManager()
login_manager.init_app(app)
@ -2052,34 +2058,20 @@ def jobs():
@app.route("/jobs/download", methods=["GET"])
@login_required
def jobs_download():
plugin_id = request.args.get("plugin_id", "")
job_name = request.args.get("job_name", None)
file_name = request.args.get("file_name", None)
service_id = request.args.get("service_id", "")
if not job_name or not file_name:
return (
jsonify(
{
"status": "ko",
"message": "job_name and file_name are required",
}
),
422,
)
if not plugin_id or not job_name or not file_name:
return jsonify({"status": "ko", "message": "plugin_id, job_name and file_name are required"}), 422
cache_file = db.get_job_cache_file(job_name, file_name)
cache_file = db.get_job_cache_file(job_name, file_name, service_id=service_id, plugin_id=plugin_id)
if not cache_file:
return (
jsonify(
{
"status": "ko",
"message": "file not found",
}
),
404,
)
return jsonify({"status": "ko", "message": "file not found"}), 404
file = BytesIO(cache_file.data)
file = BytesIO(cache_file)
return send_file(file, as_attachment=True, download_name=file_name)

View file

@ -20,21 +20,59 @@ class Download {
.closest("button")
.hasAttribute(`data-${this.prefix}-download`)
) {
const dataValue = e.target
.closest('div:has(span[data-cache-content=""])')
.firstElementChild.getAttribute("data-value");
const btnEl = e.target.closest("button");
const jobName = btnEl.getAttribute("data-cache-download");
const fileName = btnEl.getAttribute("data-cache-file");
this.sendFileToDL(jobName, fileName);
const jobName = btnEl.getAttribute(`data-${this.prefix}-job`);
const fileName = btnEl.getAttribute(`data-${this.prefix}-download`);
const pluginId = document
.querySelector('[data-level="1"]')
.getAttribute("data-name");
var serviceId = null;
if (
document.querySelector(
'[data-level="2"][data-cache-breadcrumb-item=""]:not(.hidden)',
)
) {
serviceId = document
.querySelector('[data-level="2"]')
.getAttribute("data-name");
}
if (dataValue !== "Download file to view content") {
this.download(fileName, dataValue);
} else {
this.sendFileToDL(pluginId, jobName, fileName, serviceId);
}
}
} catch (err) {}
});
}
async sendFileToDL(jobName, fileName) {
download(filename, text) {
var element = document.createElement("a");
element.setAttribute(
"href",
"data:text/plain;charset=utf-8," + encodeURIComponent(text),
);
element.setAttribute("download", filename);
element.style.display = "none";
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
async sendFileToDL(pluginId, jobName, fileName, serviceId) {
window.open(
`${location.href.replace(
"cache",
"jobs",
)}/download?job_name=${jobName}&file_name=${fileName}`,
)}/download?plugin_id=${pluginId}&job_name=${jobName}&file_name=${fileName}` +
(serviceId ? `&service_id=${serviceId}` : ""),
);
}
}

View file

@ -161,7 +161,7 @@
<!-- download button -->
{% if child['type'] == "file" and child['can_download'] == True %}
{% if current_endpoint == "cache" %}
<button role="tab" value="download" data-{{ current_endpoint }}-download="{{ child['name'].split("/")[0] }}" data-{{ current_endpoint }}-file="{{ child['name'].split("/")[1] }}" data-{{ current_endpoint }}-setting-select-dropdown-btn="{{ child['name'].split("/")[0] }}" class="duration-300 border-gray-300 hover:brightness-90 bg-white text-white my-0 relative px-6 py-2 text-center align-middle transition-all rounded-none cursor-pointer leading-normal text-sm ease-in tracking-tight-rem dark:border-slate-600 dark:bg-slate-700 dark:text-gray-300 w-full border-b border-l border-r hover:bg-gray-100">
<button role="tab" value="download" data-{{ current_endpoint }}-download="{{ child['name'] }}" data-{{ current_endpoint }}-job="{{ child['job_name'] }}" class="duration-300 border-gray-300 hover:brightness-90 bg-white text-white my-0 relative px-6 py-2 text-center align-middle transition-all rounded-none cursor-pointer leading-normal text-sm ease-in tracking-tight-rem dark:border-slate-600 dark:bg-slate-700 dark:text-gray-300 w-full border-b border-l border-r hover:bg-gray-100">
<span class="flex justify-start items-center">
<svg class="h-5.5 w-5.5 stroke-sky-500"
xmlns="http://www.w3.org/2000/svg"

View file

@ -159,6 +159,7 @@ def path_to_dict(
file_info = {
"name": conf["file_name"],
"job_name": conf["job_name"],
"type": "file",
"path": join(
path,