Skip to content

Commit

Permalink
Updated Webengine
Browse files Browse the repository at this point in the history
  • Loading branch information
JWLMT88 committed Jul 11, 2023
1 parent 24f5203 commit c1a4eef
Show file tree
Hide file tree
Showing 321 changed files with 208,459 additions and 157 deletions.
9 changes: 9 additions & 0 deletions srvlocal/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Windows.Forms;
using srvlocal.Api;
using srvlocal.error_handling;
using srvlocal.data_handling;

namespace Local
{
Expand Down Expand Up @@ -433,6 +434,14 @@ public void HandelRequest()
response.ContentLength64 = buffer.Length;
response.OutputStream.Write(buffer, 0, buffer.Length);
}
if (request.Url.AbsolutePath == "/api/resources/data")
{
AddFiles.JSONDATAHANDLER.ProcessRequest(context);
}
if (request.Url.AbsolutePath == "/api/subdirectories")
{
srvlocal.data_handling.DirectoryController.GetSubdirectories(context);
}
if (request.Url.AbsolutePath == "/api/login")
{
var indexHtml = srvlocal.auto_generators.GenerateLoginHtml.Instance().v1();
Expand Down
96 changes: 45 additions & 51 deletions srvlocal/auto_generator/GenerateIndexHtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,54 +34,46 @@ public string v1(string reqDirectory)
var sb = new StringBuilder();
sb.Append("<html>");
sb.Append("<head>");
sb.Append("<meta charset=\"UTF-8\">");
sb.Append("<title>Index of Files</title>");
sb.Append("<link rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/favlogo.png\">");

// Add modern CSS styling
sb.Append("<style>");
sb.Append("body {");
sb.Append(" font-family: 'Roboto', sans-serif;");
sb.Append(" background-color: #fafafa;");
sb.Append("}");
sb.Append("h1 {");
sb.Append(" font-size: 2rem;");
sb.Append(" margin-top: 2rem;");
sb.Append(" margin-bottom: 1rem;");
sb.Append("}");
sb.Append("table {");
sb.Append(" border-collapse: collapse;");
sb.Append(" width: 100%;");
sb.Append("}");
sb.Append("table th, table td {");
sb.Append(" border: 1px solid #ddd;");
sb.Append(" padding: 8px;");
sb.Append(" text-align: left;");
sb.Append("}");
sb.Append("table th {");
sb.Append(" background-color: #f2f2f2;");
sb.Append("}");
sb.Append(".download-link {");
sb.Append(" background-color: #4CAF50;");
sb.Append(" border: none;");
sb.Append(" color: white;");
sb.Append(" padding: 8px 16px;");
sb.Append(" text-align: center;");
sb.Append(" text-decoration: none;");
sb.Append(" display: inline-block;");
sb.Append(" font-size: 14px;");
sb.Append(" margin-right: 8px;");
sb.Append(" border-radius: 4px;");
sb.Append(" cursor: pointer;");
sb.Append("}");
sb.Append(".download-link:hover {");
sb.Append(" background-color: #3e8e41;");
sb.Append("}");
sb.Append("</style>");

sb.Append("<link rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/images/favlogo.png\">");
sb.Append("<link rel=\"stylesheet\" href=\"/css/styles.css\">");
sb.Append("<link rel=\"stylesheet\" href=\"/css/winui-style.css\">");
sb.Append("<link href=\"https://cdn.jsdelivr.net/npm/bootstrap-icons@1.6.0/font/bootstrap-icons.css\" rel=\"stylesheet\">");
sb.Append("</head>");
sb.Append("<body>");
sb.Append($"<h1>Index of Directory : {reqDirectory.Replace("C:\\LILO\\", "")}</h1>");
sb.Append("<table>");
sb.Append($"<header class=\"header\">\r\n " +
$"<h1>srvlocal index " +
$" <small class=\"app-name-subtitle\"> ({reqDirectory.Replace("C:\\LILO\\", "")})\r\n " +
$" </small>" +
$"</h1>\r\n <div class=\"header-right\">\r\n <div class=\"search-bar\">\r\n <input type=\"text\" id=\"searchInput\" placeholder=\"Search files...\">\r\n <button id=\"searchButton\"><i class=\"bi bi-search\"></i></button>\r\n </div> " +
$"<div>\r\n " +
$" <button class=\"button secondary\" id=\"addFileButton\"><i class=\"bi bi-plus\"></i></button>\r\n <button class=\"button secondary\"><i class=\"bi bi-three-dots-vertical\"></i></button>\r\n" +

$"</div> " +
$"</header>");
sb.Append("<div class=\"content\">");
sb.Append("<nav>");
sb.Append("<ul class=\"breadcrumb\">");
sb.Append("<li><a href=\"/\">Home</a></li>");

var currentDirectory = new DirectoryInfo(reqDirectory);
var directories = currentDirectory.FullName.Split(Path.DirectorySeparatorChar);

string path = string.Empty;
foreach (var directory in directories)
{
if (!string.IsNullOrEmpty(directory))
{
path += directory + Path.DirectorySeparatorChar;
sb.Append($"<li><a href=\"/{path}\">{directory}</a></li>");
}
}

sb.Append("</ul>");
sb.Append("</nav>");

sb.Append("<table class=\"table\">");
sb.Append("<thead>");
sb.Append("<tr>");
sb.Append("<th>Name</th>");
Expand All @@ -92,13 +84,12 @@ public string v1(string reqDirectory)
sb.Append("</thead>");
sb.Append("<tbody>");

var currentDirectory = new DirectoryInfo(reqDirectory);
var parentDirectory = currentDirectory.Parent;

if (parentDirectory != null)
{
sb.Append("<tr>");
sb.Append($"<td><a href='../'>../</a></td>");
sb.Append($"<td><a href=\"../\"><i class=\"bi bi-arrow-up\"></i> Parent Directory</a></td>");
sb.Append("<td></td>");
sb.Append("<td></td>");
sb.Append("<td></td>");
Expand All @@ -108,7 +99,7 @@ public string v1(string reqDirectory)
foreach (var directory in currentDirectory.GetDirectories())
{
sb.Append("<tr>");
sb.Append($"<td><a href='{directory.Name}/'>{directory.Name}/</a></td>");
sb.Append($"<td><a href=\"{directory.Name}/\"><i class=\"bi bi-folder\"></i> {directory.Name}/</a></td>");
sb.Append("<td></td>");
sb.Append($"<td>{directory.LastWriteTime}</td>");
sb.Append("<td></td>");
Expand All @@ -118,18 +109,20 @@ public string v1(string reqDirectory)
foreach (var file in currentDirectory.GetFiles())
{
sb.Append("<tr>");
sb.Append($"<td><a href='{file.Name}'>{file.Name}</a></td>");
sb.Append($"<td><a href=\"{file.Name}\"><i class=\"bi bi-file-binary-fill\"></i> {file.Name}</a></td>");
sb.Append($"<td>{GetSizeString(file.Length)}</td>");
sb.Append($"<td>{file.LastWriteTime}</td>");
sb.Append("<td>");
sb.Append($"<a class='download-link' href='{file.Name}' download>Download</a>");
sb.Append($"<a class=\"download-link\" href=\"{file.Name}\" download><i class=\"bi bi-download\"></i> Download</a>");
sb.Append("</td>");
sb.Append("</tr>");
}

sb.Append("</tbody>");
sb.Append("</table>");
sb.Append("</div>");
sb.Append("</body>");
sb.Append("<script src=\"/js/index-action.js\" nonce=\"random-nonce\"></script>");
sb.Append("</html>");
return sb.ToString();
}
Expand All @@ -145,6 +138,7 @@ private string GetSizeString(long size)
}
return $"{size} {sizes[order]}";
}



}
}
Binary file modified srvlocal/bin/Debug/net7.0-windows/srvlocal.dll
Binary file not shown.
Binary file modified srvlocal/bin/Debug/net7.0-windows/srvlocal.pdb
Binary file not shown.
Binary file modified srvlocal/bin/Debug/srvlocal.1.0.0.nupkg
Binary file not shown.
121 changes: 121 additions & 0 deletions srvlocal/data_handling/AddFiles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Web;

