From 153781c78e9048348972a12e9843592b3800d30d Mon Sep 17 00:00:00 2001 From: amcmanu3 Date: Mon, 4 Sep 2023 12:12:02 -0400 Subject: [PATCH] Add cache refresh to api v2 --- app/classes/web/ajax_handler.py | 8 ----- app/classes/web/routes/api/api_handlers.py | 6 ++++ .../web/routes/api/crafty/exe_cache.py | 27 ++++++++++++++++ app/frontend/templates/server/wizard.html | 31 ++++++++++++------- 4 files changed, 52 insertions(+), 20 deletions(-) create mode 100644 app/classes/web/routes/api/crafty/exe_cache.py diff --git a/app/classes/web/ajax_handler.py b/app/classes/web/ajax_handler.py index aa54505c..8cd81b50 100644 --- a/app/classes/web/ajax_handler.py +++ b/app/classes/web/ajax_handler.py @@ -128,14 +128,6 @@ class AjaxHandler(BaseHandler): self.controller.cached_login = "login_1.jpg" return - elif page == "jar_cache": - if not superuser: - self.redirect("/panel/error?error=Not a super user") - return - - self.controller.server_jars.manual_refresh_cache() - return - elif page == "update_server_dir": if self.helper.dir_migration: return diff --git a/app/classes/web/routes/api/api_handlers.py b/app/classes/web/routes/api/api_handlers.py index 4f34cfbc..7294b931 100644 --- a/app/classes/web/routes/api/api_handlers.py +++ b/app/classes/web/routes/api/api_handlers.py @@ -55,6 +55,7 @@ from app.classes.web.routes.api.users.user.public import ApiUsersUserPublicHandl from app.classes.web.routes.api.crafty.config.index import ApiCraftyConfigIndexHandler from app.classes.web.routes.api.crafty.clogs.index import ApiCraftyLogIndexHandler from app.classes.web.routes.api.crafty.imports.index import ApiImportFilesIndexHandler +from app.classes.web.routes.api.crafty.exe_cache import ApiCraftyExeCacheIndexHandler def api_handlers(handler_args): @@ -147,6 +148,11 @@ def api_handlers(handler_args): ApiServersIndexHandler, handler_args, ), + ( + r"/api/v2/crafty/exeCache/?", + ApiCraftyExeCacheIndexHandler, + handler_args, + ), ( r"/api/v2/servers/([0-9]+)/?", ApiServersServerIndexHandler, diff --git a/app/classes/web/routes/api/crafty/exe_cache.py b/app/classes/web/routes/api/crafty/exe_cache.py new file mode 100644 index 00000000..a779195d --- /dev/null +++ b/app/classes/web/routes/api/crafty/exe_cache.py @@ -0,0 +1,27 @@ +from app.classes.web.base_api_handler import BaseApiHandler + + +class ApiCraftyExeCacheIndexHandler(BaseApiHandler): + def get(self): + auth_data = self.authenticate_user() + if not auth_data: + return + ( + _, + _, + _, + _, + _, + ) = auth_data + + if not auth_data[4]["superuser"]: + return self.finish_json(400, {"status": "error", "error": "NOT_AUTHORIZED"}) + + self.controller.server_jars.manual_refresh_cache() + self.finish_json( + 200, + { + "status": "ok", + "data": self.controller.server_jars.get_serverjar_data(), + }, + ) diff --git a/app/frontend/templates/server/wizard.html b/app/frontend/templates/server/wizard.html index 3acecd45..83205160 100644 --- a/app/frontend/templates/server/wizard.html +++ b/app/frontend/templates/server/wizard.html @@ -1104,24 +1104,31 @@ } } - function refreshCache() { - let token = getCookie("_xsrf") + async function refreshCache() { document.getElementById("refresh-cache").classList.add("fa-spin") - $.ajax({ - type: "POST", - headers: { 'X-XSRFToken': token }, - url: '/ajax/jar_cache', - success: function () { + let token = getCookie("_xsrf") + let res = await fetch(`/api/v2/crafty/exeCache`, { + method: 'GET', + headers: { + 'X-XSRFToken': token + }, + }); + let responseData = await res.json(); + if (responseData.status === "ok") { document.getElementById("refresh-cache").classList.remove("fa-sync"); document.getElementById("refresh-cache").classList.remove("fa-spin"); document.getElementById("refresh-cache").classList.add("fa-check"); - - setTimeout(function () { + setTimeout(() => { location.reload(); }, 2000); - }, - }); - } + } else { + + bootbox.alert({ + title: responseData.error, + message: responseData.error + }); + } + } $('#file').change(function () { console.log("File changed");