From fe216ea64831895737e62428a60c80394a799ecd Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sun, 22 Nov 2020 08:01:38 +0000 Subject: [PATCH] add webui properties with a drop box configuration --- .../muwire/webui/ConfigurationServlet.java | 11 +++++- .../java/com/muwire/webui/InitServlet.java | 14 +++++++- .../java/com/muwire/webui/MuWireClient.java | 27 ++++++++++++-- .../java/com/muwire/webui/WebUISettings.java | 36 +++++++++++++++++++ webui/src/main/webapp/ConfigurationPage.jsp | 8 +++++ webui/src/main/webapp/MuWire.jsp | 4 +++ 6 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 webui/src/main/java/com/muwire/webui/WebUISettings.java diff --git a/webui/src/main/java/com/muwire/webui/ConfigurationServlet.java b/webui/src/main/java/com/muwire/webui/ConfigurationServlet.java index 9f6904e0..70662e3c 100644 --- a/webui/src/main/java/com/muwire/webui/ConfigurationServlet.java +++ b/webui/src/main/java/com/muwire/webui/ConfigurationServlet.java @@ -4,6 +4,7 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.PrintStream; +import java.io.PrintWriter; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; @@ -28,6 +29,7 @@ public class ConfigurationServlet extends HttpServlet { INPUT_VALIDATORS.put("uploadSlotsPerUser", new IntegerValidator("Upload slots per user (-1 means unlimited)")); INPUT_VALIDATORS.put("downloadLocation", new DirectoryValidator()); INPUT_VALIDATORS.put("incompleteLocation", new DirectoryValidator()); + INPUT_VALIDATORS.put("dropBoxLocation", new DirectoryValidator()); INPUT_VALIDATORS.put("speedSmoothSeconds", new PositiveIntegerValidator("Download speed smoothing interval (seconds)")); INPUT_VALIDATORS.put("inbound.length", new PositiveIntegerValidator("Inbound tunnel length")); INPUT_VALIDATORS.put("inbound.quantity", new PositiveIntegerValidator("Inbound tunnel quantity")); @@ -37,6 +39,7 @@ public class ConfigurationServlet extends HttpServlet { INPUT_VALIDATORS.put("defaultFeedItemsToKeep", new IntegerValidator("Number of items to keep on disk (-1 means unlimited)")); } + private WebUISettings webSettings; private Core core; private ServletContext context; @@ -60,6 +63,11 @@ public class ConfigurationServlet extends HttpServlet { String value = req.getParameter(name); update(name, value); } + + PrintWriter pw = new PrintWriter(new File(core.getHome(), "webui.properties"), "UTF-8"); + webSettings.write(pw); + pw.close(); + core.saveMuSettings(); core.saveI2PSettings(); context.setAttribute("MWConfigError", null); @@ -98,6 +106,7 @@ public class ConfigurationServlet extends HttpServlet { case "uploadSlotsPerUser" : core.getMuOptions().setUploadSlotsPerUser(Integer.parseInt(value)); break; case "downloadLocation" : core.getMuOptions().setDownloadLocation(getDirectory(value)); break; case "incompleteLocation" : core.getMuOptions().setIncompleteLocation(getDirectory(value)); break; + case "dropBoxLocation" : webSettings.setDropBoxLocation(getDirectory(value)); break; case "shareDownloadedFiles" : core.getMuOptions().setShareDownloadedFiles(true); break; case "shareHiddenFiles" : core.getMuOptions().setShareHiddenFiles(true); break; case "searchComments" : core.getMuOptions().setSearchComments(true); break; @@ -116,7 +125,6 @@ public class ConfigurationServlet extends HttpServlet { case "defaultFeedUpdateInterval" : core.getMuOptions().setDefaultFeedUpdateInterval(60000 * Long.parseLong(value)); break; case "defaultFeedItemsToKeep" : core.getMuOptions().setDefaultFeedItemsToKeep(Integer.parseInt(value)); break; - // TODO: ui settings } } @@ -178,6 +186,7 @@ public class ConfigurationServlet extends HttpServlet { public void init(ServletConfig config) throws ServletException { context = config.getServletContext(); core = (Core) config.getServletContext().getAttribute("core"); + webSettings = (WebUISettings) config.getServletContext().getAttribute("webSettings"); } } diff --git a/webui/src/main/java/com/muwire/webui/InitServlet.java b/webui/src/main/java/com/muwire/webui/InitServlet.java index 8853e50a..ebd9efca 100644 --- a/webui/src/main/java/com/muwire/webui/InitServlet.java +++ b/webui/src/main/java/com/muwire/webui/InitServlet.java @@ -48,8 +48,20 @@ public class InitServlet extends HttpServlet { else if (!incompleteLocationFile.canWrite()) throw new Exception("Incomplete files location not writeable"); + String dropBoxLocation = req.getParameter("dropbox_location"); + if (dropBoxLocation == null) + throw new Exception("DropBox location cannot be blank"); + File dropBoxLocationFile = new File(dropBoxLocation); + if (!dropBoxLocationFile.exists()) { + if (!dropBoxLocationFile.mkdirs()) + throw new Exception("Couldn't create DropBox location"); + } else if (dropBoxLocationFile.isFile()) + throw new Exception("DropBox location must point to a directory"); + else if (!dropBoxLocationFile.canWrite()) + throw new Exception("DropBox location not writeable"); + MuWireClient client = (MuWireClient) req.getServletContext().getAttribute("mwClient"); - client.initMWProps(nickname, downloadLocationFile, incompleteLocationFile); + client.initMWProps(nickname, downloadLocationFile, incompleteLocationFile, dropBoxLocationFile); client.start(); resp.sendRedirect("/MuWire/index"); } catch (Throwable e) { diff --git a/webui/src/main/java/com/muwire/webui/MuWireClient.java b/webui/src/main/java/com/muwire/webui/MuWireClient.java index 403939f0..79f0b2a3 100644 --- a/webui/src/main/java/com/muwire/webui/MuWireClient.java +++ b/webui/src/main/java/com/muwire/webui/MuWireClient.java @@ -59,7 +59,7 @@ public class MuWireClient { private final ServletContext servletContext; private final String version; private final String home; - private final File mwProps; + private final File mwProps, webUIProps; private volatile Core core; private volatile boolean coreLoaded; @@ -70,6 +70,7 @@ public class MuWireClient { this.version = version; this.home = home; this.mwProps = new File(home, "MuWire.properties"); + this.webUIProps = new File(home, "webui.properties"); } public void start() throws Throwable { @@ -87,8 +88,19 @@ public class MuWireClient { logger.addHandler(new I2PLogHandler(ctx)); } - BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(mwProps), StandardCharsets.UTF_8)); + BufferedReader reader; Properties props = new Properties(); + + if (webUIProps.exists() && webUIProps.isFile()) { + reader = new BufferedReader(new InputStreamReader(new FileInputStream(webUIProps), StandardCharsets.UTF_8)); + props.load(reader); + reader.close(); + } + WebUISettings webSettings = new WebUISettings(props); + servletContext.setAttribute("webSettings", webSettings); + + reader = new BufferedReader(new InputStreamReader(new FileInputStream(mwProps), StandardCharsets.UTF_8)); + props = new Properties(); props.load(reader); reader.close(); @@ -113,11 +125,13 @@ public class MuWireClient { return !mwProps.exists(); } - public void initMWProps(String nickname, File downloadLocation, File incompleteLocation) throws Exception { + public void initMWProps(String nickname, File downloadLocation, File incompleteLocation, File dropBoxLocation) throws Exception { if (!downloadLocation.exists()) downloadLocation.mkdirs(); if (!incompleteLocation.exists()) incompleteLocation.mkdirs(); + if (!dropBoxLocation.exists()) + dropBoxLocation.mkdirs(); MuWireSettings settings = new MuWireSettings(); settings.setNickname(nickname); @@ -128,6 +142,13 @@ public class MuWireClient { PrintWriter pw = new PrintWriter(mwProps, "UTF-8"); settings.write(pw); pw.close(); + + WebUISettings webSettings = new WebUISettings(); + webSettings.setDropBoxLocation(dropBoxLocation); + + pw = new PrintWriter(webUIProps, "UTF-8"); + webSettings.write(pw); + pw.close(); } public Core getCore() { diff --git a/webui/src/main/java/com/muwire/webui/WebUISettings.java b/webui/src/main/java/com/muwire/webui/WebUISettings.java new file mode 100644 index 00000000..f53d8685 --- /dev/null +++ b/webui/src/main/java/com/muwire/webui/WebUISettings.java @@ -0,0 +1,36 @@ +package com.muwire.webui; + +import java.io.File; +import java.io.IOException; +import java.io.Writer; +import java.util.Properties; + +public class WebUISettings extends Util { + + private File dropBoxLocation; + + + public File getDropBoxLocation() { + return dropBoxLocation; + } + + public void setDropBoxLocation(File dropBoxLocation) { + this.dropBoxLocation = dropBoxLocation; + } + + WebUISettings() { + this(new Properties()); + } + + WebUISettings(Properties props) { + dropBoxLocation = new File(props.getProperty("dropBoxLocation", System.getProperty("user.home"))); + } + + void write(Writer out) throws IOException { + Properties props = new Properties(); + + props.setProperty("dropBoxLocation", dropBoxLocation.getAbsolutePath()); + + props.store(out, "This file is UTF-8"); + } +} diff --git a/webui/src/main/webapp/ConfigurationPage.jsp b/webui/src/main/webapp/ConfigurationPage.jsp index efea36e6..abe7b555 100644 --- a/webui/src/main/webapp/ConfigurationPage.jsp +++ b/webui/src/main/webapp/ConfigurationPage.jsp @@ -14,6 +14,7 @@ String pagetitle=Util._t("Configuration"); String helptext = Util._t("Use this page to change MuWire options."); +WebUISettings webSettings = (WebUISettings) application.getAttribute("webSettings"); Core core = (Core) application.getAttribute("core"); String inboundLength = core.getI2pOptions().getProperty("inbound.length"); @@ -127,6 +128,13 @@ Exception error = (Exception) application.getAttribute("MWConfigError");

<%=Util._t("Sharing")%>

+ + + +
<%=Util._t("Drop Box directory")%> + <%=Util._t("Where to store files you share with drag and drop")%> +
+

<%=Util._t("Share downloaded files")%> <%=Util._t("Automatically share files you have downloaded with MuWire")%> diff --git a/webui/src/main/webapp/MuWire.jsp b/webui/src/main/webapp/MuWire.jsp index 23b66126..75d88b21 100644 --- a/webui/src/main/webapp/MuWire.jsp +++ b/webui/src/main/webapp/MuWire.jsp @@ -30,8 +30,10 @@ <% String defaultDownloadLocation = System.getProperty("user.home")+File.separator+"Downloads"; String defaultIncompletesLocation = System.getProperty("user.home") + File.separator+"MuWire Incompletes"; + String defaultDropBoxLocation = System.getProperty("user.home") + File.separator + "MuWire DropBox"; session.setAttribute("defaultDownloadLocation",defaultDownloadLocation); session.setAttribute("defaultIncompletesLocation",defaultIncompletesLocation); + session.setAttribute("defaultDropBoxLocation",defaultDropBoxLocation); Throwable error = (Throwable) application.getAttribute("MWInitError"); %> @@ -55,6 +57,8 @@
<%=Util._t("Directory for storing incomplete files")%>:

+ <%=Util._t("Drop Box for files you share with MuWire")%>:
+
">