improvements

This commit is contained in:
Telesphoreo 2022-04-18 01:36:45 -05:00
parent 1f91e9c684
commit cbc7907aea
6 changed files with 99 additions and 21 deletions

View File

@ -1,12 +1,16 @@
package dev.plex.request; package dev.plex.request;
import dev.plex.HTTPDModule; import dev.plex.HTTPDModule;
import dev.plex.Plex;
import dev.plex.cache.DataUtils;
import dev.plex.player.PlexPlayer;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexLog;
import jakarta.servlet.ServletException; import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.Part; import jakarta.servlet.http.Part;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -14,46 +18,79 @@ import java.nio.file.Files;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.Arrays; import java.util.Arrays;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
public class SchematicUploadServlet extends HttpServlet public class SchematicUploadServlet extends HttpServlet
{ {
private static final Pattern schemNameMatcher = Pattern.compile("^[a-z0-9'!,_ -]{1,30}\\.schem(atic)?$", Pattern.CASE_INSENSITIVE); private static final Pattern schemNameMatcher = Pattern.compile("^[a-z0-9'!,_ -]{1,30}\\.schem(atic)?$", Pattern.CASE_INSENSITIVE);
@Override @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
if (request.getRemoteAddr() == null)
{
response.getWriter().println(schematicUploadBadHTML("Your IP address could not be detected. Please ensure you are using IPv4."));
return;
}
PlexPlayer plexPlayer = DataUtils.getPlayerByIP(request.getRemoteAddr());
if (plexPlayer == null)
{
response.getWriter().println(schematicUploadBadHTML("Couldn't load your IP Address: " + request.getRemoteAddr() + ". Have you joined the server before?"));
return;
}
if (Plex.get().getSystem().equalsIgnoreCase("ranks"))
{
PlexLog.debug("Plex-HTTPD using ranks check");
if (!plexPlayer.getRankFromString().isAtLeast(Rank.ADMIN))
{
response.getWriter().println(schematicUploadBadHTML("You must be an admin or above to upload schematics."));
return;
}
}
else if (Plex.get().getSystem().equalsIgnoreCase("permissions"))
{
PlexLog.debug("Plex-HTTPD using permissions check");
final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(plexPlayer.getUuid());
if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.schematics.upload"))
{
response.getWriter().println(schematicUploadBadHTML("You do not have permission to upload schematics."));
return;
}
}
File worldeditFolder = HTTPDModule.getWorldeditFolder(); File worldeditFolder = HTTPDModule.getWorldeditFolder();
if (worldeditFolder == null) if (worldeditFolder == null)
{ {
resp.getWriter().println(schematicUploadBadHTML("Worldedit is not installed!")); response.getWriter().println(schematicUploadBadHTML("Worldedit is not installed!"));
return; return;
} }
File[] schematics = worldeditFolder.listFiles(); File[] schematics = worldeditFolder.listFiles();
Part uploadPart; Part uploadPart;
try try
{ {
uploadPart = req.getPart("file"); uploadPart = request.getPart("file");
} }
catch (IllegalStateException e) catch (IllegalStateException e)
{ {
resp.getWriter().println(schematicUploadBadHTML("That schematic is too large!")); response.getWriter().println(schematicUploadBadHTML("That schematic is too large!"));
return; return;
} }
String filename = uploadPart.getSubmittedFileName().replaceAll("[^a-zA-Z0-9'!,_ .-]", "_"); String filename = uploadPart.getSubmittedFileName().replaceAll("[^a-zA-Z0-9'!,_ .-]", "_");
if (!schemNameMatcher.matcher(filename).matches()) if (!schemNameMatcher.matcher(filename).matches())
{ {
resp.getWriter().println(schematicUploadBadHTML("That is not a valid schematic filename!")); response.getWriter().println(schematicUploadBadHTML("That is not a valid schematic filename!"));
return; return;
} }
boolean alreadyExists = schematics != null && Arrays.stream(schematics).anyMatch(file -> HTTPDModule.fileNameEquals(file.getName(), filename)); boolean alreadyExists = schematics != null && Arrays.stream(schematics).anyMatch(file -> HTTPDModule.fileNameEquals(file.getName(), filename));
if (alreadyExists) if (alreadyExists)
{ {
resp.getWriter().println(schematicUploadBadHTML("A schematic with the name <b>" + filename + "</b> already exists!")); response.getWriter().println(schematicUploadBadHTML("A schematic with the name <b>" + filename + "</b> already exists!"));
return; return;
} }
InputStream inputStream = uploadPart.getInputStream(); InputStream inputStream = uploadPart.getInputStream();
Files.copy(inputStream, new File(worldeditFolder, filename).toPath(), StandardCopyOption.REPLACE_EXISTING); Files.copy(inputStream, new File(worldeditFolder, filename).toPath(), StandardCopyOption.REPLACE_EXISTING);
inputStream.close(); inputStream.close();
resp.getWriter().println(schematicUploadGoodHTML("Successfully uploaded <b>" + filename + ".")); response.getWriter().println(schematicUploadGoodHTML("Successfully uploaded <b>" + filename + "."));
} }
private String schematicUploadBadHTML(String message) private String schematicUploadBadHTML(String message)

