[#752] Html Customization for favicon.ico file

Added a new favicon.ico file that can be customized like all other
web files, by placing a favicon.ico file to /plugins/Plan/web
This commit is contained in:
Rsl1122 2018-10-27 11:51:36 +03:00
parent 7d1f70e5a1
commit 23613d7a63
10 changed files with 88 additions and 7 deletions

View File

@ -20,6 +20,7 @@
<include>**/*.js</include>
<include>**/*.css</include>
<include>locale/*.txt</include>
<include>**/*.ico</include>
</includes>
</resource>
</resources>

View File

@ -223,6 +223,7 @@
<include>**/*.js</include>
<include>**/*.css</include>
<include>locale/*.txt</include>
<include>**/*.ico</include>
</includes>
</resource>
</resources>

View File

@ -14,6 +14,7 @@ import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
@ -110,6 +111,10 @@ public class PlanFiles implements SubSystem {
));
}
public InputStream readCustomizableResource(String fileName) {
return FileUtil.stream(plugin, new File(plugin.getDataFolder(), fileName.replace("/", File.separator)), fileName);
}
private String flatten(List<String> lines) {
StringBuilder flat = new StringBuilder();
for (String line : lines) {

View File

@ -66,7 +66,6 @@ public class ResponseHandler extends TreePageHandler {
}
public void registerPages() {
registerPage("favicon.ico", responseFactory.redirectResponse("https://puu.sh/tK0KL/6aa2ba141b.ico"), 5);
registerPage("debug", debugPageHandler);
registerPage("players", playersPageHandler);
registerPage("player", playerPageHandler);
@ -124,6 +123,9 @@ public class ResponseHandler extends TreePageHandler {
if (targetString.endsWith(".js")) {
return ResponseCache.loadResponse(PageId.JS.of(targetString), () -> responseFactory.javaScriptResponse(targetString));
}
if (targetString.endsWith("favicon.ico")) {
return ResponseCache.loadResponse(PageId.FAVICON.id(), responseFactory::faviconResponse);
}
boolean isNotInfoRequest = target.isEmpty() || !target.get(0).equals("info");
boolean isAuthRequired = webServer.get().isAuthRequired() && isNotInfoRequest;
if (isAuthRequired) {

View File

@ -26,7 +26,7 @@ public enum PageId {
JS("js:"),
CSS("css:"),
FAVICON_REDIRECT("Redirect:Favicon"),
FAVICON("Favicon"),
PLAYER_PLUGINS_TAB("playerPluginsTab:"),
NETWORK_CONTENT("networkContent");

View File

@ -0,0 +1,44 @@
package com.djrapitops.plan.system.webserver.response;
import com.djrapitops.plan.system.file.PlanFiles;
import com.djrapitops.plan.system.locale.Locale;
import com.djrapitops.plan.system.settings.theme.Theme;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* {@link Response} for raw bytes.
*
* @author Rsl1122
*/
public class ByteResponse extends Response {
private final PlanFiles files;
private final String fileName;
public ByteResponse(ResponseType type, String fileName, PlanFiles files) {
super(type);
this.fileName = fileName;
this.files = files;
setHeader("HTTP/1.1 200 OK");
}
@Override
public void send(HttpExchange exchange, Locale locale, Theme theme) throws IOException {
responseHeaders.set("Accept-Ranges", "bytes");
exchange.sendResponseHeaders(getCode(), 0);
try (OutputStream out = exchange.getResponseBody();
InputStream bis = files.readCustomizableResource(fileName)) {
byte[] buffer = new byte[2048];
int count;
while ((count = bis.read(buffer)) != -1) {
out.write(buffer, 0, count);
}
}
}
}

View File

@ -115,6 +115,10 @@ public class ResponseFactory {
return new RedirectResponse(location);
}
public Response faviconResponse() {
return new ByteResponse(ResponseType.X_ICON, "web/favicon.ico", files);
}
public ErrorResponse pageNotFound404() {
return notFound404(locale.getString(ErrorPageLang.UNKNOWN_PAGE_404));
}

View File

@ -13,7 +13,9 @@ public enum ResponseType {
HTML("text/html; charset=utf-8"),
CSS("text/css"),
JSON("application/json"),
JAVASCRIPT("application/javascript");
JAVASCRIPT("application/javascript"),
IMAGE("image/gif"),
X_ICON("image/x-icon");
private final String type;

View File

@ -4,10 +4,7 @@ import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.utilities.MiscUtils;
import com.djrapitops.plugin.logging.L;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@ -34,6 +31,31 @@ public class FileUtil {
return lines(plugin, defaults);
}
public static InputStream stream(PlanPlugin plugin, File savedFile, String defaults) {
try {
if (savedFile.exists()) {
return stream(savedFile);
} else {
String fileName = savedFile.getName();
File found = attemptToFind(fileName, new File(plugin.getDataFolder(), "web"));
if (found != null) {
return stream(found);
}
}
} catch (FileNotFoundException ignore) {
// File was not found, use jar version
}
return stream(plugin, defaults);
}
private static InputStream stream(PlanPlugin plugin, String resource) {
return plugin.getResource(resource);
}
private static InputStream stream(File savedFile) throws FileNotFoundException {
return new FileInputStream(savedFile);
}
/**
* Breadth-First search through the file tree to find the file.
*

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB