1515using Microsoft . Extensions . Caching . Memory ;
1616using System . Text ;
1717using Microsoft . AspNetCore . Authorization ;
18- using System . Collections . Generic ;
18+ using Microsoft . AspNetCore . Hosting ;
19+ using Microsoft . AspNetCore . Http . Extensions ;
1920
2021namespace jaindb . Controllers
2122{
2223 [ Produces ( "application/json" ) ]
23- public class HomeController : Controller
24+ public class JainDBController : Controller
2425 {
2526 private readonly IConfiguration _config ;
2627 private readonly ILogger _logger ;
2728 private IMemoryCache _cache ;
29+ private readonly IHostingEnvironment _env ;
2830
29- public HomeController ( IConfiguration config , ILogger < HomeController > logger , IMemoryCache memoryCache )
31+ public JainDBController ( IConfiguration config , ILogger < JainDBController > logger , IMemoryCache memoryCache , IHostingEnvironment env )
3032 {
3133 _config = config ;
3234 _logger = logger ;
3335 _cache = memoryCache ;
36+ _env = env ;
3437 jDB . _cache = memoryCache ;
3538 }
3639
37- [ HttpGet ]
38- public ActionResult get ( )
39- {
40- string sVersion = Assembly . GetEntryAssembly ( ) . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) . InformationalVersion ;
41- return Content ( "JainDB (c) 2018 by Roger Zander; Version: " + sVersion ) ;
42- }
40+ // [HttpGet]
41+ // public ActionResult get()
42+ // {
43+ // string sVersion = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
44+ // return Content("JainDB (c) 2018 by Roger Zander; Version: " + sVersion);
45+ // }
4346
4447 [ HttpPost ]
4548 [ Route ( "upload/{Id}" ) ]
@@ -96,32 +99,18 @@ public string GetPS()
9699 return sResult ;
97100 }
98101
99- if ( System . IO . File . Exists ( "/app/wwwroot/ inventory.ps1") )
102+ if ( System . IO . File . Exists ( Path . Combine ( _env . WebRootPath , " inventory.ps1") ) )
100103 {
101- string sFile = System . IO . File . ReadAllText ( "/app/wwwroot/inventory.ps1" ) ;
102- if ( string . IsNullOrEmpty ( Environment . GetEnvironmentVariable ( "WebPort" ) ) )
103- sResult = sFile . Replace ( "%LocalURL%" , Environment . GetEnvironmentVariable ( "localURL" ) ) . Replace ( ":%WebPort%" , "" ) ;
104- else
105- sResult = sFile . Replace ( "%LocalURL%" , Environment . GetEnvironmentVariable ( "localURL" ) ) . Replace ( "%WebPort%" , Environment . GetEnvironmentVariable ( "WebPort" ) ) ;
104+ string sFile = System . IO . File . ReadAllText ( Path . Combine ( _env . WebRootPath , "inventory.ps1" ) ) ;
106105
107- //Cache result in Memory
108- if ( ! string . IsNullOrEmpty ( sResult ) )
109- {
110- var cacheEntryOptions = new MemoryCacheEntryOptions ( ) . SetSlidingExpiration ( TimeSpan . FromSeconds ( 300 ) ) ; //cache ID for 5min
111- _cache . Set ( "GetPS" , sResult , cacheEntryOptions ) ;
112- }
106+ string sLocalURL = Request . GetEncodedUrl ( ) . Replace ( "/getps" , "" ) ;
113107
114- return sResult ;
115- }
108+ //if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WebPort")))
109+ // sResult = sFile.Replace("%LocalURL%", Environment.GetEnvironmentVariable("localURL")).Replace(":%WebPort%", "");
110+ //else
111+ // sResult = sFile.Replace("%LocalURL%", Environment.GetEnvironmentVariable("localURL")).Replace("%WebPort%", Environment.GetEnvironmentVariable("WebPort"));
116112
117- string sCurrDir = System . IO . Directory . GetCurrentDirectory ( ) ;
118- if ( System . IO . File . Exists ( sCurrDir + "/wwwroot/inventory.ps1" ) )
119- {
120- string sFile = System . IO . File . ReadAllText ( sCurrDir + "/wwwroot/inventory.ps1" ) ;
121- if ( string . IsNullOrEmpty ( Environment . GetEnvironmentVariable ( "WebPort" ) ) )
122- sResult = sFile . Replace ( "%LocalURL%" , Environment . GetEnvironmentVariable ( "localURL" ) ) . Replace ( ":%WebPort%" , "" ) ;
123- else
124- sResult = sFile . Replace ( "%LocalURL%" , Environment . GetEnvironmentVariable ( "localURL" ) ) . Replace ( "%WebPort%" , Environment . GetEnvironmentVariable ( "WebPort" ) ) ;
113+ sResult = sFile . Replace ( "%LocalURL%" , sLocalURL ) . Replace ( ":%WebPort%" , "" ) ;
125114
126115 //Cache result in Memory
127116 if ( ! string . IsNullOrEmpty ( sResult ) )
@@ -133,21 +122,40 @@ public string GetPS()
133122 return sResult ;
134123 }
135124
136- try
137- {
138- string sFile2 = System . IO . File . ReadAllText ( "wwwroot/inventory.ps1" ) ;
139- sResult = sFile2 . Replace ( "%LocalURL%" , "http://localhost" ) . Replace ( "%WebPort%" , "5000" ) ;
140-
141- //Cache result in Memory
142- if ( ! string . IsNullOrEmpty ( sResult ) )
143- {
144- var cacheEntryOptions = new MemoryCacheEntryOptions ( ) . SetSlidingExpiration ( TimeSpan . FromSeconds ( 300 ) ) ; //cache ID for 5min
145- _cache . Set ( "GetPS" , sResult , cacheEntryOptions ) ;
146- }
147-
148- return sResult ;
149- }
150- catch { }
125+ //string sCurrDir = System.IO.Directory.GetCurrentDirectory();
126+ //if (System.IO.File.Exists(sCurrDir + "/wwwroot/inventory.ps1"))
127+ //{
128+ // string sFile = System.IO.File.ReadAllText(sCurrDir + "/wwwroot/inventory.ps1");
129+ // if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WebPort")))
130+ // sResult = sFile.Replace("%LocalURL%", Environment.GetEnvironmentVariable("localURL")).Replace(":%WebPort%", "");
131+ // else
132+ // sResult = sFile.Replace("%LocalURL%", Environment.GetEnvironmentVariable("localURL")).Replace("%WebPort%", Environment.GetEnvironmentVariable("WebPort"));
133+
134+ // //Cache result in Memory
135+ // if (!string.IsNullOrEmpty(sResult))
136+ // {
137+ // var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(300)); //cache ID for 5min
138+ // _cache.Set("GetPS", sResult, cacheEntryOptions);
139+ // }
140+
141+ // return sResult;
142+ //}
143+
144+ //try
145+ //{
146+ // string sFile2 = System.IO.File.ReadAllText("wwwroot/inventory.ps1");
147+ // sResult = sFile2.Replace("%LocalURL%", "http://localhost").Replace("%WebPort%", "5000");
148+
149+ // //Cache result in Memory
150+ // if (!string.IsNullOrEmpty(sResult))
151+ // {
152+ // var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(300)); //cache ID for 5min
153+ // _cache.Set("GetPS", sResult, cacheEntryOptions);
154+ // }
155+
156+ // return sResult;
157+ //}
158+ //catch { }
151159
152160 return sResult ;
153161
0 commit comments