@@ -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