Refactor server creation for big bucket

This commit is contained in:
--unset 2024-05-04 15:55:52 -04:00
parent 7967962e3a
commit ef39564c0a
4 changed files with 43 additions and 65 deletions

View File

@ -186,7 +186,7 @@ class BigBucket:
logger.info("Automatic cache refresh initiated due to old cache.") logger.info("Automatic cache refresh initiated due to old cache.")
self._refresh_cache() self._refresh_cache()
def get_fetch_url(self, jar, server, version): def get_fetch_url(self, server, version):
""" """
Constructs the URL for downloading a server JAR file based on the server type. Constructs the URL for downloading a server JAR file based on the server type.
@ -203,10 +203,13 @@ class BigBucket:
str or None: URL for downloading the JAR file, or None if URL cannot be str or None: URL for downloading the JAR file, or None if URL cannot be
constructed or an error occurs. constructed or an error occurs.
""" """
print(self._read_cache()["types"][server]["versions"][version]["url"][0])
try: try:
# Check if the server type is not specifically handled by Paper. # Check if the server type is not specifically handled by Paper.
if server not in PAPERJARS: if server not in PAPERJARS:
return f"{self.base_url}/api/fetchJar/{jar}/{server}/{version}" return self._read_cache()["types"][server]["versions"][version]["url"][
0
]
# For Paper servers, attempt to get the build for the specified version. # For Paper servers, attempt to get the build for the specified version.
paper_build_info = self.get_paper_build(server, version) paper_build_info = self.get_paper_build(server, version)
@ -236,16 +239,16 @@ class BigBucket:
logger.error(f"An error occurred while constructing fetch URL: {e}") logger.error(f"An error occurred while constructing fetch URL: {e}")
return None return None
def download_jar(self, jar, server, version, path, server_id): def download_jar(self, server, version, path, server_id):
update_thread = threading.Thread( update_thread = threading.Thread(
name=f"server_download-{server_id}-{server}-{version}", name=f"server_download-{server_id}-{server}-{version}",
target=self.a_download_jar, target=self.a_download_jar,
daemon=True, daemon=True,
args=(jar, server, version, path, server_id), args=(server, version, path, server_id),
) )
update_thread.start() update_thread.start()
def a_download_jar(self, jar, server, version, path, server_id): def a_download_jar(self, server, version, path, server_id):
""" """
Downloads a server JAR file and performs post-download actions including Downloads a server JAR file and performs post-download actions including
notifying users and setting import status. notifying users and setting import status.
@ -276,7 +279,7 @@ class BigBucket:
# delaying download for server register to finish # delaying download for server register to finish
time.sleep(3) time.sleep(3)
fetch_url = self.get_fetch_url(jar, server, version) fetch_url = self.get_fetch_url(server, version)
if not fetch_url: if not fetch_url:
return False return False

View File

