Skip to content

Commit d4e87da

Browse files
committed
Fix TemplaterServer example for .NET
File detection should be case insensitive, as otherwise it only works on Windows. Cache files at startup instead of probing them later... as that takes care of potential file access security problems.
1 parent 6d05bdc commit d4e87da

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

Advanced/TemplaterServer/src/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void ConfigureServices(IServiceCollection services)
2222
policy =>
2323
{
2424
policy
25-
.WithOrigins("http://localhost:3000", "http://localhost:3001")
25+
.WithOrigins("http://localhost:5000", "http://localhost:5001")
2626
.AllowAnyHeader()
2727
.AllowAnyMethod();
2828
});

Advanced/TemplaterServer/src/TemplaterController.cs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,17 @@ static TemplaterController()
2929
}
3030

3131
private readonly SharedResource SharedResource;
32+
private readonly Dictionary<string, FileInfo> ResourceFiles = new Dictionary<string, FileInfo>();
3233

3334
public TemplaterController(SharedResource sharedResource)
3435
{
3536
this.SharedResource = sharedResource;
37+
var resourcesPath = new DirectoryInfo("resources");
38+
foreach(var d in resourcesPath.GetDirectories())
39+
{
40+
foreach (var f in d.GetFiles())
41+
ResourceFiles[d.Name.ToLowerInvariant() + "/" + f.Name.ToLowerInvariant()] = f;
42+
}
3643
}
3744

3845
static object JavaFormat(object value, string metadata)
@@ -74,36 +81,41 @@ public IActionResult Nested([FromQuery]string template)
7481
[HttpGet("static/{file}")]
7582
public IActionResult Static(string file)
7683
{
77-
var fi = new FileInfo("resources/static/" + file);
78-
if (!fi.Exists) return NotFound();
84+
var name = "static/" + file.ToLowerInvariant().Trim();
85+
FileInfo fi;
86+
if (!ResourceFiles.TryGetValue(name, out fi) || !fi.Exists) return NotFound();
7987
return PhysicalFile(fi.FullName, file.EndsWith(".css") ? "text/css" : "application/javascript");
8088
}
8189

8290
[HttpGet("js/{file}")]
8391
public IActionResult JS(string file)
84-
{
85-
var fi = new FileInfo("resources/js/" + file);
86-
if (!fi.Exists) return NotFound();
92+
{
93+
94+
var name = "js/" + file.ToLowerInvariant().Trim();
95+
FileInfo fi;
96+
if (!ResourceFiles.TryGetValue(name, out fi) || !fi.Exists) return NotFound();
8797
return PhysicalFile(fi.FullName, "application/javascript");
8898
}
8999

90100
[HttpGet("examples/{file}")]
91101
public IActionResult Examples(string file)
92-
{
93-
var fi = new FileInfo("resources/examples/" + file);
94-
if (!fi.Exists) return NotFound();
102+
{
103+
var name = "examples/" + file.ToLowerInvariant().Trim();
104+
FileInfo fi;
105+
if (!ResourceFiles.TryGetValue(name, out fi) || !fi.Exists) return NotFound();
95106
return PhysicalFile(fi.FullName, "text/json");
96107
}
97108

98109
[HttpGet("templates/{file}")]
99110
public IActionResult Templates(string file, [FromQuery] string withSchema)
100-
{
101-
var fi = new FileInfo("resources/templates/" + file);
102-
if (!fi.Exists) return NotFound();
111+
{
112+
var templateName = "templates/" + file.ToLowerInvariant().Trim();
113+
var jsonName = "examples/" + file.ToLowerInvariant().Trim() + ".json";
114+
FileInfo fi, jfi;
115+
if (!ResourceFiles.TryGetValue(templateName, out fi) || !fi.Exists) return NotFound();
116+
if (!ResourceFiles.TryGetValue(jsonName, out jfi) || !jfi.Exists) return NotFound();
103117
if ("true".Equals(withSchema, StringComparison.OrdinalIgnoreCase))
104118
{
105-
var jfi = new FileInfo("resources/examples/" + file + ".json");
106-
if (!jfi.Exists) return NotFound();
107119
string jsonString = System.IO.File.ReadAllText(jfi.FullName);
108120
MemoryStream ms;
109121
try
@@ -163,9 +175,10 @@ private MemoryStream Process(FileInfo fi, string argument, bool asSchema, bool d
163175
[HttpPost("process")]
164176
public IActionResult Post(Argument arg)
165177
{
166-
if (arg == null) return NotFound();
167-
var fi = new FileInfo("resources/templates/" + arg.template);
168-
if (!fi.Exists) return NotFound();
178+
if (arg == null || string.IsNullOrEmpty(arg.template)) return NotFound();
179+
var name = "templates/" + arg.template.ToLowerInvariant().Trim();
180+
FileInfo fi;
181+
if (!ResourceFiles.TryGetValue(name, out fi) || !fi.Exists) return NotFound();
169182
MemoryStream ms;
170183
try
171184
{

0 commit comments

Comments
 (0)