2022-07-24 12:10:00 +02:00
|
|
|
package de.bluecolored.bluemap.common.api;
|
|
|
|
|
|
|
|
import de.bluecolored.bluemap.api.WebApp;
|
|
|
|
import de.bluecolored.bluemap.common.plugin.Plugin;
|
2022-10-14 10:24:19 +02:00
|
|
|
import de.bluecolored.bluemap.core.util.FileHelper;
|
2022-07-24 12:10:00 +02:00
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
|
|
public class WebAppImpl implements WebApp {
|
2022-08-02 18:14:38 +02:00
|
|
|
private static final Path IMAGE_ROOT_PATH = Path.of("data", "images");
|
2022-07-24 12:10:00 +02:00
|
|
|
|
|
|
|
private final Plugin plugin;
|
|
|
|
|
|
|
|
public WebAppImpl(Plugin plugin) {
|
|
|
|
this.plugin = plugin;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Path getWebRoot() {
|
|
|
|
return plugin.getConfigs().getWebappConfig().getWebroot();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setPlayerVisibility(UUID player, boolean visible) {
|
|
|
|
if (visible) {
|
|
|
|
plugin.getPluginState().removeHiddenPlayer(player);
|
|
|
|
} else {
|
|
|
|
plugin.getPluginState().addHiddenPlayer(player);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 15:46:37 +02:00
|
|
|
@Override
|
|
|
|
public boolean getPlayerVisibility(UUID player) {
|
2022-11-07 11:48:28 +01:00
|
|
|
return !plugin.getPluginState().isPlayerHidden(player);
|
2022-08-25 15:46:37 +02:00
|
|
|
}
|
|
|
|
|
2022-07-24 12:10:00 +02:00
|
|
|
@Override
|
|
|
|
public String createImage(BufferedImage image, String path) throws IOException {
|
|
|
|
path = path.replaceAll("[^a-zA-Z0-9_.\\-/]", "_");
|
|
|
|
|
|
|
|
Path webRoot = getWebRoot().toAbsolutePath();
|
|
|
|
String separator = webRoot.getFileSystem().getSeparator();
|
|
|
|
|
2022-08-02 18:14:38 +02:00
|
|
|
Path imageRootFolder = webRoot.resolve(IMAGE_ROOT_PATH);
|
|
|
|
Path imagePath = imageRootFolder.resolve(Path.of(path.replace("/", separator) + ".png")).toAbsolutePath();
|
2022-07-24 12:10:00 +02:00
|
|
|
|
2022-10-14 10:24:19 +02:00
|
|
|
FileHelper.createDirectories(imagePath.getParent());
|
2022-07-24 12:10:00 +02:00
|
|
|
Files.deleteIfExists(imagePath);
|
|
|
|
Files.createFile(imagePath);
|
|
|
|
|
|
|
|
if (!ImageIO.write(image, "png", imagePath.toFile()))
|
|
|
|
throw new IOException("The format 'png' is not supported!");
|
|
|
|
|
|
|
|
return webRoot.relativize(imagePath).toString().replace(separator, "/");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Map<String, String> availableImages() throws IOException {
|
|
|
|
Path webRoot = getWebRoot().toAbsolutePath();
|
|
|
|
String separator = webRoot.getFileSystem().getSeparator();
|
|
|
|
|
|
|
|
Path imageRootPath = webRoot.resolve("data").resolve(IMAGE_ROOT_PATH).toAbsolutePath();
|
|
|
|
|
|
|
|
Map<String, String> availableImagesMap = new HashMap<>();
|
|
|
|
|
2022-08-02 18:14:38 +02:00
|
|
|
if (Files.exists(imageRootPath)) {
|
|
|
|
try (Stream<Path> fileStream = Files.walk(imageRootPath)) {
|
|
|
|
fileStream
|
|
|
|
.filter(p -> !Files.isDirectory(p))
|
|
|
|
.filter(p -> p.getFileName().toString().endsWith(".png"))
|
|
|
|
.map(Path::toAbsolutePath)
|
|
|
|
.forEach(p -> {
|
|
|
|
try {
|
|
|
|
String key = imageRootPath.relativize(p).toString();
|
|
|
|
key = key
|
|
|
|
.substring(0, key.length() - 4) //remove .png
|
|
|
|
.replace(separator, "/");
|
|
|
|
|
|
|
|
String value = webRoot.relativize(p).toString()
|
|
|
|
.replace(separator, "/");
|
|
|
|
|
|
|
|
availableImagesMap.put(key, value);
|
|
|
|
} catch (IllegalArgumentException ignore) {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-07-24 12:10:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return availableImagesMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|