-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathManualLastUpdated.cs
More file actions
47 lines (42 loc) · 1.99 KB
/
Copy pathManualLastUpdated.cs
File metadata and controls
47 lines (42 loc) · 1.99 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
using System.IO;
using System.Linq;
using System.Text;
using RT.Servers;
using RT.Util;
using RT.Util.ExtensionMethods;
namespace KtaneWeb
{
public sealed partial class KtanePropellerModule
{
private HttpResponse ManualLastUpdated(HttpRequest req)
{
var filename = req.Url.Path.UrlUnescape();
if (Path.GetInvalidPathChars().Any(filename.Contains))
return HttpResponse.PlainText($"“{filename}” contains a character not allowed in file names.", HttpStatusCode._400_BadRequest);
var moduleInfoCache = _moduleInfoCache;
lock (moduleInfoCache.ManualsLastModified)
{
if (!moduleInfoCache.ManualsLastModified.TryGetValue(filename, out var result))
{
var htmlFile = new DirectoryInfo(Path.Combine(_config.BaseDir, "HTML")).GetFiles(Path.GetFileNameWithoutExtension(filename) + ".html").Select(fs => fs.FullName).FirstOrDefault();
if (htmlFile == null)
return HttpResponse.PlainText("Manual doesn’t exist.", HttpStatusCode._404_NotFound);
var relativeFilename = Path.GetFileName(htmlFile);
if (filename != "/" + relativeFilename)
return HttpResponse.Redirect(req.Url.WithPath("/" + relativeFilename));
var output = new StringBuilder();
var cmd = new CommandRunner
{
Command = $@"git log -n 1 --format=%cd ""HTML/{relativeFilename}""",
WorkingDirectory = _config.BaseDir
};
cmd.StdoutText += str => output.Append(str);
cmd.StderrText += str => output.Append(str);
cmd.StartAndWait();
result = moduleInfoCache.ManualsLastModified[filename] = output.ToString();
}
return HttpResponse.PlainText(result);
}
}
}
}