2022-12-13 16:54:31 +01:00
|
|
|
/*
|
|
|
|
* This file is part of BlueMap, licensed under the MIT License (MIT).
|
|
|
|
*
|
|
|
|
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
|
|
|
|
* Copyright (c) contributors
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
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() {
|
2024-02-01 23:01:42 +01:00
|
|
|
return plugin.getBlueMap().getConfig().getWebappConfig().getWebroot();
|
2022-07-24 12:10:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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-12-13 16:54:31 +01:00
|
|
|
@Override
|
|
|
|
public void registerScript(String url) {
|
|
|
|
plugin.getBlueMap().getWebFilesManager().getScripts().add(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void registerStyle(String url) {
|
|
|
|
plugin.getBlueMap().getWebFilesManager().getStyles().add(url);
|
|
|
|
}
|
|
|
|
|
2022-07-24 12:10:00 +02:00
|
|
|
@Override
|
2022-12-29 17:49:08 +01:00
|
|
|
@Deprecated(forRemoval = true)
|
2022-07-24 12:10:00 +02:00
|
|
|
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
|
2024-02-01 23:01:42 +01:00
|
|
|
@Deprecated(forRemoval = true)
|
2022-07-24 12:10:00 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|