-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cs
132 lines (120 loc) · 4.81 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Text;
namespace Microcosm
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// Empty
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Use Directory Browser?
string useDirectoryBrowser = Environment.GetEnvironmentVariable("UseDirectoryBrowser");
useDirectoryBrowser = useDirectoryBrowser?.ToLower();
if (useDirectoryBrowser == "true")
{
app.UseDirectoryBrowser();
}
// Use Developer Exception Page
app.UseDeveloperExceptionPage();
// Use Routing
app.UseRouting();
// Use Default Files
app.UseDefaultFiles();
// Use Static Files
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = ContentLog.Log
});
// Use Endpoints
app.UseEndpoints(endpoints =>
{
// Portal UI
endpoints.Map("/portal", async context =>
{
// Html
string html = await File.ReadAllTextAsync("Portal.html");
// Message
string message = string.Empty;
if (context.Request.Query.ContainsKey("message"))
{
message = context.Request.Query["message"];
message = WebUtility.HtmlEncode(message);
html = html.Replace("{{message}}", message);
}
else
{
html = html.Replace("{{message}}", string.Empty);
}
// Log
StringBuilder logBuilder = new StringBuilder();
if (ContextHelper.AuthForm(context))
{
logBuilder.AppendLine($"<p>Uptime: {ContentLog.GetUptime()}</p>");
logBuilder.AppendLine($"<p>Count: {ContentLog.Lines.Count}</p>");
foreach (string line in ContentLog.Lines)
{
logBuilder.AppendLine($"<p>{line}</p>");
}
}
html = html.Replace("{{log}}", logBuilder.ToString());
// Response
await context.Response.WriteAsync(html);
});
// Portal Content POST
endpoints.MapPost("/portal/content", async context =>
{
// Auth
if (!ContextHelper.AuthForm(context))
{
ContextHelper.Deny(context, "/portal");
return;
}
// Run
IFormFile zipFile = context.Request.Form.Files["zip"];
if (zipFile != null)
{
if (!Directory.Exists("wwwroot"))
{
Directory.CreateDirectory("wwwroot");
}
if (context.Request.Form.ContainsKey("replace"))
{
DirectoryInfo directory = new DirectoryInfo("wwwroot");
foreach (FileInfo file in directory.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in directory.GetDirectories())
{
dir.Delete(true);
}
}
using (FileStream fileStream = File.Create("wwwroot.zip"))
{
await zipFile.CopyToAsync(fileStream);
}
ZipFile.ExtractToDirectory("wwwroot.zip", "wwwroot", true);
ContextHelper.Redirect(context, "/portal", "Success!");
return;
}
else
{
ContextHelper.Redirect(context, "/portal", "No Zip File");
return;
}
});
});
}
}
}