-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a08b0a1
commit a72e592
Showing
6 changed files
with
130 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using Microsoft.Extensions.Options; | ||
using SimpleCDN.Configuration; | ||
using System.Text; | ||
|
||
namespace SimpleCDN.Helpers | ||
{ | ||
public class IndexGenerator(IOptionsMonitor<CDNConfiguration> options) | ||
{ | ||
private readonly IOptionsMonitor<CDNConfiguration> _options = options; | ||
|
||
public byte[]? GenerateIndex(string absolutePath, string rootRelativePath) | ||
{ | ||
if (!Directory.Exists(absolutePath)) | ||
{ | ||
return null; | ||
} | ||
|
||
var directory = new DirectoryInfo(absolutePath); | ||
|
||
var index = new StringBuilder(); | ||
|
||
index.AppendFormat( | ||
""" | ||
<html> | ||
<head> | ||
<meta name="robots" content="noindex,nofollow"> | ||
<link rel="stylesheet" href="/_cdn/styles.css"> | ||
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0"> | ||
</head> | ||
<body> | ||
<header> | ||
<h1>Index of {0}</h1> | ||
</header> | ||
<main> | ||
<table> | ||
<thead><tr> | ||
<th>Name</th> | ||
<th>Size</th> | ||
<th>Last Modified (UTC)</th> | ||
</tr></thead> | ||
<tbody> | ||
""", rootRelativePath.Replace("/", "<wbr>/")); | ||
|
||
if (rootRelativePath is not "/" and not "" && directory.Parent is DirectoryInfo parent) | ||
{ | ||
var lastSlashIndex = rootRelativePath.LastIndexOf('/'); | ||
|
||
string parentRootRelativePath; | ||
|
||
if (lastSlashIndex is < 1) | ||
{ | ||
parentRootRelativePath = "/"; | ||
} else | ||
{ | ||
parentRootRelativePath = rootRelativePath[..lastSlashIndex]; | ||
} | ||
|
||
AppendRow(index, parentRootRelativePath, "Parent Directory", -1, parent.LastWriteTimeUtc); | ||
} | ||
|
||
foreach (var subDirectory in directory.EnumerateDirectories()) | ||
{ | ||
var name = subDirectory.Name; | ||
|
||
AppendRow(index, Path.Combine(rootRelativePath, name), name, -1, subDirectory.LastWriteTimeUtc); | ||
} | ||
|
||
foreach (var file in directory.EnumerateFiles()) | ||
{ | ||
var name = file.Name; | ||
|
||
AppendRow(index, Path.Combine(rootRelativePath, name), name, file.Length, file.LastWriteTimeUtc); | ||
} | ||
|
||
index.AppendFormat("</tbody></table></main><footer>{0}</footer></body></html>", _options.CurrentValue.Footer); | ||
|
||
var bytes = Encoding.UTF8.GetBytes(index.ToString()); | ||
|
||
return bytes; | ||
} | ||
|
||
private static void AppendRow(StringBuilder index, string href, string name, long size, DateTimeOffset lastModified) | ||
{ | ||
index.AppendFormat("""<tr><td><a href="{0}">{1}</a></td>""", href, name); | ||
index.AppendFormat("""<td>{0}</td>""", size < 0 ? "-" : size.FormatByteCount()); | ||
index.AppendFormat("""<td>{0}</td></tr>""", lastModified.ToString("dd/MM/yyyy HH:mm")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,54 @@ | ||
@font-face { | ||
font-family: Code; | ||
font-display: swap; | ||
src: local("Cascadia Code"),url(/CascadiaCode/woff2/CascadiaCode.woff2) format("woff2"),url(/CascadiaCode/ttf/CascadiaCode.ttf) format("truetype"),url(/CascadiaCode/CascadiaCode.ttf) format("truetype"),url(/CascadiaCode/otf/static/CascadiaCodeNF-Regular.otf) format("opentype"),local("Cascadia Mono"),monospace | ||
src: local("Cascadia Code"),url(/CascadiaCode/woff2/CascadiaCode.woff2) format("woff2"),url(/CascadiaCode/ttf/CascadiaCode.ttf) format("truetype"),url(/CascadiaCode/CascadiaCode.ttf) format("truetype"),url(/CascadiaCode/otf/static/CascadiaCodeNF-Regular.otf) format("opentype"),local("Cascadia Mono"),monospace; | ||
} | ||
|
||
:root { | ||
font-family: Code; | ||
} | ||
|
||
* { | ||
box-sizing: border-box; | ||
} | ||
|
||
body { | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: space-between; | ||
min-height: 100vh; | ||
margin: 0; | ||
padding: 0; | ||
padding-block: 5px; | ||
max-width: 100vw; | ||
overflow-x: hidden; | ||
} | ||
|
||
main { | ||
overflow: auto; | ||
} | ||
|
||
table { | ||
margin: auto; | ||
overflow: auto; | ||
} | ||
|
||
h1 { | ||
text-align: center; | ||
overflow: auto; | ||
padding-inline: 5px; | ||
|
||
} | ||
|
||
th, td { | ||
padding: .2em 1em; | ||
} | ||
|
||
table > thead > tr > th { | ||
thead th { | ||
font-size: 1.2rem; | ||
border-bottom: 1px solid black; | ||
} | ||
|
||
footer { | ||
text-align: center; | ||
} |