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;
|
|
|
|
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;
|
2022-04-17 23:21:44 +00:00
|
|
|
|
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
2022-04-17 22:27:30 +00:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
|
|
|
|
public class SchematicDownloadEndpoint extends AbstractServlet
|
|
|
|
{
|
|
|
|
@GetMapping(endpoint = "/api/schematics/download/")
|
2022-04-17 23:21:44 +00:00
|
|
|
public String downloadSchematics(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
|
|
|
|
{
|
|
|
|
OutputStream outputStream = null;
|
|
|
|
try {
|
|
|
|
outputStream = response.getOutputStream();
|
|
|
|
} catch (IOException e) {
|
|
|
|
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();
|
|
|
|
if (worldeditFolder == null) return;
|
|
|
|
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
|
|
|
|
{
|
|
|
|
outputStream.write(HTTPDModule.fileCache.getFile(schemFile));
|
|
|
|
} catch (IOException ignored) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String schematicHTML()
|
|
|
|
{
|
|
|
|
String file = readFile(this.getClass().getResourceAsStream("/httpd/schematic_list.html"));
|
|
|
|
File worldeditFolder = getWorldeditFolder();
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
sb.append("<tr>" +
|
|
|
|
"<th scope=\"row\"><a href=\"" + worldeditFile.getName() + "\" download>" + worldeditFile.getName() + "</a></th>" +
|
|
|
|
"<td>" + worldeditFile.length() + "B" + "</td>" +
|
|
|
|
"</tr>");
|
|
|
|
}
|
|
|
|
file = file.replace("${schematics}", sb.toString());
|
2022-04-17 22:27:30 +00:00
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
}
|