namespace srvlocal.data_handling
{
public class AddFiles
{
public class JSONDATAHANDLER
{
public static void ProcessRequest(HttpListenerContext context)
{
string requestMethod = context.Request.HttpMethod;

if (requestMethod == "POST")
{

using (StreamReader reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding))
{
string requestBody = reader.ReadToEnd();

FileData fileData = Newtonsoft.Json.JsonConvert.DeserializeObject<FileData>(requestBody);

if (!string.IsNullOrEmpty(fileData.FileName) && !string.IsNullOrEmpty(fileData.Location))
{
try
{
string filePath = Path.Combine(fileData.Location, fileData.FileName);
File.WriteAllText(filePath, string.Empty);

context.Response.StatusCode = (int)HttpStatusCode.OK;
string responseString = "File added successfully!";
byte[] buffer = Encoding.UTF8.GetBytes(responseString);
context.Response.ContentLength64 = buffer.Length;
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
}
catch (Exception ex)
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
string responseString = "Failed to add file: " + ex.Message;
byte[] buffer = Encoding.UTF8.GetBytes(responseString);
context.Response.ContentLength64 = buffer.Length;
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
}
}
else
{
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
string responseString = "File name and location are required.";
byte[] buffer = Encoding.UTF8.GetBytes(responseString);
context.Response.ContentLength64 = buffer.Length;
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
}
}
}
else
{
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
context.Response.Close();
}
}

public class FileData
{
public string FileName { get; set; }
public string Location { get; set; }
}
}

