mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-08-21 20:22:45 +02:00
[Debt] Removed FileUtil.getStringFromResource
Because of PlanPlugin dependency this code needed to be moved elsewhere. New method to use is FileSystem#readCustomizableResourceFlat
This commit is contained in:
parent
e7cbf10ee3
commit
594e9b4072
|
@ -39,10 +39,6 @@ public class FileSystem implements SubSystem {
|
|||
this.configFile = getFileFromPluginFolder("config.yml");
|
||||
}
|
||||
|
||||
public List<String> readFromResource(String fileName) throws IOException {
|
||||
return FileUtil.lines(plugin, fileName);
|
||||
}
|
||||
|
||||
public File getDataFolder() {
|
||||
return dataFolder;
|
||||
}
|
||||
|
@ -88,8 +84,42 @@ public class FileSystem implements SubSystem {
|
|||
// No disable actions necessary.
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a file from jar as lines.
|
||||
*
|
||||
* @param fileName Name of the file.
|
||||
* @return lines of the file
|
||||
* @throws IOException If the resource can not be read.
|
||||
*/
|
||||
public List<String> readFromResource(String fileName) throws IOException {
|
||||
return FileUtil.lines(plugin, fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a file from jar as a flat String.
|
||||
*
|
||||
* @param fileName Name of the file
|
||||
* @return Flattened lines with {@code \r\n} line separators.
|
||||
* @throws IOException If the resource can not be read.
|
||||
*/
|
||||
public String readFromResourceFlat(String fileName) throws IOException {
|
||||
List<String> lines = readFromResource(fileName);
|
||||
return flatten(readFromResource(fileName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a file from jar or /plugins/Plan/ folder.
|
||||
*
|
||||
* @param fileName Name of the file
|
||||
* @return Flattened lines with {@code \r\n} line separators.
|
||||
* @throws IOException If the resource can not be read.
|
||||
*/
|
||||
public String readCustomizableResourceFlat(String fileName) throws IOException {
|
||||
return flatten(FileUtil.lines(
|
||||
plugin, new File(plugin.getDataFolder(), fileName.replace("/", File.separator)), fileName
|
||||
));
|
||||
}
|
||||
|
||||
private String flatten(List<String> lines) {
|
||||
StringBuilder flat = new StringBuilder();
|
||||
for (String line : lines) {
|
||||
flat.append(line).append("\r\n");
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
*/
|
||||
package com.djrapitops.plan.system.webserver.response;
|
||||
|
||||
import com.djrapitops.plan.system.file.FileSystem;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.NotFoundResponse;
|
||||
import com.djrapitops.plan.utilities.file.FileUtil;
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -20,10 +20,13 @@ import java.io.IOException;
|
|||
*/
|
||||
public class FileResponse extends Response {
|
||||
|
||||
// TODO
|
||||
private FileSystem fileSystem;
|
||||
|
||||
public FileResponse(String fileName) {
|
||||
super.setHeader("HTTP/1.1 200 OK");
|
||||
try {
|
||||
super.setContent(FileUtil.getStringFromResource(fileName));
|
||||
super.setContent(fileSystem.readCustomizableResourceFlat(fileName));
|
||||
} catch (IOException e) {
|
||||
super.setContent(new NotFoundResponse(fileName + " was not found inside the .jar or /plugins/Plan/ folder").getContent());
|
||||
}
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
*/
|
||||
package com.djrapitops.plan.system.webserver.response.errors;
|
||||
|
||||
import com.djrapitops.plan.system.file.FileSystem;
|
||||
import com.djrapitops.plan.system.webserver.response.Response;
|
||||
import com.djrapitops.plan.utilities.file.FileUtil;
|
||||
import com.djrapitops.plugin.logging.L;
|
||||
import com.djrapitops.plugin.logging.error.ErrorHandler;
|
||||
import org.apache.commons.text.StringSubstitutor;
|
||||
|
@ -26,11 +26,12 @@ public class ErrorResponse extends Response {
|
|||
|
||||
// TODO
|
||||
private String version;
|
||||
private FileSystem fileSystem;
|
||||
private ErrorHandler errorHandler;
|
||||
|
||||
public ErrorResponse() {
|
||||
try {
|
||||
setContent(FileUtil.getStringFromResource("web/error.html"));
|
||||
setContent(fileSystem.readCustomizableResourceFlat("web/error.html"));
|
||||
} catch (IOException e) {
|
||||
errorHandler.log(L.WARN, this.getClass(), e);
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ package com.djrapitops.plan.utilities.file;
|
|||
import com.djrapitops.plan.PlanPlugin;
|
||||
import com.djrapitops.plan.utilities.MiscUtils;
|
||||
import com.djrapitops.plugin.logging.L;
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
|
@ -22,16 +21,6 @@ public class FileUtil {
|
|||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static String getStringFromResource(String fileName) throws IOException {
|
||||
StringBuilder html = new StringBuilder();
|
||||
PlanPlugin plugin = PlanPlugin.getInstance();
|
||||
|
||||
lines(PlanPlugin.getInstance(), new File(plugin.getDataFolder(), fileName.replace("/", File.separator)), fileName)
|
||||
.forEach(line -> html.append(line).append("\r\n"));
|
||||
return html.toString();
|
||||
}
|
||||
|
||||
public static List<String> lines(PlanPlugin plugin, File savedFile, String defaults) throws IOException {
|
||||
if (savedFile.exists()) {
|
||||
return lines(savedFile);
|
||||
|
@ -97,7 +86,7 @@ public class FileUtil {
|
|||
|
||||
public static List<String> lines(File file, Charset charset) throws IOException {
|
||||
List<String> lines = new ArrayList<>();
|
||||
if (Verify.exists(file)) {
|
||||
if (file != null && file.exists()) {
|
||||
try (Stream<String> linesStream = Files.lines(file.toPath(), charset)) {
|
||||
lines = linesStream.collect(Collectors.toList());
|
||||
}
|
||||
|
|
|
@ -6,8 +6,8 @@ package com.djrapitops.plan.utilities.html.pages;
|
|||
|
||||
import com.djrapitops.plan.api.exceptions.ParseException;
|
||||
import com.djrapitops.plan.data.store.containers.AnalysisContainer;
|
||||
import com.djrapitops.plan.system.file.FileSystem;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.ErrorResponse;
|
||||
import com.djrapitops.plan.utilities.file.FileUtil;
|
||||
import com.djrapitops.plan.utilities.formatting.Formatter;
|
||||
import com.djrapitops.plan.utilities.formatting.PlaceholderReplacer;
|
||||
import com.djrapitops.plugin.api.Benchmark;
|
||||
|
@ -28,10 +28,16 @@ public class AnalysisPage implements Page {
|
|||
|
||||
private final AnalysisContainer analysisContainer;
|
||||
|
||||
private final FileSystem fileSystem;
|
||||
private final Formatter<Double> decimalFormatter;
|
||||
|
||||
AnalysisPage(AnalysisContainer analysisContainer, Formatter<Double> decimalFormatter) {
|
||||
AnalysisPage(
|
||||
AnalysisContainer analysisContainer,
|
||||
FileSystem fileSystem,
|
||||
Formatter<Double> decimalFormatter
|
||||
) {
|
||||
this.analysisContainer = analysisContainer;
|
||||
this.fileSystem = fileSystem;
|
||||
this.decimalFormatter = decimalFormatter;
|
||||
}
|
||||
|
||||
|
@ -67,7 +73,7 @@ public class AnalysisPage implements Page {
|
|||
performanceNumbers(placeholderReplacer);
|
||||
|
||||
try {
|
||||
return placeholderReplacer.apply(FileUtil.getStringFromResource("web/server.html"));
|
||||
return placeholderReplacer.apply(fileSystem.readCustomizableResourceFlat("web/server.html"));
|
||||
} catch (IOException e) {
|
||||
throw new ParseException(e);
|
||||
} finally {
|
||||
|
|
|
@ -12,13 +12,13 @@ import com.djrapitops.plan.data.store.keys.PlayerKeys;
|
|||
import com.djrapitops.plan.data.store.mutators.*;
|
||||
import com.djrapitops.plan.data.time.WorldTimes;
|
||||
import com.djrapitops.plan.system.cache.SessionCache;
|
||||
import com.djrapitops.plan.system.file.FileSystem;
|
||||
import com.djrapitops.plan.system.info.server.ServerInfo;
|
||||
import com.djrapitops.plan.system.settings.Settings;
|
||||
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
||||
import com.djrapitops.plan.system.settings.theme.Theme;
|
||||
import com.djrapitops.plan.system.settings.theme.ThemeVal;
|
||||
import com.djrapitops.plan.utilities.comparators.SessionStartComparator;
|
||||
import com.djrapitops.plan.utilities.file.FileUtil;
|
||||
import com.djrapitops.plan.utilities.formatting.Formatter;
|
||||
import com.djrapitops.plan.utilities.formatting.Formatters;
|
||||
import com.djrapitops.plan.utilities.formatting.PlaceholderReplacer;
|
||||
|
@ -48,6 +48,7 @@ public class InspectPage implements Page {
|
|||
|
||||
private final String version;
|
||||
|
||||
private final FileSystem fileSystem;
|
||||
private final PlanConfig config;
|
||||
private final Theme theme;
|
||||
private final Graphs graphs;
|
||||
|
@ -64,6 +65,7 @@ public class InspectPage implements Page {
|
|||
InspectPage(
|
||||
PlayerContainer player, Map<UUID, String> serverNames,
|
||||
String version,
|
||||
FileSystem fileSystem,
|
||||
PlanConfig config,
|
||||
Theme theme,
|
||||
Graphs graphs,
|
||||
|
@ -76,6 +78,7 @@ public class InspectPage implements Page {
|
|||
this.player = player;
|
||||
this.serverNames = serverNames;
|
||||
this.version = version;
|
||||
this.fileSystem = fileSystem;
|
||||
this.config = config;
|
||||
this.theme = theme;
|
||||
this.graphs = graphs;
|
||||
|
@ -228,7 +231,7 @@ public class InspectPage implements Page {
|
|||
: serverName
|
||||
);
|
||||
|
||||
return replacer.apply(FileUtil.getStringFromResource("web/player.html"));
|
||||
return replacer.apply(fileSystem.readCustomizableResourceFlat("web/player.html"));
|
||||
}
|
||||
|
||||
private void sessionsAndPlaytime(PlaceholderReplacer replacer, SessionsMutator sessionsMutator, SessionsMutator daySessionsMutator, SessionsMutator weekSessionsMutator, SessionsMutator monthSessionsMutator) {
|
||||
|
|
|
@ -7,12 +7,12 @@ package com.djrapitops.plan.utilities.html.pages;
|
|||
import com.djrapitops.plan.api.exceptions.ParseException;
|
||||
import com.djrapitops.plan.data.store.containers.NetworkContainer;
|
||||
import com.djrapitops.plan.data.store.keys.NetworkKeys;
|
||||
import com.djrapitops.plan.utilities.formatting.PlaceholderReplacer;
|
||||
import com.djrapitops.plan.system.file.FileSystem;
|
||||
import com.djrapitops.plan.system.info.server.properties.ServerProperties;
|
||||
import com.djrapitops.plan.system.webserver.cache.PageId;
|
||||
import com.djrapitops.plan.system.webserver.cache.ResponseCache;
|
||||
import com.djrapitops.plan.system.webserver.response.pages.parts.NetworkPageContent;
|
||||
import com.djrapitops.plan.utilities.file.FileUtil;
|
||||
import com.djrapitops.plan.utilities.formatting.PlaceholderReplacer;
|
||||
|
||||
import static com.djrapitops.plan.data.store.keys.NetworkKeys.*;
|
||||
|
||||
|
@ -25,13 +25,16 @@ public class NetworkPage implements Page {
|
|||
|
||||
private final NetworkContainer networkContainer;
|
||||
|
||||
private final FileSystem fileSystem;
|
||||
private final ServerProperties serverProperties;
|
||||
|
||||
public NetworkPage(
|
||||
NetworkContainer networkContainer,
|
||||
FileSystem fileSystem,
|
||||
ServerProperties serverProperties
|
||||
) {
|
||||
this.networkContainer = networkContainer;
|
||||
this.fileSystem = fileSystem;
|
||||
this.serverProperties = serverProperties;
|
||||
}
|
||||
|
||||
|
@ -57,7 +60,7 @@ public class NetworkPage implements Page {
|
|||
ResponseCache.loadResponse(PageId.NETWORK_CONTENT.id(), NetworkPageContent::new);
|
||||
placeholderReplacer.put("tabContentServers", networkPageContent.getContents());
|
||||
|
||||
return placeholderReplacer.apply(FileUtil.getStringFromResource("web/network.html"));
|
||||
return placeholderReplacer.apply(fileSystem.readCustomizableResourceFlat("web/network.html"));
|
||||
} catch (Exception e) {
|
||||
throw new ParseException(e);
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import com.djrapitops.plan.data.store.containers.AnalysisContainer;
|
|||
import com.djrapitops.plan.data.store.containers.NetworkContainer;
|
||||
import com.djrapitops.plan.data.store.containers.PlayerContainer;
|
||||
import com.djrapitops.plan.system.database.databases.Database;
|
||||
import com.djrapitops.plan.system.file.FileSystem;
|
||||
import com.djrapitops.plan.system.info.connection.ConnectionSystem;
|
||||
import com.djrapitops.plan.system.info.server.ServerInfo;
|
||||
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
||||
|
@ -34,6 +35,7 @@ import java.util.UUID;
|
|||
public class PageFactory {
|
||||
|
||||
private final String version;
|
||||
private final Lazy<FileSystem> fileSystem;
|
||||
private final Lazy<PlanConfig> config;
|
||||
private final Lazy<Theme> theme;
|
||||
private final Lazy<Database> database;
|
||||
|
@ -51,6 +53,7 @@ public class PageFactory {
|
|||
@Inject
|
||||
public PageFactory(
|
||||
@Named("currentVersion") String version,
|
||||
Lazy<FileSystem> fileSystem,
|
||||
Lazy<PlanConfig> config,
|
||||
Lazy<Theme> theme,
|
||||
Lazy<Database> database,
|
||||
|
@ -66,6 +69,7 @@ public class PageFactory {
|
|||
Lazy<ErrorHandler> errorHandler
|
||||
) {
|
||||
this.version = version;
|
||||
this.fileSystem = fileSystem;
|
||||
this.config = config;
|
||||
this.theme = theme;
|
||||
this.database = database;
|
||||
|
@ -90,12 +94,14 @@ public class PageFactory {
|
|||
}
|
||||
|
||||
public PlayersPage playersPage() {
|
||||
return new PlayersPage(version, config.get(), database.get(), serverInfo.get(), tables.get(), timings.get());
|
||||
return new PlayersPage(version, fileSystem.get(), config.get(),
|
||||
database.get(), serverInfo.get(), tables.get(),
|
||||
timings.get());
|
||||
}
|
||||
|
||||
public AnalysisPage analysisPage(UUID serverUUID) {
|
||||
AnalysisContainer analysisContainer = new AnalysisContainer(database.get().fetch().getServerContainer(serverUUID));
|
||||
return new AnalysisPage(analysisContainer, formatters.get().decimals());
|
||||
return new AnalysisPage(analysisContainer, fileSystem.get(), formatters.get().decimals());
|
||||
}
|
||||
|
||||
public InspectPage inspectPage(UUID uuid) {
|
||||
|
@ -104,7 +110,7 @@ public class PageFactory {
|
|||
return new InspectPage(
|
||||
player, serverNames,
|
||||
version,
|
||||
config.get(), theme.get(),
|
||||
fileSystem.get(), config.get(), theme.get(),
|
||||
graphs.get(), tables.get(), accordions.get(), formatters.get(),
|
||||
serverInfo.get(), timings.get()
|
||||
);
|
||||
|
@ -116,6 +122,6 @@ public class PageFactory {
|
|||
|
||||
public NetworkPage networkPage() {
|
||||
NetworkContainer networkContainer = database.get().fetch().getNetworkContainer(); // Not cached, big.
|
||||
return new NetworkPage(networkContainer, serverInfo.get().getServerProperties());
|
||||
return new NetworkPage(networkContainer, fileSystem.get(), serverInfo.get().getServerProperties());
|
||||
}
|
||||
}
|
|
@ -3,10 +3,10 @@ package com.djrapitops.plan.utilities.html.pages;
|
|||
import com.djrapitops.plan.api.exceptions.ParseException;
|
||||
import com.djrapitops.plan.data.store.containers.PlayerContainer;
|
||||
import com.djrapitops.plan.system.database.databases.Database;
|
||||
import com.djrapitops.plan.system.file.FileSystem;
|
||||
import com.djrapitops.plan.system.info.server.ServerInfo;
|
||||
import com.djrapitops.plan.system.settings.Settings;
|
||||
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
||||
import com.djrapitops.plan.utilities.file.FileUtil;
|
||||
import com.djrapitops.plan.utilities.formatting.PlaceholderReplacer;
|
||||
import com.djrapitops.plan.utilities.html.tables.HtmlTables;
|
||||
import com.djrapitops.plugin.api.Check;
|
||||
|
@ -22,6 +22,7 @@ import java.util.List;
|
|||
public class PlayersPage implements Page {
|
||||
|
||||
private final String version;
|
||||
private final FileSystem fileSystem;
|
||||
private final PlanConfig config;
|
||||
private final Database database;
|
||||
private final ServerInfo serverInfo;
|
||||
|
@ -32,6 +33,7 @@ public class PlayersPage implements Page {
|
|||
|
||||
PlayersPage(
|
||||
String version,
|
||||
FileSystem fileSystem,
|
||||
PlanConfig config,
|
||||
Database database,
|
||||
ServerInfo serverInfo,
|
||||
|
@ -39,6 +41,7 @@ public class PlayersPage implements Page {
|
|||
Timings timings
|
||||
) {
|
||||
this.version = version;
|
||||
this.fileSystem = fileSystem;
|
||||
this.config = config;
|
||||
this.database = database;
|
||||
this.serverInfo = serverInfo;
|
||||
|
@ -63,7 +66,7 @@ public class PlayersPage implements Page {
|
|||
placeholderReplacer.put("playersTable", tables.playerTableForPlayersPage(playerContainers).parseHtml());
|
||||
timings.end("Pages", "Players page players table parsing");
|
||||
|
||||
return placeholderReplacer.apply(FileUtil.getStringFromResource("web/players.html"));
|
||||
return placeholderReplacer.apply(fileSystem.readCustomizableResourceFlat("web/players.html"));
|
||||
} catch (Exception e) {
|
||||
throw new ParseException(e);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user