Module-HTTPD/src/main/java/dev/plex/request/impl/SchematicDownloadEndpoint.java

109 lines
3.5 KiB
Java
Raw Normal View History

2022-04-17 22:27:30 +00:00
package dev.plex.request.impl;
2022-04-17 23:21:44 +00:00
import dev.plex.HTTPDModule;
2022-04-17 22:27:30 +00:00
import dev.plex.request.AbstractServlet;
import dev.plex.request.GetMapping;
import jakarta.servlet.http.HttpServletRequest;
2022-04-17 23:26:01 +00:00
import jakarta.servlet.http.HttpServletResponse;
2022-04-17 22:27:30 +00:00
import java.io.File;
2022-04-17 23:21:44 +00:00
import java.io.IOException;
import java.io.OutputStream;
2022-04-17 22:27:30 +00:00
import java.util.Arrays;
import org.bukkit.Bukkit;
2022-04-18 00:10:12 +00:00
public class SchematicDownloadEndpoint extends AbstractServlet
2022-04-17 22:27:30 +00:00
{
2022-04-18 00:10:12 +00:00
@GetMapping(endpoint = "/api/schematics/download/")
public String downloadSchematic(HttpServletRequest request, HttpServletResponse response)
2022-04-17 22:27:30 +00:00
{
2022-04-17 23:21:44 +00:00
if (request.getPathInfo() == null || request.getPathInfo().equals("/"))
{
return schematicHTML();
}
else
{
2022-04-17 23:54:38 +00:00
OutputStream outputStream;
2022-04-17 23:26:01 +00:00
try
{
2022-04-17 23:21:44 +00:00
outputStream = response.getOutputStream();
2022-04-17 23:26:01 +00:00
}
catch (IOException e)
{
2022-04-17 23:21:44 +00:00
return null;
}
schematicServe(request.getPathInfo().replace("/", ""), outputStream);
return null;
}
2022-04-17 22:27:30 +00:00
}
2022-04-17 23:21:44 +00:00
private File getWorldeditFolder()
2022-04-17 22:27:30 +00:00
{
if (Bukkit.getPluginManager().isPluginEnabled("WorldEdit"))
{
2022-04-17 23:21:44 +00:00
return new File(Bukkit.getPluginManager().getPlugin("WorldEdit").getDataFolder() + "/schematics/");
2022-04-17 22:27:30 +00:00
}
else if (Bukkit.getPluginManager().isPluginEnabled("FastAsyncWorldEdit"))
{
2022-04-17 23:21:44 +00:00
return new File(Bukkit.getPluginManager().getPlugin("FastAsyncWorldEdit").getDataFolder() + "/schematics/");
2022-04-17 22:27:30 +00:00
}
else
{
return null;
}
2022-04-17 23:21:44 +00:00
}
private void schematicServe(String requestedSchematic, OutputStream outputStream)
{
File worldeditFolder = getWorldeditFolder();
2022-04-17 23:26:01 +00:00
if (worldeditFolder == null)
{
return;
}
2022-04-17 23:21:44 +00:00
File[] schems = worldeditFolder.listFiles();
if (schems != null)
{
File schemFile = Arrays.stream(schems).filter(file -> file.getName().equals(requestedSchematic)).findFirst().orElse(null);
if (schemFile != null)
{
try
{
2022-04-17 23:54:10 +00:00
byte[] schemData = HTTPDModule.fileCache.getFile(schemFile);
2022-04-18 00:21:12 +00:00
if (schemData != null)
{
outputStream.write(schemData);
}
2022-04-17 23:26:01 +00:00
}
catch (IOException ignored)
{
}
2022-04-17 23:21:44 +00:00
}
}
}
private String schematicHTML()
{
2022-04-18 00:10:12 +00:00
String file = readFile(this.getClass().getResourceAsStream("/httpd/schematic_download.html"));
2022-04-17 23:21:44 +00:00
File worldeditFolder = getWorldeditFolder();
2022-04-17 23:26:01 +00:00
if (worldeditFolder == null)
{
return null;
}
2022-04-17 22:27:30 +00:00
StringBuilder sb = new StringBuilder();
File[] alphabetical = worldeditFolder.listFiles();
if (alphabetical != null)
{
Arrays.sort(alphabetical);
2022-04-17 23:21:44 +00:00
for (File worldeditFile : alphabetical)
{
2022-04-18 00:21:12 +00:00
String sanitizedName = worldeditFile.getName().replaceAll("<[^>]*>", "");
2022-04-17 23:21:44 +00:00
sb.append("<tr>" +
2022-04-18 00:21:12 +00:00
"<th scope=\"row\"><a href=\"" + worldeditFile.getName() + "\" download>" + sanitizedName + "</a></th>" +
2022-04-17 23:54:38 +00:00
"<td>" + formattedSize(worldeditFile.length()) + "</td>" +
2022-04-17 23:21:44 +00:00
"</tr>");
}
file = file.replace("${schematics}", sb.toString());
2022-04-17 22:27:30 +00:00
}
return file;
}
}