diff --git a/out/codeBackground b/out/codeBackground index a2b694f..aafc58b 100644 --- a/out/codeBackground +++ b/out/codeBackground @@ -29,7 +29,7 @@ //Send default directory. CreateRequest(client,"200","OK","testfile.html"); } else { - CreateRequest(client,"200","OK",requestloc.replace("/","")); + CreateRequest(client,"200","OK",requestloc.replaceFirst("/","")); } } } else { diff --git a/sigPlace.java b/sigPlace.java index 037ed9b..3dbc0d9 100644 --- a/sigPlace.java +++ b/sigPlace.java @@ -1,7 +1,6 @@ import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.CopyOption; -import java.nio.file.DirectoryNotEmptyException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -13,15 +12,14 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.regex.Pattern; -import java.util.stream.Collectors; public class sigPlace { final static String ROOTDIR = "sitefiles"; final static String REFDIR = "ref"; final static String OUTDIR = "out"; + final static String DIRECTORYLISTING_FILENAME = "DIRECTORY_LISTING"; static int PORT = 8080; final static HashMap map = new HashMap<>(Map.ofEntries( @@ -108,7 +106,19 @@ public class sigPlace { } }catch (IOException e) { e.printStackTrace(); + System.err.println("Copying files over failed!"); + return; } + + System.out.println("Building directory listings..."); + try { + buildDirectoryListings(); + } catch (IOException e) { + e.printStackTrace(); + System.err.println("Failed to build directory listings!"); + return; + } + System.out.println("Site has been built into the "+OUTDIR+" directory."); ExportCodeFile(); @@ -133,6 +143,21 @@ public class sigPlace { } }); } + static void buildDirectoryListings() + throws IOException { + String startingPath=Paths.get(sigPlace.OUTDIR).toAbsolutePath().toString(); + HashMap> map = new HashMap<>(); + Iterator it = Files.walk(Paths.get(sigPlace.OUTDIR)).iterator(); + map.put("/",new ArrayList()); + while (it.hasNext()) { + Path f = it.next(); + String myKey = f.toAbsolutePath().toString().replace(startingPath,"").replace(f.getFileName().toString(),""); + System.out.println(myKey+","+f); + map.putIfAbsent(myKey,new ArrayList()); + map.get(myKey).add(f); + } + System.out.println(map); + } private static boolean isArticleFile(Path f) { return f.getFileName().toString().contains(".article"); } diff --git a/sigServer.java b/sigServer.java index 6ee96bd..2fcfbde 100644 --- a/sigServer.java +++ b/sigServer.java @@ -45,7 +45,7 @@ public class sigServer { //Send default directory. CreateRequest(client,"200","OK","testfile.html"); } else { - CreateRequest(client,"200","OK",requestloc.replace("/","")); + CreateRequest(client,"200","OK",requestloc.replaceFirst("/","")); } } } else { @@ -83,10 +83,15 @@ public class sigServer { OutputStream clientOutput = client.getOutputStream(); if (statusCode.equals("200")) { if (Files.exists(file)) { - CreateRawRequest(clientOutput,statusCode,statusMsg,Files.probeContentType(file),Files.readAllBytes(file)); - String contentType = Files.probeContentType(file); - if (contentType!=null&&contentType.equals("text/html")) { + if (Files.isDirectory(file)) { + CreateRawRequest(clientOutput,statusCode,statusMsg,"text/html",Files.readAllBytes(Paths.get(sigPlace.OUTDIR,string,sigPlace.DIRECTORYLISTING_FILENAME))); clientOutput.write(("
Webpage generated in "+(System.currentTimeMillis()-startTime)+"ms
\r\n").getBytes()); + } else { + CreateRawRequest(clientOutput,statusCode,statusMsg,Files.probeContentType(file),Files.readAllBytes(file)); + String contentType = Files.probeContentType(file); + if (contentType!=null&&contentType.equals("text/html")) { + clientOutput.write(("
Webpage generated in "+(System.currentTimeMillis()-startTime)+"ms
\r\n").getBytes()); + } } } else { CreateRawRequest(clientOutput,statusCode,statusMsg,"text/html","We're sorry, your webpage is in another castle!".getBytes());