Skip to content

Commit

Permalink
Better approach to resolving relative file paths
Browse files Browse the repository at this point in the history
Replaced strange approach to resolving relative folder  / file paths
with an approach that can consistently get the application root path
using the AppDomain. This avoids problems when the HttpContext is not
available.

Better approach to resolving relative file paths

Application was using a strange workaround to avoid problems when the
HttpContext was not available for resolving relative file paths.
Replaced this with a more consistent approach using the current app
domain.
  • Loading branch information
dpaquette committed Aug 18, 2014
1 parent 942521c commit bb3a035
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 45 deletions.
19 changes: 8 additions & 11 deletions src/BugTracker.Web/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,51 +98,48 @@ public void Application_Error(Object sender, EventArgs e)

public void Application_OnStart(Object sender, EventArgs e)
{
string path = HttpContext.Current.Server.MapPath(null);
HttpRuntime.Cache.Add("MapPath", path, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable, null);
HttpRuntime.Cache.Add("Application", Application, null, Cache.NoAbsoluteExpiration,
Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);

string dir = path + "\\App_Data";
string dir = Util.GetAbsolutePath("App_Data");
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}

dir = path + "\\App_Data\\logs";
dir = Util.GetAbsolutePath("App_Data\\logs");
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}

dir = path + "\\App_Data\\uploads";
dir = Util.GetAbsolutePath("App_Data\\uploads");
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}

dir = path + "\\App_Data\\lucene_index";
dir = Util.GetAbsolutePath("App_Data\\lucene_index");
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}

Util.set_context(HttpContext.Current); // required for map path calls to work in util.cs

StreamReader sr = File.OpenText(path + "\\custom\\custom_header.html");
StreamReader sr = File.OpenText(Util.GetAbsolutePath("custom\\custom_header.html"));
Application["custom_header"] = sr.ReadToEnd();
sr.Close();

sr = File.OpenText(path + "\\custom\\custom_footer.html");
sr = File.OpenText(Util.GetAbsolutePath("custom\\custom_footer.html"));
Application["custom_footer"] = sr.ReadToEnd();
sr.Close();

sr = File.OpenText(path + "\\custom\\custom_logo.html");
sr = File.OpenText(Util.GetAbsolutePath("custom\\custom_logo.html"));
Application["custom_logo"] = sr.ReadToEnd();
sr.Close();

sr = File.OpenText(path + "\\custom\\custom_welcome.html");
sr = File.OpenText(Util.GetAbsolutePath("custom\\custom_welcome.html"));
Application["custom_welcome"] = sr.ReadToEnd();
sr.Close();

Expand Down
8 changes: 3 additions & 5 deletions src/BugTracker.Web/btnet/print_bug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ public static void print_bug (HttpResponse Response, DataRow dr, Security securi

// If this file exists, use it.

string map_path = (string) HttpRuntime.Cache["MapPath"];

string css_for_email_file = map_path + "\\custom\\btnet_css_for_email.css";
string css_for_email_file = Util.GetAbsolutePath("custom\\btnet_css_for_email.css");

try
{
Expand All @@ -61,10 +59,10 @@ public static void print_bug (HttpResponse Response, DataRow dr, Security securi
}
else
{
css_for_email_file = map_path + "\\btnet_base.css";
css_for_email_file = Util.GetAbsolutePath("btnet_base.css");
Response.WriteFile(css_for_email_file);
Response.Write("\n");
css_for_email_file = map_path + "\\custom\\" + "btnet_custom.css";
css_for_email_file = Util.GetAbsolutePath("custom\\btnet_custom.css");
if (System.IO.File.Exists(css_for_email_file))
{
Response.WriteFile(css_for_email_file);
Expand Down
47 changes: 20 additions & 27 deletions src/BugTracker.Web/btnet/util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -636,63 +636,56 @@ public static bool is_numeric_datatype(Type datatype)
}
}

///////////////////////////////////////////////////////////////////////
protected static string get_absolute_or_relative_folder(string folder)
{

if (folder.IndexOf(":") == 1
|| folder.StartsWith("\\\\"))
{
// leave as is
return folder;
}
else
{
string map_path = (string)HttpRuntime.Cache["MapPath"];
return map_path + "\\" + folder;
}

}
public static string GetAbsolutePath(string path)
{
string result;
if (Path.IsPathRooted(path))
{
result = path;
}
else
{
string appRootPath = Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
result = Path.Combine(appRootPath, path);
}
return result;
}

///////////////////////////////////////////////////////////////////////
public static string get_folder(string name, string dflt)
{
String folder = Util.get_setting(name, "");
if (folder == "")
if (folder == String.Empty)
return dflt;

folder = get_absolute_or_relative_folder(folder);
if (!System.IO.Directory.Exists(folder))
folder = GetAbsolutePath(folder);
if (!Directory.Exists(folder))
{
throw (new Exception(name + " specified in Web.config, \""
+ folder
+ "\", not found. Edit Web.config."));
}

return folder;

}


///////////////////////////////////////////////////////////////////////
public static string get_lucene_index_folder()
{
string map_path = (string)HttpRuntime.Cache["MapPath"];
return get_folder("LuceneIndexFolder", map_path + "\\App_Data\\lucene_index");
return get_folder("LuceneIndexFolder", GetAbsolutePath("App_Data\\lucene_index"));
}

///////////////////////////////////////////////////////////////////////
public static string get_upload_folder()
{
string map_path = (string)HttpRuntime.Cache["MapPath"];
return get_folder("UploadFolder", map_path + "\\App_Data\\uploads");
return get_folder("UploadFolder", GetAbsolutePath("App_Data\\uploads"));
}

///////////////////////////////////////////////////////////////////////
public static string get_log_folder()
{
string map_path = (string)HttpRuntime.Cache["MapPath"];
return get_folder("LogFileFolder", map_path + "\\App_Data\\logs");
return get_folder("LogFileFolder", GetAbsolutePath("App_Data\\logs"));
}

///////////////////////////////////////////////////////////////////////
Expand Down
3 changes: 1 addition & 2 deletions src/BugTracker.Web/edit_styles.aspx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ void Page_Load(Object sender, EventArgs e)
}
// create path
string map_path = (string)HttpRuntime.Cache["MapPath"];
string path = map_path + "\\custom\\btnet_custom.css";
string path = Util.GetAbsolutePath("custom\\btnet_custom.css");
StringBuilder relevant_css_lines = new StringBuilder();
Expand Down

0 comments on commit bb3a035

Please sign in to comment.