Skip to content

Commit cbc7907

Browse files
committed
improvements
1 parent 1f91e9c commit cbc7907

6 files changed

Lines changed: 99 additions & 21 deletions

File tree

src/main/java/dev/plex/request/SchematicUploadServlet.java

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,96 @@
11
package dev.plex.request;
22

33
import dev.plex.HTTPDModule;
4+
import dev.plex.Plex;
5+
import dev.plex.cache.DataUtils;
6+
import dev.plex.player.PlexPlayer;
7+
import dev.plex.rank.enums.Rank;
8+
import dev.plex.util.PlexLog;
49
import jakarta.servlet.ServletException;
510
import jakarta.servlet.http.HttpServlet;
611
import jakarta.servlet.http.HttpServletRequest;
712
import jakarta.servlet.http.HttpServletResponse;
813
import jakarta.servlet.http.Part;
9-
1014
import java.io.File;
1115
import java.io.IOException;
1216
import java.io.InputStream;
1317
import java.nio.file.Files;
1418
import java.nio.file.StandardCopyOption;
1519
import java.util.Arrays;
1620
import java.util.regex.Pattern;
21+
import org.bukkit.Bukkit;
22+
import org.bukkit.OfflinePlayer;
1723

1824
public class SchematicUploadServlet extends HttpServlet
1925
{
2026
private static final Pattern schemNameMatcher = Pattern.compile("^[a-z0-9'!,_ -]{1,30}\\.schem(atic)?$", Pattern.CASE_INSENSITIVE);
2127

2228
@Override
23-
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
29+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
30+
{
31+
if (request.getRemoteAddr() == null)
32+
{
33+
response.getWriter().println(schematicUploadBadHTML("Your IP address could not be detected. Please ensure you are using IPv4."));
34+
return;
35+
}
36+
PlexPlayer plexPlayer = DataUtils.getPlayerByIP(request.getRemoteAddr());
37+
if (plexPlayer == null)
38+
{
39+
response.getWriter().println(schematicUploadBadHTML("Couldn't load your IP Address: " + request.getRemoteAddr() + ". Have you joined the server before?"));
40+
return;
41+
}
42+
if (Plex.get().getSystem().equalsIgnoreCase("ranks"))
43+
{
44+
PlexLog.debug("Plex-HTTPD using ranks check");
45+
if (!plexPlayer.getRankFromString().isAtLeast(Rank.ADMIN))
46+
{
47+
response.getWriter().println(schematicUploadBadHTML("You must be an admin or above to upload schematics."));
48+
return;
49+
}
50+
}
51+
else if (Plex.get().getSystem().equalsIgnoreCase("permissions"))
52+
{
53+
PlexLog.debug("Plex-HTTPD using permissions check");
54+
final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(plexPlayer.getUuid());
55+
if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.schematics.upload"))
56+
{
57+
response.getWriter().println(schematicUploadBadHTML("You do not have permission to upload schematics."));
58+
return;
59+
}
60+
}
2461
File worldeditFolder = HTTPDModule.getWorldeditFolder();
2562
if (worldeditFolder == null)
2663
{
27-
resp.getWriter().println(schematicUploadBadHTML("Worldedit is not installed!"));
64+
response.getWriter().println(schematicUploadBadHTML("Worldedit is not installed!"));
2865
return;
2966
}
3067
File[] schematics = worldeditFolder.listFiles();
3168
Part uploadPart;
3269
try
3370
{
34-
uploadPart = req.getPart("file");
71+
uploadPart = request.getPart("file");
3572
}
3673
catch (IllegalStateException e)
3774
{
38-
resp.getWriter().println(schematicUploadBadHTML("That schematic is too large!"));
75+
response.getWriter().println(schematicUploadBadHTML("That schematic is too large!"));
3976
return;
4077
}
4178
String filename = uploadPart.getSubmittedFileName().replaceAll("[^a-zA-Z0-9'!,_ .-]", "_");
4279
if (!schemNameMatcher.matcher(filename).matches())
4380
{
44-
resp.getWriter().println(schematicUploadBadHTML("That is not a valid schematic filename!"));
81+
response.getWriter().println(schematicUploadBadHTML("That is not a valid schematic filename!"));
4582
return;
4683
}
4784
boolean alreadyExists = schematics != null && Arrays.stream(schematics).anyMatch(file -> HTTPDModule.fileNameEquals(file.getName(), filename));
4885
if (alreadyExists)
4986
{
50-
resp.getWriter().println(schematicUploadBadHTML("A schematic with the name <b>" + filename + "</b> already exists!"));
87+
response.getWriter().println(schematicUploadBadHTML("A schematic with the name <b>" + filename + "</b> already exists!"));
5188
return;
5289
}
5390
InputStream inputStream = uploadPart.getInputStream();
5491
Files.copy(inputStream, new File(worldeditFolder, filename).toPath(), StandardCopyOption.REPLACE_EXISTING);
5592
inputStream.close();
56-
resp.getWriter().println(schematicUploadGoodHTML("Successfully uploaded <b>" + filename + "."));
93+
response.getWriter().println(schematicUploadGoodHTML("Successfully uploaded <b>" + filename + "."));
5794
}
5895

5996
private String schematicUploadBadHTML(String message)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,57 @@
11
package dev.plex.request.impl;
22

3+
import dev.plex.HTTPDModule;
4+
import dev.plex.Plex;
5+
import dev.plex.cache.DataUtils;
6+
import dev.plex.player.PlexPlayer;
7+
import dev.plex.rank.enums.Rank;
38
import dev.plex.request.AbstractServlet;
49
import dev.plex.request.GetMapping;
10+
import dev.plex.util.PlexLog;
511
import jakarta.servlet.http.HttpServletRequest;
612
import jakarta.servlet.http.HttpServletResponse;
13+
import org.bukkit.Bukkit;
14+
import org.bukkit.OfflinePlayer;
715

816
public class SchematicUploadEndpoint extends AbstractServlet
917
{
1018
@GetMapping(endpoint = "/api/schematics/upload/")
1119
public String uploadSchematic(HttpServletRequest request, HttpServletResponse response)
1220
{
21+
String ipAddress = request.getRemoteAddr();
22+
if (ipAddress == null)
23+
{
24+
return schematicsHTML("An IP address could not be detected. Please ensure you are connecting using IPv4.");
25+
}
26+
final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress);
27+
if (player == null)
28+
{
29+
return schematicsHTML("Couldn't load your IP Address: " + ipAddress + ". Have you joined the server before?");
30+
}
31+
if (Plex.get().getSystem().equalsIgnoreCase("ranks"))
32+
{
33+
PlexLog.debug("Plex-HTTPD using ranks check");
34+
if (!player.getRankFromString().isAtLeast(Rank.ADMIN))
35+
{
36+
return schematicsHTML("You must be an admin or above to upload schematics.");
37+
}
38+
}
39+
else if (Plex.get().getSystem().equalsIgnoreCase("permissions"))
40+
{
41+
PlexLog.debug("Plex-HTTPD using permissions check");
42+
final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(player.getUuid());
43+
if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.schematics.upload"))
44+
{
45+
return schematicsHTML("You do not have permission to upload schematics.");
46+
}
47+
}
1348
return readFile(this.getClass().getResourceAsStream("/httpd/schematic_upload.html"));
1449
}
50+
51+
private String schematicsHTML(String message)
52+
{
53+
String file = readFile(this.getClass().getResourceAsStream("/httpd/schematic_upload_bad.html"));
54+
file = file.replace("${MESSAGE}", message);
55+
return file;
56+
}
1557
}