@ -570,19 +570,17 @@ class Controller:
if root_create_data["create_type"] == "download_jar": if root_create_data["create_type"] == "download_jar":
# modded update urls from server jars will only update the installer # modded update urls from server jars will only update the installer
if ( if (
create_data["category"] != "modded" create_data["type"] != "forge-installer"
and create_data["type"] not in BigBucket.get_paper_jars() and create_data["type"] not in BigBucket.get_paper_jars()
): ):
server_obj = self.servers.get_server_obj(new_server_id) server_obj = self.servers.get_server_obj(new_server_id)
url = ( url = self.big_bucket.get_fetch_url(
"https://api.serverjars.com/api/fetchJar/" create_data["type"], create_data["version"]
f"{create_data['category']}"
f"/{create_data['type']}/{create_data['version']}"
) )
print(url)
server_obj.executable_update_url = url server_obj.executable_update_url = url
self.servers.update_server(server_obj) self.servers.update_server(server_obj)
self.big_bucket.download_jar( self.big_bucket.download_jar(
create_data["category"],
create_data["type"], create_data["type"],
create_data["version"], create_data["version"],
full_jar_path, full_jar_path,

View File

@ -134,13 +134,7 @@ new_server_schema = {
"mem_min", "mem_min",
"mem_max", "mem_max",
"server_properties_port", "server_properties_port",
"category",
], ],
"category": {
"title": "Jar Category",
"type": "string",
"examples": ["modded", "vanilla"],
},
"properties": { "properties": {
"type": { "type": {
"title": "Server JAR Type", "title": "Server JAR Type",

View File

@ -67,14 +67,14 @@
{% end %} {% end %}
{% raw xsrf_form_html() %} {% raw xsrf_form_html() %}
<div class="row"> <div class="row">
<div class="col-sm-12"> <div class="col-sm-12">
<div class="form-group"> <div class="form-group">
<label for="server_jar">{{ translate('serverWizard', 'serverType', data['lang']) <label for="server_type">{{ translate('serverWizard', 'serverSelect', data['lang']) }}</label>
}}</label>
<div class="input-group"> <div class="input-group">
<select required class="form-control form-control-lg select-css" id="server_jar" <select required class="form-control form-control-lg select-css" id="server_type" name="type"
name="server_jar" onchange="serverJarChange(this)"> onchange="serverTypeChange(this)">
<option value="None">{{ translate('serverWizard', 'selectType', data['lang']) }}</option> <option value="">{{ translate('serverWizard', 'selectServer', data['lang']) }}</option>
{% for s in data['server_types'] %} {% for s in data['server_types'] %}
<option value="{{ s }}">{{ s.capitalize() }}</option> <option value="{{ s }}">{{ s.capitalize() }}</option>
{% end %} {% end %}
@ -89,16 +89,6 @@
</div> </div>
</div> </div>
<div class="col-sm-12">
<div class="form-group">
<label for="server_type">{{ translate('serverWizard', 'serverSelect', data['lang']) }}</label>
<select required class="form-control form-control-lg select-css" id="server_type" name="type"
onchange="serverTypeChange(this)">
<option value="">{{ translate('serverWizard', 'selectServer', data['lang']) }}</option>
</select>
</div>
</div>
<div class="col-sm-12"> <div class="col-sm-12">
<div class="form-group"> <div class="form-group">
<label for="server_version">{{ translate('serverWizard', 'serverVersion', data['lang']) }}</label> <label for="server_version">{{ translate('serverWizard', 'serverVersion', data['lang']) }}</label>
@ -108,7 +98,8 @@
</div> </div>
</div> </div>
<span data-html="true" class="version-hint text-center" <span data-html="true" class="version-hint text-center"
data-content="⚠️ {{ translate('serverWizard', 'unsupported', data['lang']) }} ⚠️" , data-placement="right"></span> data-content="⚠️ {{ translate('serverWizard', 'unsupported', data['lang']) }} ⚠️" ,
data-placement="right"></span>
<div class="col-sm-12"> <div class="col-sm-12">
<div class="form-group"> <div class="form-group">
<label for="server_name">{{ translate('serverWizard', 'serverName', data['lang']) }}</label> <label for="server_name">{{ translate('serverWizard', 'serverName', data['lang']) }}</label>
@ -192,7 +183,8 @@
<div class="api-alert" style="position: absolute; top: -5px; z-index: 100; opacity: .99;"> <div class="api-alert" style="position: absolute; top: -5px; z-index: 100; opacity: .99;">
<p style="color: white !important;"><i class="fas fa-exclamation-triangle" style="color: red;"></i>&nbsp;{{ <p style="color: white !important;"><i class="fas fa-exclamation-triangle" style="color: red;"></i>&nbsp;{{
translate('error', 'serverJars1', data['lang']) }}<a style="color: red;" ; translate('error', 'serverJars1', data['lang']) }}<a style="color: red;" ;
href="https://status.craftycontrol.com/status/craftycontrol" target="_blank" rel="noopener">&nbsp;{{ translate('error', href="https://status.craftycontrol.com/status/craftycontrol" target="_blank" rel="noopener">&nbsp;{{
translate('error',
'craftyStatus', data['lang']) }}</a> 'craftyStatus', data['lang']) }}</a>
&nbsp;{{ translate('error', 'serverJars2', data['lang']) }}</p> &nbsp;{{ translate('error', 'serverJars2', data['lang']) }}</p>
</div> </div>
@ -827,7 +819,7 @@
message: '<p class="text-center mb-0"><i class="fa fa-spin fa-cog"></i> Please wait while we gather your files...</p>', message: '<p class="text-center mb-0"><i class="fa fa-spin fa-cog"></i> Please wait while we gather your files...</p>',
closeButton: false closeButton: false
}); });
setTimeout(function(){ setTimeout(function () {
getDirView(); getDirView();
}, 2000); }, 2000);
} else { } else {
@ -845,9 +837,9 @@
message: '<p class="text-center mb-0"><i class="fa fa-spin fa-cog"></i> Please wait while we gather your files...</p>', message: '<p class="text-center mb-0"><i class="fa fa-spin fa-cog"></i> Please wait while we gather your files...</p>',
closeButton: false closeButton: false
}); });
setTimeout(function(){ setTimeout(function () {
getDirView(); getDirView();
}, 2000); }, 2000);
}); });
var upload = false; var upload = false;
var file; var file;
@ -970,9 +962,8 @@
"minecraft_java_create_data": { "minecraft_java_create_data": {
"create_type": "download_jar", "create_type": "download_jar",
"download_jar_create_data": { "download_jar_create_data": {
"category": formDataObject.version.split("|")[0], "type": formDataObject.version.split("|")[0],
"type": formDataObject.version.split("|")[1], "version": formDataObject.version.split("|")[1],
"version": formDataObject.version.split("|")[2],
"mem_min": formDataObject.mem_min, "mem_min": formDataObject.mem_min,
"mem_max": formDataObject.mem_max, "mem_max": formDataObject.mem_max,
"server_properties_port": formDataObject.port "server_properties_port": formDataObject.port
@ -1197,34 +1188,25 @@
*/ */
function serverTypeChange(selectObj) { function serverTypeChange(selectObj) {
// get the index of the selected option // get the index of the selected option
let idx = document.getElementById('server_type').selectedIndex; let idx = document.getElementById('server_type').value;
// get the value of the selected option // get the value of the selected option
let cSelect = document.getElementById("server"); let cSelect = document.getElementById("server");
let which = {}; let which = {};
try { try {
which = document.getElementById('server_type').options[idx].value; versions = Object.keys(serverTypesLists[idx]["versions"]);
} catch { } catch {
while (cSelect.options.length > 0) { while (cSelect.options.length > 0) {
cSelect.remove(0); cSelect.remove(0);
} }
return; return;
} }
let server_type = which.split('|')[0];
let server = which.split('|')[1];
// use the selected option value to retrieve the list of items from the serverTypesLists array
let cList = serverTypesLists[server_type];
// get the country select element via its known id
cSelect = document.getElementById("server");
// remove the current options from the country select
while (cSelect.options.length > 0) {
cSelect.remove(0);
}
let newOption;
// create new options ordered by ascending // create new options ordered by ascending
cList[server].forEach(type => { versions.forEach(type => {
newOption = document.createElement("option"); newOption = document.createElement("option");
newOption.value = which + "|" + type; // assumes option string and value are the same newOption.value = idx + "|" + type; // assumes option string and value are the same
newOption.text = type; newOption.text = type;
console.log(type)
// add the new option // add the new option
try { try {
cSelect.add(newOption); // this will fail in DOM browsers but is needed for IE cSelect.add(newOption); // this will fail in DOM browsers but is needed for IE
@ -1234,17 +1216,18 @@
} }
}) })
} }
$("#server").change(function (){
$("#server").change(function () {
let selected_version = $("#server :selected").text().split("."); let selected_version = $("#server :selected").text().split(".");
if(parseInt(selected_version[0]) === 1 && parseInt(selected_version[1]) < 8 ){ if (parseInt(selected_version[0]) === 1 && parseInt(selected_version[1]) < 8) {
$('[data-toggle="popover"]').popover(); $('[data-toggle="popover"]').popover();
if ($(window).width() < 1000) { if ($(window).width() < 1000) {
$('.version-hint').attr("data-placement", "top") $('.version-hint').attr("data-placement", "top")
} else {
$('.version-hint').attr("data-placement", "right")
}
$('.version-hint').popover("show");
} else { } else {
$('.version-hint').attr("data-placement", "right")
}
$('.version-hint').popover("show");
}else{
$('.version-hint').popover("hide"); $('.version-hint').popover("hide");
} }
}); });