mirror of
https://github.com/plexusorg/Module-HTTPD.git
synced 2025-07-03 08:56:42 +00:00
add caching (untested)
This commit is contained in:
@ -1,50 +1,92 @@
|
||||
package dev.plex.request.impl;
|
||||
|
||||
import dev.plex.HTTPDModule;
|
||||
import dev.plex.request.AbstractServlet;
|
||||
import dev.plex.request.GetMapping;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Arrays;
|
||||
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
public class SchematicDownloadEndpoint extends AbstractServlet
|
||||
{
|
||||
@GetMapping(endpoint = "/api/schematics/download/")
|
||||
public String downloadSchematics(HttpServletRequest request)
|
||||
public String downloadSchematics(HttpServletRequest request, HttpServletResponse response)
|
||||
{
|
||||
return schematicHTML();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
private String schematicHTML()
|
||||
private File getWorldeditFolder()
|
||||
{
|
||||
String file = readFile(this.getClass().getResourceAsStream("/httpd/schematic_list.html"));
|
||||
File worldeditFolder;
|
||||
if (Bukkit.getPluginManager().isPluginEnabled("WorldEdit"))
|
||||
{
|
||||
worldeditFolder = new File(Bukkit.getPluginManager().getPlugin("WorldEdit").getDataFolder() + "/schematics/");
|
||||
return new File(Bukkit.getPluginManager().getPlugin("WorldEdit").getDataFolder() + "/schematics/");
|
||||
}
|
||||
else if (Bukkit.getPluginManager().isPluginEnabled("FastAsyncWorldEdit"))
|
||||
{
|
||||
worldeditFolder = new File(Bukkit.getPluginManager().getPlugin("FastAsyncWorldEdit").getDataFolder() + "/schematics/");
|
||||
return new File(Bukkit.getPluginManager().getPlugin("FastAsyncWorldEdit").getDataFolder() + "/schematics/");
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
File[] alphabetical = worldeditFolder.listFiles();
|
||||
if (alphabetical != null)
|
||||
{
|
||||
Arrays.sort(alphabetical);
|
||||
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());
|
||||
}
|
||||
for (File worldeditFile : alphabetical)
|
||||
{
|
||||
sb.append("<tr>" +
|
||||
"<th scope=\"row\">" + worldeditFile.getName() + "</th>" +
|
||||
"<td>" + worldeditFile.length() + "B" + "</td>" +
|
||||
"</tr>");
|
||||
}
|
||||
file = file.replace("${schematics}", sb.toString());
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user