Properly handle static assets for announcements (#891)

This commit is contained in:
HotaruYS
2022-05-15 05:05:19 +02:00
committed by GitHub
parent 5b3c05a9a0
commit 159feb4064
3 changed files with 43 additions and 62 deletions

View File

@@ -6,6 +6,7 @@ import emu.grasscutter.server.http.Router;
import emu.grasscutter.utils.FileUtils;
import emu.grasscutter.utils.Utils;
import express.Express;
import express.http.MediaType;
import express.http.Request;
import express.http.Response;
import io.javalin.Javalin;
@@ -13,27 +14,15 @@ import io.javalin.Javalin;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import static emu.grasscutter.Configuration.DATA;
import static emu.grasscutter.Configuration.*;
/**
* Handles requests related to the announcements page.
*/
public final class AnnouncementsHandler implements Router {
private static String template, swjs, vue;
public AnnouncementsHandler() {
var templateFile = new File(Utils.toFilePath(DATA("/hk4e/announcement/index.html")));
var swjsFile = new File(Utils.toFilePath(DATA("/hk4e/announcement/sw.js")));
var vueFile = new File(Utils.toFilePath(DATA("/hk4e/announcement/vue.min.js")));
template = templateFile.exists() ? new String(FileUtils.read(template)) : null;
swjs = swjsFile.exists() ? new String(FileUtils.read(swjs)) : null;
vue = vueFile.exists() ? new String(FileUtils.read(vueFile)) : null;
}
@Override public void applyRoutes(Express express, Javalin handle) {
// hk4e-api-os.hoyoverse.com
express.all("/common/hk4e_global/announcement/api/getAlertPic", new HttpJsonResponse("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"total\":0,\"list\":[]}}"));
@@ -47,58 +36,57 @@ public final class AnnouncementsHandler implements Router {
express.all("/hk4e_global/mdk/shopwindow/shopwindow/listPriceTier", new HttpJsonResponse("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"suggest_currency\":\"USD\",\"tiers\":[]}}"));
express.get("/hk4e/announcement/*", AnnouncementsHandler::getPageResources);
express.get("/sw.js", AnnouncementsHandler::getPageResources);
express.get("/dora/lib/vue/2.6.11/vue.min.js", AnnouncementsHandler::getPageResources);
}
private static void getAnnouncement(Request request, Response response) {
String data = "";
if (Objects.equals(request.baseUrl(), "/common/hk4e_global/announcement/api/getAnnContent")) {
String data = readToString(Paths.get(DATA("GameAnnouncement.json")).toFile());
response.send("{\"retcode\":0,\"message\":\"OK\",\"data\":" + data + "}");
data = readToString(new File(Utils.toFilePath(DATA("GameAnnouncement.json"))));
} else if (Objects.equals(request.baseUrl(), "/common/hk4e_global/announcement/api/getAnnList")) {
String data = readToString(Paths.get(DATA("GameAnnouncementList.json")).toFile())
.replace("System.currentTimeMillis()", String.valueOf(System.currentTimeMillis()));
response.send("{\"retcode\":0,\"message\":\"OK\",\"data\": " + data + "}");
data = readToString(new File(Utils.toFilePath(DATA("GameAnnouncementList.json"))));
} else {
response.send("{\"retcode\":404,\"message\":\"Unknown request path\"}");
}
if (data.isEmpty()) {
response.send("{\"retcode\":500,\"message\":\"Unable to fetch requsted content\"}");
return;
}
String dispatchDomain = "http" + (HTTP_ENCRYPTION.useInRouting ? "s" : "") + "://"
+ lr(HTTP_INFO.accessAddress, HTTP_INFO.bindAddress) + ":"
+ lr(HTTP_INFO.accessPort, HTTP_INFO.bindPort);
data = data
.replace("{{DISPATCH_PUBLIC}}", dispatchDomain)
.replace("{{SYSTEM_TIME}}", String.valueOf(System.currentTimeMillis()));
response.send("{\"retcode\":0,\"message\":\"OK\",\"data\": " + data + "}");
}
private static void getPageResources(Request request, Response response) {
var path = request.path();
switch(path) {
case "/sw.js" -> response.send(swjs);
case "/hk4e/announcement/index.html" -> response.send(template);
case "/dora/lib/vue/2.6.11/vue.min.js" -> response.send(vue);
default -> {
File renderFile = new File(Utils.toFilePath(DATA(path)));
if(!renderFile.exists()) {
Grasscutter.getLogger().info("File not exist: " + path);
return;
}
String ext = path.substring(path.lastIndexOf(".") + 1);
if ("css".equals(ext)) {
response.type("text/css");
response.send(FileUtils.read(renderFile));
} else {
response.send(FileUtils.read(renderFile));
}
}
String filename = Utils.toFilePath(DATA(request.path()));
File file = new File(filename);
if (file.exists() && file.isFile()) {
MediaType fromExtension = MediaType.getByExtension(filename.substring(filename.lastIndexOf(".") + 1));
response.type((fromExtension != null) ? fromExtension.getMIME() : "application/octet-stream");
response.send(FileUtils.read(file));
} else {
Grasscutter.getLogger().warn("File does not exist: " + file);
response.status(404);
}
}
@SuppressWarnings("ResultOfMethodCallIgnored")
private static String readToString(File file) {
long length = file.length();
byte[] content = new byte[(int) length];
byte[] content = new byte[(int) file.length()];
try {
FileInputStream in = new FileInputStream(file);
in.read(content); in.close();
} catch (IOException ignored) {
Grasscutter.getLogger().warn("File not found: " + file.getAbsolutePath());
Grasscutter.getLogger().warn("File does not exist: " + file);
}
return new String(content);
return new String(content, StandardCharsets.UTF_8);
}
}