Skip to content

Commit

Permalink
Added file manager
Browse files Browse the repository at this point in the history
  • Loading branch information
dries007 committed Oct 12, 2014
1 parent 69c3aa6 commit 11af94d
Show file tree
Hide file tree
Showing 321 changed files with 1,008 additions and 167 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Expand Up @@ -56,6 +56,8 @@ repositories {
dependencies {
compile group: "com.google.code.gson", name: "gson", version: "2.2.4"
compile group: "org.apache.commons", name: "commons-io", version: "1.3.2"
compile group: "org.apache.commons", name: "commons-lang3", version: "3.3.2"
compile group: "commons-codec", name: "commons-codec", version: "1.9"
compile group: "org.freemarker", name: "freemarker", version: "2.3.20"
compile group: "org.apache.logging.log4j", name: "log4j-core", version: "2.0.2"
compile group: "net.lingala.zip4j", name:"zip4j", version: "1.3.2"
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/net/doubledoordev/backend/Main.java
Expand Up @@ -56,6 +56,7 @@
import java.util.HashMap;

import static net.doubledoordev.backend.util.Constants.NAME;
import static net.doubledoordev.backend.util.Settings.SETTINGS;

/**
* @author Dries007
Expand All @@ -75,9 +76,6 @@ public static void main(String[] args) throws Exception
charset.setAccessible(true);
charset.set(null, null);

LOGGER.info("+-------------------------------------------------------+");
LOGGER.info("| ...................... Loading ...................... |");
LOGGER.info("+-------------------------------------------------------+");
LOGGER.info("Finding Java versions...");
//noinspection ResultOfMethodCallIgnored
Constants.JAVAPATH.length();
Expand All @@ -89,9 +87,8 @@ public static void main(String[] args) throws Exception
LOGGER.info("Setting up caching...");
Cache.init();

LOGGER.info("+-------------------------------------------------------+");
LOGGER.info("| Loading done. Press any key to terminate the program. |");
LOGGER.info("+-------------------------------------------------------+");
LOGGER.info("Loading done. Press any key to terminate the program.");
LOGGER.info("Webserver started on " + SETTINGS.hostname + ':' + SETTINGS.port);
// Wait for user input.
try
{
Expand Down
245 changes: 245 additions & 0 deletions src/main/java/net/doubledoordev/backend/server/FileManager.java
@@ -0,0 +1,245 @@
/*
* Unless otherwise specified through the '@author' tag or comments at
* the top of the file or on a specific portion of the code the following license applies:
*
* Copyright (c) 2014, DoubleDoorDevelopment
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* The header specified or the above copyright notice, this list of conditions
* and the following disclaimer below must be displayed at the top of the source code
* of any web page received while using any part of the service this software provides.
*
* The header to be displayed:
* This page was generated by DoubleDoorDevelopment's D3Backend or a derivative thereof.
*
* Neither the name of the project nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package net.doubledoordev.backend.server;

import net.doubledoordev.backend.webserver.NanoHTTPD;
import net.doubledoordev.backend.webserver.SimpleWebServer;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.logging.log4j.util.Strings;
import org.apache.commons.codec.binary.Base64;

import java.io.File;
import java.io.IOException;
import java.util.*;

import static net.doubledoordev.backend.webserver.NanoHTTPD.MIME_PLAINTEXT;
import static net.doubledoordev.backend.webserver.NanoHTTPD.Response.Status.FORBIDDEN;
import static net.doubledoordev.backend.webserver.NanoHTTPD.Response.Status.INTERNAL_ERROR;
import static net.doubledoordev.backend.webserver.NanoHTTPD.Response.Status.OK;

/**
* @author Dries007
*/
@SuppressWarnings("UnusedDeclaration")
public class FileManager
{
private final Server server;
private final File serverFolder;
private final File file;

public FileManager(Server server, String fileString)
{
this.server = server;
this.serverFolder = server.getFolder();
File file = this.serverFolder;

if (Strings.isNotBlank(fileString) && !fileString.equals(serverFolder.getName()))
{
fileString = fileString.replace("..", "");
try
{
file = new File(this.serverFolder, fileString).getCanonicalFile();
}
catch (IOException e)
{
e.printStackTrace();
}
}
this.file = file;
}

public Server getServer()
{
return server;
}

public File getFile()
{
return file;
}

public String getExtension()
{
return getExtension(file);
}

public String getExtension(File file)
{
return FilenameUtils.getExtension(file.getName().toLowerCase());
}

public Collection<File> makeBreadcrumbs()
{
LinkedList<File> list = new LinkedList<>();

File crumb = file;
while (!crumb.equals(serverFolder))
{
list.add(crumb);
crumb = crumb.getParentFile();
}

list.add(serverFolder);

Collections.reverse(list);
return list;
}

public String stripServer(File file)
{
if (file.equals(serverFolder)) return serverFolder.getName();
return file.toString().substring(serverFolder.toString().length() + 1);
}

public boolean canEdit(File file)
{
switch (getExtension(file))
{
case "jar":
case "zip":
case "disabled":
return false;

default:
return true;
}
}

public String getEditor()
{
switch (getExtension())
{
case "jar":
case "zip":
case "disabled":
return null;

case "json":
return "json.ftl";

case "jpg":
case "png":
return "img.ftl";

default:
return "ace.ftl";
}
}

public String getIcon(File file)
{
if (!file.canWrite()) return "lock";
if (file.isDirectory()) return "folder";
switch (getExtension(file))
{
case "html":
case "json":
return "file-code-o";

case "txt":
return "file-text-o";

case "jar":
case "zip":
case "disabled":
return "file-archive-o";

case "jpg":
case "png":
return "file-image-o";

default:
return "file-o";
}
}

public String getFileContentsAsString() throws IOException
{
return StringEscapeUtils.escapeHtml4(FileUtils.readFileToString(file));
}

public String getFileContentsAsBase64() throws IOException
{
return String.format("data:%s;base64,%s", SimpleWebServer.MIME_TYPES.get(getExtension()), Base64.encodeBase64String(FileUtils.readFileToByteArray(file)));
}

public NanoHTTPD.Response set(String contents)
{
if (!file.canWrite()) return new NanoHTTPD.Response(FORBIDDEN, MIME_PLAINTEXT, "File is write protected.");
try
{
FileUtils.writeStringToFile(file, contents);
}
catch (IOException e)
{
return new NanoHTTPD.Response(INTERNAL_ERROR, MIME_PLAINTEXT, e.toString());
}
return new NanoHTTPD.Response(OK, MIME_PLAINTEXT, "OK");
}

public void rename(String newname)
{
file.renameTo(new File(file.getParentFile(), newname));
}

public void makeWritable()
{
file.setWritable(true);
}

public void delete() throws IOException
{
if (file.isDirectory()) FileUtils.deleteDirectory(file);
else file.delete();
}

public void newFile(String name) throws IOException
{
new File(file, name).createNewFile();
}

public void newFolder(String name)
{
new File(file, name).mkdir();
}
}
21 changes: 17 additions & 4 deletions src/main/java/net/doubledoordev/backend/util/Helper.java
Expand Up @@ -40,6 +40,8 @@

package net.doubledoordev.backend.util;

import net.doubledoordev.backend.server.Server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
Expand All @@ -54,6 +56,7 @@
*
* @author Dries007
*/
@SuppressWarnings("UnusedDeclaration")
public class Helper
{
private Helper()
Expand All @@ -67,7 +70,6 @@ private Helper()
* @param port the port to check
* @return true if available
*/
@SuppressWarnings("UnusedDeclaration")
public static boolean isPortAvailable(String hostname, int port)
{
try
Expand Down Expand Up @@ -101,21 +103,32 @@ public static char[] randomCharArray(int length)
/**
* @return set of all MC versions we can grab from mojang
*/
@SuppressWarnings("UnusedDeclaration")
public static Collection<String> getAllMCVersions()
{
return Cache.getMcVersions();
}

@SuppressWarnings("UnusedDeclaration")
public static Collection<String> getForgeNames()
{
return Cache.getForgeNames();
}

@SuppressWarnings("UnusedDeclaration")
public static String getForgeVersionForName(String name)
{
return Cache.getForgeVersionForName(name);
}

public static int getTotalRamUsed()
{
int total = 0;
for (Server server : Settings.SETTINGS.getOnlineServers()) total += server.getRamMax();
return total;
}

public static int getTotalDiskspaceUsed()
{
int total = 0;
for (Server server : Settings.SETTINGS.getServers()) total += server.getDiskspaceUse();
return total;
}
}
9 changes: 8 additions & 1 deletion src/main/java/net/doubledoordev/backend/util/Settings.java
Expand Up @@ -61,7 +61,7 @@
*
* @author Dries007
*/
@SuppressWarnings("ALL")
@SuppressWarnings("UnusedDeclaration")
public class Settings
{
public static final Settings SETTINGS = new Settings();
Expand Down Expand Up @@ -182,6 +182,13 @@ public Collection<Server> getServers()
return servers.values();
}

public Collection<Server> getOnlineServers()
{
HashSet<Server> onlineServers = new HashSet<>();
for (Server server : getServers()) if (server.getOnline()) onlineServers.add(server);
return onlineServers;
}

public Collection<User> getUsers()
{
return users.values();
Expand Down
Expand Up @@ -255,7 +255,7 @@ public static JavaInfo parseJavaVersion()
if (!preferred.is64bits && aJava32.compareTo(preferred) == 1) preferred = aJava32;
}
}
Main.LOGGER.debug("Preferred: " + String.valueOf(preferred));
Main.LOGGER.info("Preferred: " + String.valueOf(preferred));
}

if (preferred != null)
Expand Down
Expand Up @@ -60,7 +60,7 @@ public abstract class SimpleWebServer extends NanoHTTPD
/**
* Hashtable mapping (String)FILENAME_EXTENSION -> (String)MIME_TYPE
*/
private static final Map<String, String> MIME_TYPES = new HashMap<String, String>()
public static final Map<String, String> MIME_TYPES = new HashMap<String, String>()
{{
put("css", "text/css");
put("htm", "text/html");
Expand Down

0 comments on commit 11af94d

Please sign in to comment.