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.")
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.
@ -203,10 +203,13 @@ class BigBucket:
str or None: URL for downloading the JAR file, or None if URL cannot be
constructed or an error occurs.
"""
print(self._read_cache()["types"][server]["versions"][version]["url"][0])
try:
# Check if the server type is not specifically handled by Paper.
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.
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}")
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(
name=f"server_download-{server_id}-{server}-{version}",
target=self.a_download_jar,
daemon=True,
args=(jar, server, version, path, server_id),
args=(server, version, path, server_id),
)
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
notifying users and setting import status.
@ -276,7 +279,7 @@ class BigBucket:
# delaying download for server register to finish
time.sleep(3)
fetch_url = self.get_fetch_url(jar, server, version)
fetch_url = self.get_fetch_url(server, version)
if not fetch_url:
return False

View File

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

View File

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

View File

@ -67,14 +67,14 @@
{% end %}
{% raw xsrf_form_html() %}
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<label for="server_jar">{{ translate('serverWizard', 'serverType', data['lang'])
}}</label>
<label for="server_type">{{ translate('serverWizard', 'serverSelect', data['lang']) }}</label>
<div class="input-group">
<select required class="form-control form-control-lg select-css" id="server_jar"
name="server_jar" onchange="serverJarChange(this)">
<option value="None">{{ translate('serverWizard', 'selectType', data['lang']) }}</option>
<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>
{% for s in data['server_types'] %}
<option value="{{ s }}">{{ s.capitalize() }}</option>
{% end %}
@ -89,16 +89,6 @@
</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="form-group">
<label for="server_version">{{ translate('serverWizard', 'serverVersion', data['lang']) }}</label>
@ -108,7 +98,8 @@
</div>
</div>
<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="form-group">
<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;">
<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;" ;
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>
&nbsp;{{ translate('error', 'serverJars2', data['lang']) }}</p>
</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>',
closeButton: false
});
setTimeout(function(){
setTimeout(function () {
getDirView();
}, 2000);
} 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>',
closeButton: false
});
setTimeout(function(){
getDirView();
}, 2000);
setTimeout(function () {
getDirView();
}, 2000);
});
var upload = false;
var file;
@ -970,9 +962,8 @@
"minecraft_java_create_data": {
"create_type": "download_jar",
"download_jar_create_data": {
"category": formDataObject.version.split("|")[0],
"type": formDataObject.version.split("|")[1],
"version": formDataObject.version.split("|")[2],
"type": formDataObject.version.split("|")[0],
"version": formDataObject.version.split("|")[1],
"mem_min": formDataObject.mem_min,
"mem_max": formDataObject.mem_max,
"server_properties_port": formDataObject.port
@ -1197,34 +1188,25 @@
*/
function serverTypeChange(selectObj) {
// 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
let cSelect = document.getElementById("server");
let which = {};
try {
which = document.getElementById('server_type').options[idx].value;
versions = Object.keys(serverTypesLists[idx]["versions"]);
} catch {
while (cSelect.options.length > 0) {
cSelect.remove(0);
}
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
cList[server].forEach(type => {
versions.forEach(type => {
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;
console.log(type)
// add the new option
try {
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(".");
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();
if ($(window).width() < 1000) {
$('.version-hint').attr("data-placement", "top")
if ($(window).width() < 1000) {
$('.version-hint').attr("data-placement", "top")
} else {
$('.version-hint').attr("data-placement", "right")
}
$('.version-hint').popover("show");
} else {
$('.version-hint').attr("data-placement", "right")
}
$('.version-hint').popover("show");
}else{
$('.version-hint').popover("hide");
}
});