BaseApplicationPaths.cs 9.9 KB

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