Add ApplicationPaths class

pull/702/head
ebr11 Eric Reed spam 12 years ago
parent a7bead51b2
commit 6e8bfb6d9b

@ -0,0 +1,162 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Configuration;
using System.Reflection;
namespace MediaBrowser.Common.Configuration
{
public static class ApplicationPaths
{
private static string _programDataPath;
/// <summary>
/// Gets the path to the program data folder
/// </summary>
public static string ProgramDataPath
{
get
{
if (_programDataPath == null)
{
_programDataPath = GetProgramDataPath();
InitDirectories(); //move this here so we don't have to check for existence on every directory reference
}
return _programDataPath;
}
}
private static void InitDirectories()
{
if (!Directory.Exists(LogDirectoryPath))
{
Directory.CreateDirectory(LogDirectoryPath);
}
if (!Directory.Exists(PluginsPath))
{
Directory.CreateDirectory(PluginsPath);
}
if (!Directory.Exists(RootFolderPath))
{
Directory.CreateDirectory(RootFolderPath);
}
if (!Directory.Exists(ConfigurationPath))
{
Directory.CreateDirectory(ConfigurationPath);
Directory.CreateDirectory(SystemConfigurationPath);
Directory.CreateDirectory(DeviceConfigurationPath);
Directory.CreateDirectory(UserConfigurationPath);
}
}
/// <summary>
/// Gets the path to the plugin directory
/// </summary>
public static string PluginsPath
{
get
{
return Path.Combine(ProgramDataPath, "plugins");
}
}
/// <summary>
/// Gets the path to the application configuration root directory
/// </summary>
public static string ConfigurationPath
{
get
{
return Path.Combine(ProgramDataPath, "config");
}
}
/// <summary>
/// Gets the path to the system configuration directory
/// </summary>
public static string SystemConfigurationPath
{
get
{
return Path.Combine(ConfigurationPath, "system");
}
}
/// <summary>
/// Gets the path to the user configuration directory
/// </summary>
public static string UserConfigurationPath
{
get
{
return Path.Combine(ConfigurationPath, "user");
}
}
/// <summary>
/// Gets the path to the device configuration directory
/// </summary>
public static string DeviceConfigurationPath
{
get
{
return Path.Combine(ConfigurationPath, "device");
}
}
/// <summary>
/// Gets the path to the log directory
/// </summary>
public static string LogDirectoryPath
{
get
{
return Path.Combine(ProgramDataPath, "logs");
}
}
/// <summary>
/// Gets the path to the root media directory
/// </summary>
public static string RootFolderPath
{
get
{
return Path.Combine(ProgramDataPath, "root");
}
}
/// <summary>
/// Gets the path to the application's ProgramDataFolder
/// </summary>
private static string GetProgramDataPath()
{
string programDataPath = ConfigurationManager.AppSettings["ProgramDataPath"];
// If it's a relative path, e.g. "..\"
if (!Path.IsPathRooted(programDataPath))
{
string path = Assembly.GetExecutingAssembly().Location;
path = Path.GetDirectoryName(path);
programDataPath = Path.Combine(path, programDataPath);
programDataPath = Path.GetFullPath(programDataPath);
}
if (!Directory.Exists(programDataPath))
{
Directory.CreateDirectory(programDataPath);
}
return programDataPath;
}
}
}

