BaseApplicationPaths.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. if (!Directory.Exists(_dataDirectory))
  69. {
  70. Directory.CreateDirectory(_dataDirectory);
  71. }
  72. }
  73. return _dataDirectory;
  74. }
  75. }
  76. /// <summary>
  77. /// The _image cache path
  78. /// </summary>
  79. private string _imageCachePath;
  80. /// <summary>
  81. /// Gets the image cache path.
  82. /// </summary>
  83. /// <value>The image cache path.</value>
  84. public string ImageCachePath
  85. {
  86. get
  87. {
  88. if (_imageCachePath == null)
  89. {
  90. _imageCachePath = Path.Combine(CachePath, "images");
  91. if (!Directory.Exists(_imageCachePath))
  92. {
  93. Directory.CreateDirectory(_imageCachePath);
  94. }
  95. }
  96. return _imageCachePath;
  97. }
  98. }
  99. /// <summary>
  100. /// The _plugins path
  101. /// </summary>
  102. private string _pluginsPath;
  103. /// <summary>
  104. /// Gets the path to the plugin directory
  105. /// </summary>
  106. /// <value>The plugins path.</value>
  107. public string PluginsPath
  108. {
  109. get
  110. {
  111. if (_pluginsPath == null)
  112. {
  113. _pluginsPath = Path.Combine(ProgramDataPath, "plugins");
  114. if (!Directory.Exists(_pluginsPath))
  115. {
  116. Directory.CreateDirectory(_pluginsPath);
  117. }
  118. }
  119. return _pluginsPath;
  120. }
  121. }
  122. /// <summary>
  123. /// The _plugin configurations path
  124. /// </summary>
  125. private string _pluginConfigurationsPath;
  126. /// <summary>
  127. /// Gets the path to the plugin configurations directory
  128. /// </summary>
  129. /// <value>The plugin configurations path.</value>
  130. public string PluginConfigurationsPath
  131. {
  132. get
  133. {
  134. if (_pluginConfigurationsPath == null)
  135. {
  136. _pluginConfigurationsPath = Path.Combine(PluginsPath, "configurations");
  137. if (!Directory.Exists(_pluginConfigurationsPath))
  138. {
  139. Directory.CreateDirectory(_pluginConfigurationsPath);
  140. }
  141. }
  142. return _pluginConfigurationsPath;
  143. }
  144. }
  145. private string _tempUpdatePath;
  146. /// <summary>
  147. /// Gets the path to where temporary update files will be stored
  148. /// </summary>
  149. /// <value>The plugin configurations path.</value>
  150. public string TempUpdatePath
  151. {
  152. get
  153. {
  154. if (_tempUpdatePath == null)
  155. {
  156. _tempUpdatePath = Path.Combine(ProgramDataPath, "updates");
  157. if (!Directory.Exists(_tempUpdatePath))
  158. {
  159. Directory.CreateDirectory(_tempUpdatePath);
  160. }
  161. }
  162. return _tempUpdatePath;
  163. }
  164. }
  165. /// <summary>
  166. /// The _log directory path
  167. /// </summary>
  168. private string _logDirectoryPath;
  169. /// <summary>
  170. /// Gets the path to the log directory
  171. /// </summary>
  172. /// <value>The log directory path.</value>
  173. public string LogDirectoryPath
  174. {
  175. get
  176. {
  177. if (_logDirectoryPath == null)
  178. {
  179. _logDirectoryPath = Path.Combine(ProgramDataPath, "logs");
  180. if (!Directory.Exists(_logDirectoryPath))
  181. {
  182. Directory.CreateDirectory(_logDirectoryPath);
  183. }
  184. }
  185. return _logDirectoryPath;
  186. }
  187. }
  188. /// <summary>
  189. /// The _configuration directory path
  190. /// </summary>
  191. private string _configurationDirectoryPath;
  192. /// <summary>
  193. /// Gets the path to the application configuration root directory
  194. /// </summary>
  195. /// <value>The configuration directory path.</value>
  196. public string ConfigurationDirectoryPath
  197. {
  198. get
  199. {
  200. if (_configurationDirectoryPath == null)
  201. {
  202. _configurationDirectoryPath = Path.Combine(ProgramDataPath, "config");
  203. if (!Directory.Exists(_configurationDirectoryPath))
  204. {
  205. Directory.CreateDirectory(_configurationDirectoryPath);
  206. }
  207. }
  208. return _configurationDirectoryPath;
  209. }
  210. }
  211. /// <summary>
  212. /// Gets the path to the system configuration file
  213. /// </summary>
  214. /// <value>The system configuration file path.</value>
  215. public string SystemConfigurationFilePath
  216. {
  217. get
  218. {
  219. return Path.Combine(ConfigurationDirectoryPath, "system.xml");
  220. }
  221. }
  222. /// <summary>
  223. /// The _cache directory
  224. /// </summary>
  225. private string _cachePath;
  226. /// <summary>
  227. /// Gets the folder path to the cache directory
  228. /// </summary>
  229. /// <value>The cache directory.</value>
  230. public string CachePath
  231. {
  232. get
  233. {
  234. if (_cachePath == null)
  235. {
  236. _cachePath = Path.Combine(ProgramDataPath, "cache");
  237. if (!Directory.Exists(_cachePath))
  238. {
  239. Directory.CreateDirectory(_cachePath);
  240. }
  241. }
  242. return _cachePath;
  243. }
  244. }
  245. /// <summary>
  246. /// The _temp directory
  247. /// </summary>
  248. private string _tempDirectory;
  249. /// <summary>
  250. /// Gets the folder path to the temp directory within the cache folder
  251. /// </summary>
  252. /// <value>The temp directory.</value>
  253. public string TempDirectory
  254. {
  255. get
  256. {
  257. if (_tempDirectory == null)
  258. {
  259. _tempDirectory = Path.Combine(CachePath, "temp");
  260. if (!Directory.Exists(_tempDirectory))
  261. {
  262. Directory.CreateDirectory(_tempDirectory);
  263. }
  264. }
  265. return _tempDirectory;
  266. }
  267. }
  268. /// <summary>
  269. /// Gets the path to the application's ProgramDataFolder
  270. /// </summary>
  271. /// <returns>System.String.</returns>
  272. private string GetProgramDataPath()
  273. {
  274. var programDataPath = _useDebugPath ? ConfigurationManager.AppSettings["DebugProgramDataPath"] : Path.Combine(ConfigurationManager.AppSettings["ReleaseProgramDataPath"], ConfigurationManager.AppSettings["ProgramDataFolderName"]);
  275. programDataPath = programDataPath.Replace("%ApplicationData%", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
  276. // If it's a relative path, e.g. "..\"
  277. if (!Path.IsPathRooted(programDataPath))
  278. {
  279. var path = Assembly.GetExecutingAssembly().Location;
  280. path = Path.GetDirectoryName(path);
  281. if (string.IsNullOrEmpty(path))
  282. {
  283. throw new ApplicationException("Unable to determine running assembly location");
  284. }
  285. programDataPath = Path.Combine(path, programDataPath);
  286. programDataPath = Path.GetFullPath(programDataPath);
  287. }
  288. if (!Directory.Exists(programDataPath))
  289. {
  290. Directory.CreateDirectory(programDataPath);
  291. }
  292. return programDataPath;
  293. }
  294. }
  295. }