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

112 lines
3.6 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;
2022-04-24 22:24:38 +00:00
import dev.plex.util.PlexLog;
2022-04-17 22:27:30 +00:00
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-24 22:24:38 +00:00
import java.util.ArrayList;
2022-04-17 22:27:30 +00:00
import java.util.Arrays;
2022-04-24 22:24:38 +00:00
import java.util.List;
2022-04-17 22:27:30 +00:00
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 void schematicServe(String requestedSchematic, OutputStream outputStream)
{
2022-04-18 03:05:20 +00:00
File worldeditFolder = HTTPDModule.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-18 03:05:20 +00:00
File worldeditFolder = HTTPDModule.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();
2022-04-24 22:24:38 +00:00
for (File worldeditFile : listFilesForFolder(worldeditFolder))
2022-04-17 22:27:30 +00:00
{
2022-04-24 22:24:38 +00:00
String fixedPath = worldeditFile.getPath().replace("plugins/FastAsyncWorldEdit/schematics/", "");
2022-08-02 05:09:46 +00:00
fixedPath = fixedPath.replace("plugins/WorldEdit/schematics/", "");
2022-04-24 22:24:38 +00:00
String sanitizedName = fixedPath.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
sb.append(" <tr>\n" +
" <th scope=\"row\">\n <a href=\"" + fixedPath + "\" download>" + sanitizedName + "</a>\n </th>\n" +
" <td>\n " + formattedSize(worldeditFile.length()) + "\n </td>\n" +
" </tr>\n");
}
file = file.replace("${schematics}", sb.toString());
return file;
}
List<File> files = new ArrayList<>();
public List<File> listFilesForFolder(final File folder)
{
for (File fileEntry : folder.listFiles())
{
if (fileEntry.isDirectory())
2022-04-17 23:21:44 +00:00
{
2022-04-24 22:24:38 +00:00
PlexLog.debug("Found directory");
listFilesForFolder(fileEntry);
}
else
{
files.add(fileEntry);
2022-04-17 23:21:44 +00:00
}
2022-04-17 22:27:30 +00:00
}
2022-04-24 22:24:38 +00:00
PlexLog.debug(files.toString());
return files;
2022-04-17 22:27:30 +00:00
}
}