-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMarkdownFileMiddleware.cs
More file actions
160 lines (154 loc) · 6.81 KB
/
MarkdownFileMiddleware.cs
File metadata and controls
160 lines (154 loc) · 6.81 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* ===============================================
* 功能描述:AspNetCore.FileLog.MarkdownFileMiddleware
* 创 建 者:WeiGe
* 创建日期:1/12/2019 8:33:38 PM
* ===============================================*/
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.FileProviders;
using Microsoft.Net.Http.Headers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
namespace AspNetCore.FileLog
{
internal class MarkdownFileMiddleware
{
internal const string MarkdownHead = "|时间|消息|请求|错误|跟踪|\r\n|--|--|--|--|--|\r\n";
private readonly RequestDelegate _next;
static readonly Assembly _assembly = typeof(MarkdownFileMiddleware).Assembly;
const string cerulean = "AspNetCore.FileLog.markdown.cerulean.min.css";
const string font = "AspNetCore.FileLog.markdown.font.woff2";
const string strapdownCss = "AspNetCore.FileLog.markdown.strapdown.min.css";
const string strapdownJs = "AspNetCore.FileLog.markdown.strapdown.min.js";
const string markdownHtml = "AspNetCore.FileLog.markdown.markdown.html";
const string strapdown = "strapdown";
static string markdownHtmlContent;
readonly string _fileDirectory;
private static readonly HtmlEncoder _htmlEncoder= HtmlEncoder.Default;
internal static void SaveResourceFiles(string path)
{
if (!Directory.Exists(Path.Combine(path, strapdown)))
{
Directory.CreateDirectory(Path.Combine(path, strapdown));
}
var file = Path.Combine(path, strapdown, "cerulean.min.css");
if (!File.Exists(file))
{
using (StreamReader sr = new StreamReader(_assembly.GetManifestResourceStream(cerulean)))
{
File.WriteAllText(file, sr.ReadToEnd());
}
}
file = Path.Combine(path, strapdown, "font.woff2");
if (!File.Exists(file))
{
using (StreamReader sr = new StreamReader(_assembly.GetManifestResourceStream(font)))
{
File.WriteAllText(file, sr.ReadToEnd());
}
}
file = Path.Combine(path, strapdown, "strapdown.css");
if (!File.Exists(file))
{
using (StreamReader sr = new StreamReader(_assembly.GetManifestResourceStream(strapdownCss)))
{
File.WriteAllText(file, sr.ReadToEnd());
}
}
file = Path.Combine(path, strapdown, "strapdown.js");
if (!File.Exists(file))
{
using (StreamReader sr = new StreamReader(_assembly.GetManifestResourceStream(strapdownJs)))
{
File.WriteAllText(file, sr.ReadToEnd());
}
}
if (string.IsNullOrEmpty(markdownHtmlContent))
{
using (StreamReader sr = new StreamReader(_assembly.GetManifestResourceStream(markdownHtml)))
{
markdownHtmlContent = sr.ReadToEnd();
}
}
}
public MarkdownFileMiddleware(RequestDelegate next, string fileDirectory)
{
_next = next;
_fileDirectory = fileDirectory;
}
public async Task Invoke(HttpContext context)
{
context.Response.ContentType = "text/html; charset=utf-8";
var _path = context.Request.Path.ToString().Replace(LoggerSettings.LogRequestPath, "");
var file = new FileInfo(Path.Combine(_fileDirectory, _path.TrimStart('\\','/')));
context.Response.Headers.Add(HeaderNames.AcceptRanges, "bytes");
EntityTagHeaderValue _tag = null;
if (file.Exists)
{
var _lastModified = file.LastAccessTime.ToUniversalTime();
long value = _lastModified.ToFileTime() ^ file.Length;
_tag = EntityTagHeaderValue.Parse($"\"{ Convert.ToString(value, 16)}\"");
if (context.Request.Headers.Keys.Contains("If-None-Match"))
{
var tag = context.Request.Headers["If-None-Match"].ToString();
if (tag == _tag.Tag)
{
context.Response.StatusCode = 304;
await Task.CompletedTask;
return;
}
}
if (_tag != null)
{
var _responseHeaders = context.Response.GetTypedHeaders();
_responseHeaders.LastModified = file.LastAccessTime;
_responseHeaders.ETag = _tag;
_responseHeaders.CacheControl = new CacheControlHeaderValue
{
MaxAge = TimeSpan.FromHours(2),
//Public = true,
};
}
string fileContent;
using (var stream = file.OpenRead())
{
using (StreamReader reader = new StreamReader(stream))
{
fileContent = reader.ReadToEnd();
}
}
StringBuilder html = new StringBuilder(markdownHtmlContent);
var pathString = context.Request.Path;
string text = "/";
string[] array = pathString.Value.Split(new char[1]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);
//var _paths = array.Take(array.Length - 1).ToArray();
StringBuilder stringBuilder = new StringBuilder($"<a href='{LoggerSettings.SettingsPath}'>Settings</a> ");
foreach (string text2 in array)
{
text =string.Format("{0}{1}/", text , text2);
if (text.TrimEnd('/') == context.Request.Path)
{
stringBuilder.AppendFormat("<a href='javascript:;'>{0}</a>", _htmlEncoder.Encode(text2));
}
else
{
stringBuilder.AppendFormat("<a href=\"{0}\">{1}/</a>", _htmlEncoder.Encode(text), _htmlEncoder.Encode(text2));
}
}
html.Replace("{{content}}",MarkdownHead+fileContent)
.Replace("{{title}}", string.Join("/", array.Take(array.Length - 1)))
.Replace("{{link}}", stringBuilder.ToString());
context.Response.StatusCode = 200;
await context.Response.WriteAsync(html.ToString(), Encoding.UTF8);
}
}
}
}