@ -21,49 +21,6 @@ namespace MediaBrowser.Common.Kernel
public abstract class BaseKernel<TConfigurationType> : IDisposable public abstract class BaseKernel<TConfigurationType> : IDisposable
where TConfigurationType : BaseApplicationConfiguration, new() where TConfigurationType : BaseApplicationConfiguration, new()
{ {
/// <summary>
/// Gets the path to the program data folder
/// </summary>
public string ProgramDataPath { get; private set; }
/// <summary>
/// Gets the path to the plugin directory
/// </summary>
protected string PluginsPath
{
get
{
return Path.Combine(ProgramDataPath, "plugins");
}
}
/// <summary>
/// Gets the path to the application configuration file
/// </summary>
protected string ConfigurationPath
{
get
{
return Path.Combine(ProgramDataPath, "config.js");
}
}
/// <summary>
/// Gets the path to the log directory
/// </summary>
private string LogDirectoryPath
{
get
{
return Path.Combine(ProgramDataPath, "logs");
}
}
/// <summary>
/// Gets or sets the path to the current log file
/// </summary>
private string LogFilePath { get; set; }
/// <summary> /// <summary>
/// Gets the current configuration /// Gets the current configuration
/// </summary> /// </summary>
@ -88,7 +45,7 @@ namespace MediaBrowser.Common.Kernel
public BaseKernel() public BaseKernel()
{ {
ProgramDataPath = GetProgramDataPath();
} }
public virtual void Init(IProgress<TaskProgress> progress) public virtual void Init(IProgress<TaskProgress> progress)
@ -102,18 +59,18 @@ namespace MediaBrowser.Common.Kernel
ReloadComposableParts(); ReloadComposableParts();
} }
/// <summary>
/// Gets or sets the path to the current log file
/// </summary>
public static string LogFilePath { get; set; }
private void ReloadLogger() private void ReloadLogger()
{ {
DisposeLogger(); DisposeLogger();
if (!Directory.Exists(LogDirectoryPath))
{
Directory.CreateDirectory(LogDirectoryPath);
}
DateTime now = DateTime.Now; DateTime now = DateTime.Now;
LogFilePath = Path.Combine(LogDirectoryPath, now.ToString("dMyyyy") + "-" + now.Ticks + ".log"); LogFilePath = Path.Combine(ApplicationPaths.LogDirectoryPath, Assembly.GetExecutingAssembly().GetType().Name + "-" + now.ToString("dMyyyy") + "-" + now.Ticks + ".log");
FileStream fs = new FileStream(LogFilePath, FileMode.Append, FileAccess.Write, FileShare.Read); FileStream fs = new FileStream(LogFilePath, FileMode.Append, FileAccess.Write, FileShare.Read);
@ -126,14 +83,9 @@ namespace MediaBrowser.Common.Kernel
/// </summary> /// </summary>
protected void ReloadComposableParts() protected void ReloadComposableParts()
{ {
if (!Directory.Exists(PluginsPath))
{
Directory.CreateDirectory(PluginsPath);
}
// Gets all plugin assemblies by first reading all bytes of the .dll and calling Assembly.Load against that // Gets all plugin assemblies by first reading all bytes of the .dll and calling Assembly.Load against that
// This will prevent the .dll file from getting locked, and allow us to replace it when needed // This will prevent the .dll file from getting locked, and allow us to replace it when needed
IEnumerable<Assembly> pluginAssemblies = Directory.GetFiles(PluginsPath, "*.dll", SearchOption.AllDirectories).Select(f => Assembly.Load(File.ReadAllBytes((f)))); IEnumerable<Assembly> pluginAssemblies = Directory.GetFiles(ApplicationPaths.PluginsPath, "*.dll", SearchOption.AllDirectories).Select(f => Assembly.Load(File.ReadAllBytes((f))));
var catalog = new AggregateCatalog(pluginAssemblies.Select(a => new AssemblyCatalog(a))); var catalog = new AggregateCatalog(pluginAssemblies.Select(a => new AssemblyCatalog(a)));
//catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly())); //catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
@ -172,7 +124,7 @@ namespace MediaBrowser.Common.Kernel
AssemblyName assemblyName = assembly.GetName(); AssemblyName assemblyName = assembly.GetName();
plugin.Version = assemblyName.Version; plugin.Version = assemblyName.Version;
plugin.Path = Path.Combine(PluginsPath, assemblyName.Name); plugin.Path = Path.Combine(ApplicationPaths.PluginsPath, assemblyName.Name);
plugin.Context = KernelContext; plugin.Context = KernelContext;
@ -185,57 +137,34 @@ namespace MediaBrowser.Common.Kernel
} }
} }
/// <summary>
/// Gets the path to the application's ProgramDataFolder
/// </summary>
private string GetProgramDataPath()
{
string programDataPath = ConfigurationManager.AppSettings["ProgramDataPath"];
// If it's a relative path, e.g. "..\"
if (!Path.IsPathRooted(programDataPath))
{
string path = Assembly.GetExecutingAssembly().Location;
path = Path.GetDirectoryName(path);
programDataPath = Path.Combine(path, programDataPath);
programDataPath = Path.GetFullPath(programDataPath);
}
if (!Directory.Exists(programDataPath))
{
Directory.CreateDirectory(programDataPath);
}
return programDataPath;
}
/// <summary> /// <summary>
/// Reloads application configuration from the config file /// Reloads application configuration from the config file
/// </summary> /// </summary>
private void ReloadConfiguration() private void ReloadConfiguration()
{ {
// Deserialize config //Configuration information for anything other than server-specific configuration will have to come via the API... -ebr
if (!File.Exists(ConfigurationPath))
{
Configuration = new TConfigurationType();
}
else
{
Configuration = JsonSerializer.DeserializeFromFile<TConfigurationType>(ConfigurationPath);
}
Logger.LoggerInstance.LogSeverity = Configuration.LogSeverity; //// Deserialize config
//if (!File.Exists(ConfigurationPath))
//{
// Configuration = new TConfigurationType();
//}
//else
//{
// Configuration = JsonSerializer.DeserializeFromFile<TConfigurationType>(ConfigurationPath);
//}
//Logger.LoggerInstance.LogSeverity = Configuration.LogSeverity;
} }
/// <summary> /// <summary>
/// Saves the current application configuration to the config file /// Saves the current application configuration to the config file
/// </summary> /// </summary>
public void SaveConfiguration() //public void SaveConfiguration()
{ //{
JsonSerializer.SerializeToFile(Configuration, ConfigurationPath); // JsonSerializer.SerializeToFile(Configuration, ConfigurationPath);
} //}
/// <summary> /// <summary>
/// Restarts the Http Server, or starts it if not currently running /// Restarts the Http Server, or starts it if not currently running
@ -255,7 +184,7 @@ namespace MediaBrowser.Common.Kernel
AssemblyName assemblyName = new AssemblyName(args.Name); AssemblyName assemblyName = new AssemblyName(args.Name);
// Look for the .dll recursively within the plugins directory // Look for the .dll recursively within the plugins directory
string dll = Directory.GetFiles(PluginsPath, "*.dll", SearchOption.AllDirectories) string dll = Directory.GetFiles(ApplicationPaths.PluginsPath, "*.dll", SearchOption.AllDirectories)
.FirstOrDefault(f => Path.GetFileNameWithoutExtension(f) == assemblyName.Name); .FirstOrDefault(f => Path.GetFileNameWithoutExtension(f) == assemblyName.Name);
// If we found a matching assembly, load it now // If we found a matching assembly, load it now

