reupload polish

This commit is contained in:
Jill 2023-01-05 20:08:30 +03:00
parent ede1e7a217
commit 94c8cdf463
5 changed files with 40 additions and 5 deletions

View File

@ -29,6 +29,10 @@ date = "relative"
# allow new accounts to be created
allow_registration = true
[reuploads]
# allow reuploading levels from other servers
allowed = true
[chests]
enabled = true

View File

@ -17,9 +17,13 @@
<div>Uploaded successfully! Level ID: <b><%= level_id %></b></div><br />
<%- end -%>
<%- if disabled -%>
<b>Reuploading levels has been disabled</b><br><br>
<%- end -%>
<form action="/tools/reupload" method="post">
ID: <input type="text" id="level_id" name="level_id" />
<input type="submit" value="Submit" />
ID: <input type="text" id="level_id" name="level_id" disabled="<%= disabled ? "1" : "" %>" />
<input type="submit" value="Submit" disabled="<%= disabled ? "1" : "" %>" />
</form>
<br>
<i>Currently only vanilla servers are supported</i>

View File

@ -21,6 +21,7 @@ require "./lib/ids"
require "./lib/level"
require "./lib/dailies"
require "./lib/templates"
require "./lib/reupload"
if File.exists?(".env")
Dotenv.load
@ -261,6 +262,8 @@ module CrystalGauntlet
LOG.warn { " #{" " * min_length}#{"^" * (max_length - min_length)}"}
end
Reupload.init()
@@up_at = Time.utc
LOG.notice { "Listening on #{listen_on.to_s.colorize(:white)}" }
server.listen

23
src/lib/reupload.cr Normal file
View File

@ -0,0 +1,23 @@
module CrystalGauntlet::Reupload
extend self
REUPLOAD_ACC_USERNAME = "Reupload"
@@reupload_acc_id = 0
def init()
begin
@@reupload_acc_id = DATABASE.query_one("select id from accounts where username = ?", REUPLOAD_ACC_USERNAME, as: {Int32})
LOG.debug { "reupload acc id #{@@reupload_acc_id}" }
rescue
next_id = IDs.get_next_id("accounts")
DATABASE.exec("insert into accounts (id, username, password, gjp2, email, is_admin, messages_enabled, friend_requests_enabled, comments_enabled) values (?, ?, ?, ?, ?, ?, ?, ?, ?)", next_id, REUPLOAD_ACC_USERNAME, "!", "!", "", 1, 0, 0, 0)
LOG.debug { "created reupload acc id #{next_id}" }
@@reupload_acc_id = next_id
end
end
def reupload_acc_id()
@@reupload_acc_id
end
end

View File

@ -6,10 +6,12 @@ include CrystalGauntlet
CrystalGauntlet.template_endpoints["/tools/reupload"] = ->(context : HTTP::Server::Context) {
context.response.content_type = "text/html"
disabled = !(config_get("reuploads.allowed").as?(Bool))
error = nil
level_id = nil
body = context.request.body
if body
if body && !disabled
begin
params = URI::Params.parse(body.gets_to_end)
remote_level_id = params["level_id"]
@ -31,8 +33,7 @@ CrystalGauntlet.template_endpoints["/tools/reupload"] = ->(context : HTTP::Serve
# todo: deduplicate this and level uploads
next_id = IDs.get_next_id("levels")
# todo: reupload as reupload acc
DATABASE.exec("insert into levels (id, name, user_id, description, original, game_version, binary_version, password, requested_stars, unlisted, version, extra_data, level_info, editor_time, editor_time_copies, song_id, length, objects, coins, has_ldm, two_player) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", next_id, Clean.clean_special(level_data["k2"]), 1, Base64.decode_string(Base64.decode_string(level_data["k3"])), nil, level_data["k17"].to_i32, (level_data["k50"]? || "0").to_i32, level_data["k41"]? ? level_data["k41"].to_i32 : nil, level_data["k66"].to_i32, 0, level_data["k16"].to_i32, Clean.clean_special(level["extra_string"].as_s? || Level::DEFAULT_EXTRA_STRING), Level::DEFAULT_LEVEL_INFO, (level_data["k80"]? || "0").to_i32, (level_data["k81"]? || "0").to_i32, (level_data["k8"]? || "0") == "0" ? level_data["k45"] : level_data["k8"], level["length"].as_i, level_data["k48"], (level_data["k64"]? || "0").to_i, (level_data["k72"]? || "0").to_i, (level_data["k43"]? || "0").to_i)
DATABASE.exec("insert into levels (id, name, user_id, description, original, game_version, binary_version, password, requested_stars, unlisted, version, extra_data, level_info, editor_time, editor_time_copies, song_id, length, objects, coins, has_ldm, two_player) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", next_id, Clean.clean_special(level_data["k2"]), Reupload.reupload_acc_id, Base64.decode_string(Base64.decode_string(level_data["k3"])), nil, level_data["k17"].to_i32, (level_data["k50"]? || "0").to_i32, level_data["k41"]? ? level_data["k41"].to_i32 : nil, level_data["k66"].to_i32, 0, level_data["k16"].to_i32, Clean.clean_special(level["extra_string"].as_s? || Level::DEFAULT_EXTRA_STRING), Level::DEFAULT_LEVEL_INFO, (level_data["k80"]? || "0").to_i32, (level_data["k81"]? || "0").to_i32, (level_data["k8"]? || "0") == "0" ? level_data["k45"] : level_data["k8"], level["length"].as_i, level_data["k48"], (level_data["k64"]? || "0").to_i, (level_data["k72"]? || "0").to_i, (level_data["k43"]? || "0").to_i)
File.write(DATA_FOLDER / "levels" / "#{next_id.to_s}.lvl", Base64.decode(level_data["k4"]))