BaseApplicationPaths.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using MediaBrowser.Common.Kernel;
  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 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. /// The _program data path
  28. /// </summary>
  29. private string _programDataPath;
  30. /// <summary>
  31. /// Gets the path to the program data folder
  32. /// </summary>
  33. /// <value>The program data path.</value>
  34. public string ProgramDataPath
  35. {
  36. get
  37. {
  38. return _programDataPath ?? (_programDataPath = GetProgramDataPath());
  39. }
  40. }
  41. /// <summary>
  42. /// The _data directory
  43. /// </summary>
  44. private string _dataDirectory;
  45. /// <summary>
  46. /// Gets the folder path to the data directory
  47. /// </summary>
  48. /// <value>The data directory.</value>
  49. public string DataPath
  50. {
  51. get
  52. {
  53. if (_dataDirectory == null)
  54. {
  55. _dataDirectory = Path.Combine(ProgramDataPath, "data");
  56. if (!Directory.Exists(_dataDirectory))
  57. {
  58. Directory.CreateDirectory(_dataDirectory);
  59. }
  60. }
  61. return _dataDirectory;
  62. }
  63. }
  64. /// <summary>
  65. /// The _image cache path
  66. /// </summary>
  67. private string _imageCachePath;
  68. /// <summary>
  69. /// Gets the image cache path.
  70. /// </summary>
  71. /// <value>The image cache path.</value>
  72. public string ImageCachePath
  73. {
  74. get
  75. {
  76. if (_imageCachePath == null)
  77. {
  78. _imageCachePath = Path.Combine(CachePath, "images");
  79. if (!Directory.Exists(_imageCachePath))
  80. {
  81. Directory.CreateDirectory(_imageCachePath);
  82. }
  83. }
  84. return _imageCachePath;
  85. }
  86. }
  87. /// <summary>
  88. /// The _plugins path
  89. /// </summary>
  90. private string _pluginsPath;
  91. /// <summary>
  92. /// Gets the path to the plugin directory
  93. /// </summary>
  94. /// <value>The plugins path.</value>
  95. public string PluginsPath
  96. {
  97. get
  98. {
  99. if (_pluginsPath == null)
  100. {
  101. _pluginsPath = Path.Combine(ProgramDataPath, "plugins");
  102. if (!Directory.Exists(_pluginsPath))
  103. {
  104. Directory.CreateDirectory(_pluginsPath);
  105. }
  106. }
  107. return _pluginsPath;
  108. }
  109. }
  110. /// <summary>
  111. /// The _plugin configurations path
  112. /// </summary>
  113. private string _pluginConfigurationsPath;
  114. /// <summary>
  115. /// Gets the path to the plugin configurations directory
  116. /// </summary>
  117. /// <value>The plugin configurations path.</value>
  118. public string PluginConfigurationsPath
  119. {
  120. get
  121. {
  122. if (_pluginConfigurationsPath == null)
  123. {
  124. _pluginConfigurationsPath = Path.Combine(PluginsPath, "configurations");
  125. if (!Directory.Exists(_pluginConfigurationsPath))
  126. {
  127. Directory.CreateDirectory(_pluginConfigurationsPath);
  128. }
  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. if (!Directory.Exists(_tempUpdatePath))
  146. {
  147. Directory.CreateDirectory(_tempUpdatePath);
  148. }
  149. }
  150. return _tempUpdatePath;
  151. }
  152. }
  153. /// <summary>
  154. /// The _log directory path
  155. /// </summary>
  156. private string _logDirectoryPath;
  157. /// <summary>
  158. /// Gets the path to the log directory
  159. /// </summary>
  160. /// <value>The log directory path.</value>
  161. public string LogDirectoryPath
  162. {
  163. get
  164. {
  165. if (_logDirectoryPath == null)
  166. {
  167. _logDirectoryPath = Path.Combine(ProgramDataPath, "logs");
  168. if (!Directory.Exists(_logDirectoryPath))
  169. {
  170. Directory.CreateDirectory(_logDirectoryPath);
  171. }
  172. }
  173. return _logDirectoryPath;
  174. }
  175. }
  176. /// <summary>
  177. /// The _configuration directory path
  178. /// </summary>
  179. private string _configurationDirectoryPath;
  180. /// <summary>
  181. /// Gets the path to the application configuration root directory
  182. /// </summary>
  183. /// <value>The configuration directory path.</value>
  184. public string ConfigurationDirectoryPath
  185. {
  186. get
  187. {
  188. if (_configurationDirectoryPath == null)
  189. {
  190. _configurationDirectoryPath = Path.Combine(ProgramDataPath, "config");
  191. if (!Directory.Exists(_configurationDirectoryPath))
  192. {
  193. Directory.CreateDirectory(_configurationDirectoryPath);
  194. }
  195. }
  196. return _configurationDirectoryPath;
  197. }
  198. }
  199. /// <summary>
  200. /// The _system configuration file path
  201. /// </summary>
  202. private string _systemConfigurationFilePath;
  203. /// <summary>
  204. /// Gets the path to the system configuration file
  205. /// </summary>
  206. /// <value>The system configuration file path.</value>
  207. public string SystemConfigurationFilePath
  208. {
  209. get
  210. {
  211. return _systemConfigurationFilePath ?? (_systemConfigurationFilePath = Path.Combine(ConfigurationDirectoryPath, "system.xml"));
  212. }
  213. }
  214. /// <summary>
  215. /// The _cache directory
  216. /// </summary>
  217. private string _cachePath;
  218. /// <summary>
  219. /// Gets the folder path to the cache directory
  220. /// </summary>
  221. /// <value>The cache directory.</value>
  222. public string CachePath
  223. {
  224. get
  225. {
  226. if (_cachePath == null)
  227. {
  228. _cachePath = Path.Combine(ProgramDataPath, "cache");
  229. if (!Directory.Exists(_cachePath))
  230. {
  231. Directory.CreateDirectory(_cachePath);
  232. }
  233. }
  234. return _cachePath;
  235. }
  236. }
  237. /// <summary>
  238. /// The _temp directory
  239. /// </summary>
  240. private string _tempDirectory;
  241. /// <summary>
  242. /// Gets the folder path to the temp directory within the cache folder
  243. /// </summary>
  244. /// <value>The temp directory.</value>
  245. public string TempDirectory
  246. {
  247. get
  248. {
  249. if (_tempDirectory == null)
  250. {
  251. _tempDirectory = Path.Combine(CachePath, "temp");
  252. if (!Directory.Exists(_tempDirectory))
  253. {
  254. Directory.CreateDirectory(_tempDirectory);
  255. }
  256. }
  257. return _tempDirectory;
  258. }
  259. }
  260. /// <summary>
  261. /// Gets the path to the application's ProgramDataFolder
  262. /// </summary>
  263. /// <returns>System.String.</returns>
  264. private string GetProgramDataPath()
  265. {
  266. var programDataPath = _useDebugPath ? ConfigurationManager.AppSettings["DebugProgramDataPath"] : Path.Combine(ConfigurationManager.AppSettings["ReleaseProgramDataPath"], ConfigurationManager.AppSettings["ProgramDataFolderName"]);
  267. programDataPath = programDataPath.Replace("%CommonApplicationData%", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));
  268. // If it's a relative path, e.g. "..\"
  269. if (!Path.IsPathRooted(programDataPath))
  270. {
  271. var path = Assembly.GetExecutingAssembly().Location;
  272. path = Path.GetDirectoryName(path);
  273. programDataPath = Path.Combine(path, programDataPath);
  274. programDataPath = Path.GetFullPath(programDataPath);
  275. }
  276. if (!Directory.Exists(programDataPath))
  277. {
  278. Directory.CreateDirectory(programDataPath);
  279. }
  280. return programDataPath;
  281. }
  282. }
  283. }