View File

@ -1,15 +1,57 @@
package dev.plex.request.impl; package dev.plex.request.impl;
import dev.plex.HTTPDModule;
import dev.plex.Plex;
import dev.plex.cache.DataUtils;
import dev.plex.player.PlexPlayer;
import dev.plex.rank.enums.Rank;
import dev.plex.request.AbstractServlet; import dev.plex.request.AbstractServlet;
import dev.plex.request.GetMapping; import dev.plex.request.GetMapping;
import dev.plex.util.PlexLog;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
public class SchematicUploadEndpoint extends AbstractServlet public class SchematicUploadEndpoint extends AbstractServlet
{ {
@GetMapping(endpoint = "/api/schematics/upload/") @GetMapping(endpoint = "/api/schematics/upload/")
public String uploadSchematic(HttpServletRequest request, HttpServletResponse response) public String uploadSchematic(HttpServletRequest request, HttpServletResponse response)
{ {
String ipAddress = request.getRemoteAddr();
if (ipAddress == null)
{
return schematicsHTML("An IP address could not be detected. Please ensure you are connecting using IPv4.");
}
final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress);
if (player == null)
{
return schematicsHTML("Couldn't load your IP Address: " + ipAddress + ". Have you joined the server before?");
}
if (Plex.get().getSystem().equalsIgnoreCase("ranks"))
{
PlexLog.debug("Plex-HTTPD using ranks check");
if (!player.getRankFromString().isAtLeast(Rank.ADMIN))
{
return schematicsHTML("You must be an admin or above to upload schematics.");
}
}
else if (Plex.get().getSystem().equalsIgnoreCase("permissions"))
{
PlexLog.debug("Plex-HTTPD using permissions check");
final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(player.getUuid());
if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.schematics.upload"))
{
return schematicsHTML("You do not have permission to upload schematics.");
}
}
return readFile(this.getClass().getResourceAsStream("/httpd/schematic_upload.html")); return readFile(this.getClass().getResourceAsStream("/httpd/schematic_upload.html"));
} }
private String schematicsHTML(String message)
{
String file = readFile(this.getClass().getResourceAsStream("/httpd/schematic_upload_bad.html"));
file = file.replace("${MESSAGE}", message);
return file;
}
} }

View File

@ -2,7 +2,7 @@ Punishments
PUNISHMENTS PUNISHMENTS
<h2>Enter the UUID or username of the player you want to lookup</h2> <h2>Enter the UUID or username of the player you want to lookup</h2>
<div class="input-group mb-3 w-75 p-3"> <div class="input-group mb-3 w-75 p-3">
<input id="uuid" type="text" autocomplete="off" class="form-control" placeholder="UUID or username..."> <input id="uuid" type="text" autocomplete="off" autofocus class="form-control">
<button class="btn btn-outline-primary" type="submit" <button class="btn btn-outline-primary" type="submit"
onclick="redirect();">Submit onclick="redirect();">Submit
</button> </button>

View File

@ -3,7 +3,7 @@ SCHEMATICS
<h2>Plex HTTPD</h2> <h2>Plex HTTPD</h2>
<h5>A list of schematics is below. You can click on the schematic name to download it.</h5> <h5>A list of schematics is below. You can click on the schematic name to download it.</h5>
<div class="input-group mb-3 w-75 p-3"> <div class="input-group mb-3 w-75 p-3">
<input type="text" autocomplete="off" class="form-control" oninput="filterTable(this.value)" <input type="text" autocomplete="off" autofocus class="form-control" oninput="filterTable(this.value)"
placeholder="Search for a schematic..."> placeholder="Search for a schematic...">
</div> </div>
<table id="schemList" class="table table-striped table-bordered"> <table id="schemList" class="table table-striped table-bordered">

View File

@ -1,12 +1,13 @@
Schematics Schematics
SCHEMATICS SCHEMATICS
<h2>Plex HTTPD</h2> <h2>Plex HTTPD</h2>
<div class="cos-xs-8 col-lg-5">
<label for="formFile" class="form-label">Please select a schematic file to upload.</label> <label for="formFile" class="form-label">Please select a schematic file to upload.</label>
<div class="mb-3 p-3"> <form class="input-group justify-content-center" enctype="multipart/form-data" method="post"
<form class="input-group justify-content-center" enctype="multipart/form-data" method="post" action="/api/schematics/uploading"> action="/api/schematics/uploading">
<label class="btn btn-primary"> <div class="input-group">
Browse <input type="file" name="file" id="formFile" hidden> <input type="file" class="form-control" id="formFile" name="file" aria-describedby="formFile" aria-label="Upload">
</label> <button class="btn btn-outline-primary" type="submit">Upload</button>
<input type="submit" class="btn btn-outline-primary" value="Upload"> </div>
</form> </form>
</div> </div>

View File

@ -2,10 +2,8 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
href="https://bootswatch.com/5/lumen/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
integrity="sha384-H04T9zWIvlvorcAbxsAHPAUgklS+NwdHqD+BVuHnYbizHH53HDHlMsVLRYqnAK49"
crossorigin="anonymous">
<script type="text/javascript" <script type="text/javascript"
src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"