Skip to content

Commit

Permalink
Complete directory browsing feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdika2 committed Jan 12, 2025
1 parent 6f93d8f commit 5f0bbe2
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 65 deletions.
28 changes: 12 additions & 16 deletions Core/RequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ static void HandleContext(HttpRequest request, HttpResponse response)
// Default pages
bool defaultPageFound = false;
if (request.Uri.AbsolutePath.Trim('/') == "")
{
foreach (var defaultPage in config.DefaultPages)
if (File.Exists(Path.Combine(wwwappPath, defaultPage)))
{
Expand All @@ -126,13 +125,6 @@ static void HandleContext(HttpRequest request, HttpResponse response)
request.Items["R_URI"] = uriBuilder.Uri;
defaultPageFound = true;
}
if (!defaultPageFound)
{
response.StatusCode = 404;
LastError = new PageNotFoundException($"Default page file not exists!");
return;
}
}

// Http modules first
var httpModules = config.HttpModules;
Expand Down Expand Up @@ -178,10 +170,14 @@ static void HandleContext(HttpRequest request, HttpResponse response)
return;
}

string path = request.Uri.AbsolutePath.TrimEnd('/');
if (path == "") path = "/";
string rows = CreateDirectoryBrowsintTable(filename, request.Uri.AbsolutePath);
response.ResponseStream.Write(Encoding.UTF8.GetBytes(Resources.DirectoryBrowsing.Replace("{Rows}", rows)
.Replace("{Directory}", request.Uri.AbsolutePath).Replace("{ParentDirectory}"
, Path.GetDirectoryName(request.Uri.AbsolutePath).Replace('\\', '/'))));
response.ResponseStream.Write(Encoding.UTF8.GetBytes(Resources.DirectoryBrowsing
.Replace("{Rows}", rows)
.Replace("{Domain}", request.Uri.Host).TrimEnd('/').Replace("{Directory}", path)
.Replace("{ParentDirectory}", path == "/" ?
"" : Resources.DirectoryBrowsingParentDirectory.Replace("{ParentDirectory}", Path.GetDirectoryName(path)?.Replace('\\', '/')))));

return;
}
Expand Down Expand Up @@ -299,26 +295,26 @@ private static string CreateDirectoryBrowsintTable(string filename,string path)
StringBuilder sb = new StringBuilder();

string[] directories = Directory.GetDirectories(filename);
foreach (var directory in directories)
foreach (var directory in directories.Where(dir => !Path.GetFileName(dir).StartsWith(".")))
{
DirectoryInfo info = new DirectoryInfo(directory);
string name = Path.GetFileName(directory);
sb.AppendLine(@$"<tr>
<td><img src=""/icons/folder.gif"" alt=""[DIR]""></td>
<td><a href=""{(path == "/" ? "" : path + "/")}{name}"">{name}/</a></td>
<td><a href=""{(path.EndsWith("/") ? "" : path + "/")}{name}"">{name}/</a></td>
<td>{info.LastWriteTime.ToString("yyyy/MM/dd hh:mm")}</td>
<td>-</td>
</tr>");
}

string[] files = Directory.GetFiles(filename);
foreach(var file in files)
foreach(var file in files.Where(file => !Path.GetFileName(file).StartsWith(".")))
{
FileInfo info = new FileInfo(file);
string name = Path.GetFileName(file);
sb.AppendLine(@$"<tr>
<td><img src=""/icons/layout.gif"" alt=""[DIR]""></td>
<td><a href=""{(path == "/" ? "" : path + "/")}{name}"">{name}</a></td>
<td><img src=""/icons/layout.gif"" alt=""[FILE]""></td>
<td><a href=""{(path.EndsWith("/") ? path : path + "/")}{name}"">{name}</a></td>
<td>{info.LastWriteTime.ToString("yyyy/MM/dd hh:mm")}</td>
<td>{FormatSize(info.Length)}</td>
</tr>");
Expand Down
51 changes: 33 additions & 18 deletions Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 40 additions & 28 deletions Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -189,41 +189,53 @@
<data name="DirectoryBrowsing" xml:space="preserve">
<value>&lt;!DOCTYPE HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;head&gt;
&lt;title&gt;Directory {Directory}&lt;/title&gt;
&lt;style&gt;
td:not(:first-child){
padding-left: 1rem;
padding-right: 1rem;
table {
border-collapse: collapse;
}
td:not(:first-child) {
padding-left: 1rem;
padding-right: 1rem;
}
th {
text-align: left;
padding-left: 1rem;
padding-right: 1rem;
border-bottom: 2px solid gray;
background-color: #f2f2f2;
text-align: left;
padding-left: 1rem;
padding-right: 1rem;
border-bottom: 2px solid gray;
}
th,
td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Directory {Directory}&lt;/h1&gt;
&lt;table&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;{Domain} - {Directory}&lt;/h1&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Last modified&lt;/th&gt;
&lt;th&gt;Size&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;img src="/icons/back.gif" alt="[PARENTDIR]"&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="{ParentDirectory}"&gt;Parent Directory&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
{Rows}
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Last modified&lt;/th&gt;
&lt;th&gt;Size&lt;/th&gt;
&lt;/tr&gt;{ParentDirectory}
{Rows}
&lt;/tbody&gt;
&lt;/table&gt;

&lt;/body&gt;&lt;/html&gt;</value>
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;</value>
</data>
<data name="DirectoryBrowsingParentDirectory" xml:space="preserve">
<value>&lt;tr&gt;
&lt;td&gt;&lt;img src="/icons/back.gif" alt="[BACK]"&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="{ParentDirectory}"&gt;Parent Directory&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;</value>
</data>
</root>
13 changes: 10 additions & 3 deletions Settings/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class AppConfig
private const string _filename = "appconfig.yml";
private static FileSystemWatcher _fileWatcher;

public string BaseDirectory { get; set; }
public string[] DefaultPages { get; set; }
public string BaseDirectory { get; set; }
public string[] DefaultPages { get; set; }
public bool DirectoryBrowsing { get; set; }
public bool ExtentionRequired { get; set; }
public bool NotExtentionInUrl { get; set; }
Expand Down Expand Up @@ -43,7 +43,14 @@ public static void LoadConfigs()
if (!File.Exists(_filename))
throw new FileNotFoundException(_filename + " config file not found!");

_instance = ConfigParser.ParseYaml(File.ReadAllText(_filename));
try
{
_instance = ConfigParser.ParseYaml(File.ReadAllText(_filename));
}
catch (Exception ex)
{
Program.Log("&r[&4Error&r] Faild to reload config file: " + ex.Message);
}
}

public static void StartConfigWatcher()
Expand Down
2 changes: 2 additions & 0 deletions Settings/ConfigParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public static AppConfig ParseYaml(string yamlContent)

if (config.ExtentionRequired)
config.NotExtentionInUrl = false;
if (config.DefaultPages == null)
config.DefaultPages = new string[0];
if (config.Routes == null)
config.Routes = new Dictionary<string, Route>();
if (config.ErrorPages == null)
Expand Down

0 comments on commit 5f0bbe2

Please sign in to comment.