public bool IsReusable => false;
}

public class DirectoryController
{
public static void GetSubdirectories(HttpListenerContext context)
{
string directoryParam = context.Request.QueryString["directory"];
string decodedDirectory = HttpUtility.UrlDecode(directoryParam);

try
{
string[] subdirectories = Directory.GetDirectories(decodedDirectory);
string responseJson = GetSubdirectoriesJson(subdirectories);

context.Response.StatusCode = (int)HttpStatusCode.OK;
context.Response.ContentType = "application/json";
byte[] buffer = Encoding.UTF8.GetBytes(responseJson);
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
context.Response.OutputStream.Close();
}
catch (Exception ex)
{
string errorJson = GetErrorJson(ex.Message);

context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.ContentType = "application/json";
byte[] buffer = Encoding.UTF8.GetBytes(errorJson);
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
context.Response.OutputStream.Close();
}
}

private static string GetSubdirectoriesJson(string[] subdirectories)
{
return $"{{ \"subdirectories\": {Newtonsoft.Json.JsonConvert.SerializeObject(subdirectories)} }}";
}

private static string GetErrorJson(string errorMessage)
{
return $"{{ \"error\": \"{errorMessage}\" }}";
}
}
}

Binary file modified srvlocal/obj/Debug/net7.0-windows/ref/srvlocal.dll
Binary file not shown.
Binary file modified srvlocal/obj/Debug/net7.0-windows/refint/srvlocal.dll
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
c4bd85d8495a2cc6afc1a3cd381dc0644c287ab0
4654a5db46084ac836a0274e373df023ed3eb80b
Binary file modified srvlocal/obj/Debug/net7.0-windows/srvlocal.dll
Binary file not shown.
Binary file modified srvlocal/obj/Debug/net7.0-windows/srvlocal.pdb
Binary file not shown.
20 changes: 14 additions & 6 deletions srvlocal_gui/FileViewForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@ public partial class FileViewForm : Form
private string? sourceURL;
private string changedSource;
private bool Success = false;
private bool _startInProtectedMode = true;

public static FileViewForm Instance(string source)
public static FileViewForm Instance(string source, bool startInProtectedMode = true)
{
{
if (_instance == null)
{
_instance = new FileViewForm(source);
_instance = new FileViewForm(source, startInProtectedMode);
}

return _instance;
}
}

private FileViewForm(string source)
private FileViewForm(string source, bool startInProtectedMode)
{
InitializeComponent();
if (source is not null)
Expand Down Expand Up @@ -77,15 +78,22 @@ private void FileView_NavigationCompleted(object sender, Microsoft.Web.WebView2.
{
if (changedSource.EndsWith("api/home"))
{
Success = true;
Form1.Instance.APILoginHandler(true);
if (_startInProtectedMode)
{
Success = true;
Form1.Instance.APILoginHandler(true);
}
}
}
}

private void FileViewForm_FormClosing(object sender, FormClosingEventArgs e)
{
Form1.Instance.APILoginHandler_Closing(Success);
if(_startInProtectedMode)
{
Form1.Instance.APILoginHandler_Closing(Success);
}

_instance = null;
}
}
Expand Down
Loading

0 comments on commit c1a4eef

Please sign in to comment.