@ -48,6 +48,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Configuration\ApplicationPaths.cs" />
<Compile Include="Configuration\BaseApplicationConfiguration.cs" /> <Compile Include="Configuration\BaseApplicationConfiguration.cs" />
<Compile Include="Events\GenericItemEventArgs.cs" /> <Compile Include="Events\GenericItemEventArgs.cs" />
<Compile Include="Serialization\JsonSerializer.cs" /> <Compile Include="Serialization\JsonSerializer.cs" />

@ -6,6 +6,7 @@ using System.Linq;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Kernel; using MediaBrowser.Common.Kernel;
using MediaBrowser.Common.Serialization; using MediaBrowser.Common.Serialization;
using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Configuration;
@ -34,17 +35,18 @@ namespace MediaBrowser.Controller
{ {
get get
{ {
return Path.Combine(ProgramDataPath, "Root"); return ApplicationPaths.RootFolderPath;
} }
} }
private string UsersPath //gonna want to load this from the standard repository... -ebr
{ //private string UsersPath
get //{
{ // get
return Path.Combine(ProgramDataPath, "Users"); // {
} // return Path.Combine(ProgramDataPath, "Users");
} // }
//}
/// <summary> /// <summary>
/// Gets the list of currently registered entity resolvers /// Gets the list of currently registered entity resolvers
@ -434,22 +436,24 @@ namespace MediaBrowser.Controller
/// </summary> /// </summary>
private IEnumerable<User> GetAllUsers() private IEnumerable<User> GetAllUsers()
{ {
if (!Directory.Exists(UsersPath)) //we'll get this from the standard repo... -ebr
{
Directory.CreateDirectory(UsersPath); //if (!Directory.Exists(UsersPath))
} //{
// Directory.CreateDirectory(UsersPath);
//}
List<User> list = new List<User>(); List<User> list = new List<User>();
foreach (string folder in Directory.GetDirectories(UsersPath, "*", SearchOption.TopDirectoryOnly)) //foreach (string folder in Directory.GetDirectories(UsersPath, "*", SearchOption.TopDirectoryOnly))
{ //{
User item = GetFromDirectory(folder); // User item = GetFromDirectory(folder);
if (item != null) // if (item != null)
{ // {
list.Add(item); // list.Add(item);
} // }
} //}
return list; return list;
} }
@ -457,36 +461,36 @@ namespace MediaBrowser.Controller
/// <summary> /// <summary>
/// Gets a User from it's directory /// Gets a User from it's directory
/// </summary> /// </summary>
private User GetFromDirectory(string path) //private User GetFromDirectory(string path)
{ //{
string file = Path.Combine(path, "user.js"); // string file = Path.Combine(path, "user.js");
return JsonSerializer.DeserializeFromFile<User>(file); // return JsonSerializer.DeserializeFromFile<User>(file);
} //}
/// <summary> /// <summary>
/// Creates a User with a given name /// Creates a User with a given name
/// </summary> /// </summary>
public User CreateUser(string name) //public User CreateUser(string name)
{ //{
var now = DateTime.Now; // var now = DateTime.Now;
User user = new User() // User user = new User()
{ // {
Name = name, // Name = name,
Id = Guid.NewGuid(), // Id = Guid.NewGuid(),
DateCreated = now, // DateCreated = now,
DateModified = now // DateModified = now
}; // };
user.Path = Path.Combine(UsersPath, user.Id.ToString()); // user.Path = Path.Combine(UsersPath, user.Id.ToString());
Directory.CreateDirectory(user.Path); // Directory.CreateDirectory(user.Path);
JsonSerializer.SerializeToFile(user, Path.Combine(user.Path, "user.js")); // JsonSerializer.SerializeToFile(user, Path.Combine(user.Path, "user.js"));
return user; // return user;
} //}
} }
} }

@ -77,7 +77,4 @@ Global
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
EndGlobalSection EndGlobalSection
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
EndGlobal EndGlobal

Loading…
Cancel
Save