src/main/resources/httpd/punishments.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
PUNISHMENTS
33
<h2>Enter the UUID or username of the player you want to lookup</h2>
44
<div class="input-group mb-3 w-75 p-3">
5-
<input id="uuid" type="text" autocomplete="off" class="form-control" placeholder="UUID or username...">
5+
<input id="uuid" type="text" autocomplete="off" autofocus class="form-control">
66
<button class="btn btn-outline-primary" type="submit"
77
onclick="redirect();">Submit
88
</button>

src/main/resources/httpd/schematic_download.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<h2>Plex HTTPD</h2>
44
<h5>A list of schematics is below. You can click on the schematic name to download it.</h5>
55
<div class="input-group mb-3 w-75 p-3">
6-
<input type="text" autocomplete="off" class="form-control" oninput="filterTable(this.value)"
6+
<input type="text" autocomplete="off" autofocus class="form-control" oninput="filterTable(this.value)"
77
placeholder="Search for a schematic...">
88
</div>
99
<table id="schemList" class="table table-striped table-bordered">
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
Schematics
22
SCHEMATICS
33
<h2>Plex HTTPD</h2>
4-
<label for="formFile" class="form-label">Please select a schematic file to upload.</label>
5-
<div class="mb-3 p-3">
6-
<form class="input-group justify-content-center" enctype="multipart/form-data" method="post" action="/api/schematics/uploading">
7-
<label class="btn btn-primary">
8-
Browse <input type="file" name="file" id="formFile" hidden>
9-
</label>
10-
<input type="submit" class="btn btn-outline-primary" value="Upload">
4+
<div class="cos-xs-8 col-lg-5">
5+
<label for="formFile" class="form-label">Please select a schematic file to upload.</label>
6+
<form class="input-group justify-content-center" enctype="multipart/form-data" method="post"
7+
action="/api/schematics/uploading">
8+
<div class="input-group">
9+
<input type="file" class="form-control" id="formFile" name="file" aria-describedby="formFile" aria-label="Upload">
10+
<button class="btn btn-outline-primary" type="submit">Upload</button>
11+
</div>
1112
</form>
1213
</div>

src/main/resources/httpd/template.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
<html lang="en">
33
<head>
44
<meta name="viewport" content="width=device-width, initial-scale=1">
5-
<link rel="stylesheet"
6-
href="https://bootswatch.com/5/lumen/bootstrap.min.css"
7-
integrity="sha384-H04T9zWIvlvorcAbxsAHPAUgklS+NwdHqD+BVuHnYbizHH53HDHlMsVLRYqnAK49"
8-
crossorigin="anonymous">
5+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
6+
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
97
<script type="text/javascript"
108
src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
119
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"

0 commit comments

Comments
 (0)