BaseApplicationPaths.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. using MediaBrowser.Common.Configuration;
  2. using System;
  3. using System.Configuration;
  4. using System.IO;
  5. using System.Reflection;
  6. namespace MediaBrowser.Common.Implementations
  7. {
  8. /// <summary>
  9. /// Provides a base class to hold common application paths used by both the Ui and Server.
  10. /// This can be subclassed to add application-specific paths.
  11. /// </summary>
  12. public abstract class BaseApplicationPaths : IApplicationPaths
  13. {
  14. /// <summary>
  15. /// The _use debug path
  16. /// </summary>
  17. private readonly bool _useDebugPath;
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="BaseApplicationPaths" /> class.
  20. /// </summary>
  21. /// <param name="useDebugPath">if set to <c>true</c> [use debug paths].</param>
  22. protected BaseApplicationPaths(bool useDebugPath)
  23. {
  24. _useDebugPath = useDebugPath;
  25. }
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="BaseApplicationPaths"/> class.
  28. /// </summary>
  29. /// <param name="programDataPath">The program data path.</param>
  30. protected BaseApplicationPaths(string programDataPath)
  31. {
  32. _programDataPath = programDataPath;
  33. }
  34. /// <summary>
  35. /// The _program data path
  36. /// </summary>
  37. private string _programDataPath;
  38. /// <summary>
  39. /// Gets the path to the program data folder
  40. /// </summary>
  41. /// <value>The program data path.</value>
  42. public string ProgramDataPath
  43. {
  44. get
  45. {
  46. return _programDataPath ?? (_programDataPath = GetProgramDataPath());
  47. }
  48. }
  49. /// <summary>
  50. /// Gets the path to the system folder
  51. /// </summary>
  52. public string ProgramSystemPath { get { return Path.Combine(ProgramDataPath, "system"); } }
  53. /// <summary>
  54. /// The _data directory
  55. /// </summary>
  56. private string _dataDirectory;
  57. /// <summary>
  58. /// Gets the folder path to the data directory
  59. /// </summary>
  60. /// <value>The data directory.</value>
  61. public string DataPath
  62. {
  63. get
  64. {
  65. if (_dataDirectory == null)
  66. {
  67. _dataDirectory = Path.Combine(ProgramDataPath, "data");
  68. Directory.CreateDirectory(_dataDirectory);
  69. }
  70. return _dataDirectory;
  71. }
  72. }
  73. /// <summary>
  74. /// The _image cache path
  75. /// </summary>
  76. private string _imageCachePath;
  77. /// <summary>
  78. /// Gets the image cache path.
  79. /// </summary>
  80. /// <value>The image cache path.</value>
  81. public string ImageCachePath
  82. {
  83. get
  84. {
  85. if (_imageCachePath == null)
  86. {
  87. _imageCachePath = Path.Combine(CachePath, "images");
  88. Directory.CreateDirectory(_imageCachePath);
  89. }
  90. return _imageCachePath;
  91. }
  92. }
  93. /// <summary>
  94. /// The _plugins path
  95. /// </summary>
  96. private string _pluginsPath;
  97. /// <summary>
  98. /// Gets the path to the plugin directory
  99. /// </summary>
  100. /// <value>The plugins path.</value>
  101. public string PluginsPath
  102. {
  103. get
  104. {
  105. if (_pluginsPath == null)
  106. {
  107. _pluginsPath = Path.Combine(ProgramDataPath, "plugins");
  108. Directory.CreateDirectory(_pluginsPath);
  109. }
  110. return _pluginsPath;
  111. }
  112. }
  113. /// <summary>
  114. /// The _plugin configurations path
  115. /// </summary>
  116. private string _pluginConfigurationsPath;
  117. /// <summary>
  118. /// Gets the path to the plugin configurations directory
  119. /// </summary>
  120. /// <value>The plugin configurations path.</value>
  121. public string PluginConfigurationsPath
  122. {
  123. get
  124. {
  125. if (_pluginConfigurationsPath == null)
  126. {
  127. _pluginConfigurationsPath = Path.Combine(PluginsPath, "configurations");
  128. Directory.CreateDirectory(_pluginConfigurationsPath);
  129. }
  130. return _pluginConfigurationsPath;
  131. }
  132. }
  133. private string _tempUpdatePath;
  134. /// <summary>
  135. /// Gets the path to where temporary update files will be stored
  136. /// </summary>
  137. /// <value>The plugin configurations path.</value>
  138. public string TempUpdatePath
  139. {
  140. get
  141. {
  142. if (_tempUpdatePath == null)
  143. {
  144. _tempUpdatePath = Path.Combine(ProgramDataPath, "updates");
  145. Directory.CreateDirectory(_tempUpdatePath);
  146. }
  147. return _tempUpdatePath;
  148. }
  149. }
  150. /// <summary>
  151. /// The _log directory path
  152. /// </summary>
  153. private string _logDirectoryPath;
  154. /// <summary>
  155. /// Gets the path to the log directory
  156. /// </summary>
  157. /// <value>The log directory path.</value>
  158. public string LogDirectoryPath
  159. {
  160. get
  161. {
  162. if (_logDirectoryPath == null)
  163. {
  164. _logDirectoryPath = Path.Combine(ProgramDataPath, "logs");
  165. Directory.CreateDirectory(_logDirectoryPath);
  166. }
  167. return _logDirectoryPath;
  168. }
  169. }
  170. /// <summary>
  171. /// The _configuration directory path
  172. /// </summary>
  173. private string _configurationDirectoryPath;
  174. /// <summary>
  175. /// Gets the path to the application configuration root directory
  176. /// </summary>
  177. /// <value>The configuration directory path.</value>
  178. public string ConfigurationDirectoryPath
  179. {
  180. get
  181. {
  182. if (_configurationDirectoryPath == null)
  183. {
  184. _configurationDirectoryPath = Path.Combine(ProgramDataPath, "config");
  185. Directory.CreateDirectory(_configurationDirectoryPath);
  186. }
  187. return _configurationDirectoryPath;
  188. }
  189. }
  190. /// <summary>
  191. /// Gets the path to the system configuration file
  192. /// </summary>
  193. /// <value>The system configuration file path.</value>
  194. public string SystemConfigurationFilePath
  195. {
  196. get
  197. {
  198. return Path.Combine(ConfigurationDirectoryPath, "system.xml");
  199. }
  200. }
  201. /// <summary>
  202. /// The _cache directory
  203. /// </summary>
  204. private string _cachePath;
  205. /// <summary>
  206. /// Gets the folder path to the cache directory
  207. /// </summary>
  208. /// <value>The cache directory.</value>
  209. public string CachePath
  210. {
  211. get
  212. {
  213. if (_cachePath == null)
  214. {
  215. _cachePath = Path.Combine(ProgramDataPath, "cache");
  216. Directory.CreateDirectory(_cachePath);
  217. }
  218. return _cachePath;
  219. }
  220. }
  221. /// <summary>
  222. /// The _temp directory
  223. /// </summary>
  224. private string _tempDirectory;
  225. /// <summary>
  226. /// Gets the folder path to the temp directory within the cache folder
  227. /// </summary>
  228. /// <value>The temp directory.</value>
  229. public string TempDirectory
  230. {
  231. get
  232. {
  233. if (_tempDirectory == null)
  234. {
  235. _tempDirectory = Path.Combine(CachePath, "temp");
  236. Directory.CreateDirectory(_tempDirectory);
  237. }
  238. return _tempDirectory;
  239. }
  240. }
  241. /// <summary>
  242. /// Gets the path to the application's ProgramDataFolder
  243. /// </summary>
  244. /// <returns>System.String.</returns>
  245. private string GetProgramDataPath()
  246. {
  247. var programDataPath = _useDebugPath ? ConfigurationManager.AppSettings["DebugProgramDataPath"] : Path.Combine(ConfigurationManager.AppSettings["ReleaseProgramDataPath"], ConfigurationManager.AppSettings["ProgramDataFolderName"]);
  248. programDataPath = programDataPath.Replace("%ApplicationData%", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
  249. // If it's a relative path, e.g. "..\"
  250. if (!Path.IsPathRooted(programDataPath))
  251. {
  252. var path = Assembly.GetExecutingAssembly().Location;
  253. path = Path.GetDirectoryName(path);
  254. if (string.IsNullOrEmpty(path))
  255. {
  256. throw new ApplicationException("Unable to determine running assembly location");
  257. }
  258. programDataPath = Path.Combine(path, programDataPath);
  259. programDataPath = Path.GetFullPath(programDataPath);
  260. }
  261. Directory.CreateDirectory(programDataPath);
  262. return programDataPath;
  263. }
  264. }
  265. }