15
15
using Microsoft . Extensions . Caching . Memory ;
16
16
using System . Text ;
17
17
using Microsoft . AspNetCore . Authorization ;
18
- using System . Collections . Generic ;
18
+ using Microsoft . AspNetCore . Hosting ;
19
+ using Microsoft . AspNetCore . Http . Extensions ;
19
20
20
21
namespace jaindb . Controllers
21
22
{
22
23
[ Produces ( "application/json" ) ]
23
- public class HomeController : Controller
24
+ public class JainDBController : Controller
24
25
{
25
26
private readonly IConfiguration _config ;
26
27
private readonly ILogger _logger ;
27
28
private IMemoryCache _cache ;
29
+ private readonly IHostingEnvironment _env ;
28
30
29
- public HomeController ( IConfiguration config , ILogger < HomeController > logger , IMemoryCache memoryCache )
31
+ public JainDBController ( IConfiguration config , ILogger < JainDBController > logger , IMemoryCache memoryCache , IHostingEnvironment env )
30
32
{
31
33
_config = config ;
32
34
_logger = logger ;
33
35
_cache = memoryCache ;
36
+ _env = env ;
34
37
jDB . _cache = memoryCache ;
35
38
}
36
39
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
+ // }
43
46
44
47
[ HttpPost ]
45
48
[ Route ( "upload/{Id}" ) ]
@@ -96,32 +99,18 @@ public string GetPS()
96
99
return sResult ;
97
100
}
98
101
99
- if ( System . IO . File . Exists ( "/app/wwwroot/ inventory.ps1") )
102
+ if ( System . IO . File . Exists ( Path . Combine ( _env . WebRootPath , " inventory.ps1") ) )
100
103
{
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" ) ) ;
106
105
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" , "" ) ;
113
107
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"));
116
112
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%" , "" ) ;
125
114
126
115
//Cache result in Memory
127
116
if ( ! string . IsNullOrEmpty ( sResult ) )
@@ -133,21 +122,40 @@ public string GetPS()
133
122
return sResult ;
134
123
}
135
124
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 { }
151
159
152
160
return sResult ;
153
161
0 commit comments