Add cache refresh to api v2

This commit is contained in:
amcmanu3 2023-09-04 12:12:02 -04:00
parent db20d5ea7f
commit 153781c78e
4 changed files with 52 additions and 20 deletions

View File

@ -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

View File

@ -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,

View File

@ -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(),
},
)

View File

@ -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");