|
1 | 1 | package dev.plex.request; |
2 | 2 |
|
3 | 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; |
| 8 | +import dev.plex.util.PlexLog; |
4 | 9 | import jakarta.servlet.ServletException; |
5 | 10 | import jakarta.servlet.http.HttpServlet; |
6 | 11 | import jakarta.servlet.http.HttpServletRequest; |
7 | 12 | import jakarta.servlet.http.HttpServletResponse; |
8 | 13 | import jakarta.servlet.http.Part; |
9 | | - |
10 | 14 | import java.io.File; |
11 | 15 | import java.io.IOException; |
12 | 16 | import java.io.InputStream; |
13 | 17 | import java.nio.file.Files; |
14 | 18 | import java.nio.file.StandardCopyOption; |
15 | 19 | import java.util.Arrays; |
16 | 20 | import java.util.regex.Pattern; |
| 21 | +import org.bukkit.Bukkit; |
| 22 | +import org.bukkit.OfflinePlayer; |
17 | 23 |
|
18 | 24 | public class SchematicUploadServlet extends HttpServlet |
19 | 25 | { |
20 | 26 | private static final Pattern schemNameMatcher = Pattern.compile("^[a-z0-9'!,_ -]{1,30}\\.schem(atic)?$", Pattern.CASE_INSENSITIVE); |
21 | 27 |
|
22 | 28 | @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 | + } |
24 | 61 | File worldeditFolder = HTTPDModule.getWorldeditFolder(); |
25 | 62 | if (worldeditFolder == null) |
26 | 63 | { |
27 | | - resp.getWriter().println(schematicUploadBadHTML("Worldedit is not installed!")); |
| 64 | + response.getWriter().println(schematicUploadBadHTML("Worldedit is not installed!")); |
28 | 65 | return; |
29 | 66 | } |
30 | 67 | File[] schematics = worldeditFolder.listFiles(); |
31 | 68 | Part uploadPart; |
32 | 69 | try |
33 | 70 | { |
34 | | - uploadPart = req.getPart("file"); |
| 71 | + uploadPart = request.getPart("file"); |
35 | 72 | } |
36 | 73 | catch (IllegalStateException e) |
37 | 74 | { |
38 | | - resp.getWriter().println(schematicUploadBadHTML("That schematic is too large!")); |
| 75 | + response.getWriter().println(schematicUploadBadHTML("That schematic is too large!")); |
39 | 76 | return; |
40 | 77 | } |
41 | 78 | String filename = uploadPart.getSubmittedFileName().replaceAll("[^a-zA-Z0-9'!,_ .-]", "_"); |
42 | 79 | if (!schemNameMatcher.matcher(filename).matches()) |
43 | 80 | { |
44 | | - resp.getWriter().println(schematicUploadBadHTML("That is not a valid schematic filename!")); |
| 81 | + response.getWriter().println(schematicUploadBadHTML("That is not a valid schematic filename!")); |
45 | 82 | return; |
46 | 83 | } |
47 | 84 | boolean alreadyExists = schematics != null && Arrays.stream(schematics).anyMatch(file -> HTTPDModule.fileNameEquals(file.getName(), filename)); |
48 | 85 | if (alreadyExists) |
49 | 86 | { |
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!")); |
51 | 88 | return; |
52 | 89 | } |
53 | 90 | InputStream inputStream = uploadPart.getInputStream(); |
54 | 91 | Files.copy(inputStream, new File(worldeditFolder, filename).toPath(), StandardCopyOption.REPLACE_EXISTING); |
55 | 92 | inputStream.close(); |
56 | | - resp.getWriter().println(schematicUploadGoodHTML("Successfully uploaded <b>" + filename + ".")); |
| 93 | + response.getWriter().println(schematicUploadGoodHTML("Successfully uploaded <b>" + filename + ".")); |
57 | 94 | } |
58 | 95 |
|
59 | 96 | private String schematicUploadBadHTML(String message) |
|
0 commit comments