BaseApplicationPaths.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. /// 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. /// Gets the path to the system folder
  43. /// </summary>
  44. public string ProgramSystemPath { get { return Path.Combine(ProgramDataPath, "system"); } }
  45. /// <summary>
  46. /// The _data directory
  47. /// </summary>
  48. private string _dataDirectory;
  49. /// <summary>
  50. /// Gets the folder path to the data directory
  51. /// </summary>
  52. /// <value>The data directory.</value>
  53. public string DataPath
  54. {
  55. get
  56. {
  57. if (_dataDirectory == null)
  58. {
  59. _dataDirectory = Path.Combine(ProgramDataPath, "data");
  60. if (!Directory.Exists(_dataDirectory))
  61. {
  62. Directory.CreateDirectory(_dataDirectory);
  63. }
  64. }
  65. return _dataDirectory;
  66. }
  67. }
  68. /// <summary>
  69. /// The _image cache path
  70. /// </summary>
  71. private string _imageCachePath;
  72. /// <summary>
  73. /// Gets the image cache path.
  74. /// </summary>
  75. /// <value>The image cache path.</value>
  76. public string ImageCachePath
  77. {
  78. get
  79. {
  80. if (_imageCachePath == null)
  81. {
  82. _imageCachePath = Path.Combine(CachePath, "images");
  83. if (!Directory.Exists(_imageCachePath))
  84. {
  85. Directory.CreateDirectory(_imageCachePath);
  86. }
  87. }
  88. return _imageCachePath;
  89. }
  90. }
  91. /// <summary>
  92. /// The _plugins path
  93. /// </summary>
  94. private string _pluginsPath;
  95. /// <summary>
  96. /// Gets the path to the plugin directory
  97. /// </summary>
  98. /// <value>The plugins path.</value>
  99. public string PluginsPath
  100. {
  101. get
  102. {
  103. if (_pluginsPath == null)
  104. {
  105. _pluginsPath = Path.Combine(ProgramDataPath, "plugins");
  106. if (!Directory.Exists(_pluginsPath))
  107. {
  108. Directory.CreateDirectory(_pluginsPath);
  109. }
  110. }
  111. return _pluginsPath;
  112. }
  113. }
  114. /// <summary>
  115. /// The _plugin configurations path
  116. /// </summary>
  117. private string _pluginConfigurationsPath;
  118. /// <summary>
  119. /// Gets the path to the plugin configurations directory
  120. /// </summary>
  121. /// <value>The plugin configurations path.</value>
  122. public string PluginConfigurationsPath
  123. {
  124. get
  125. {
  126. if (_pluginConfigurationsPath == null)
  127. {
  128. _pluginConfigurationsPath = Path.Combine(PluginsPath, "configurations");
  129. if (!Directory.Exists(_pluginConfigurationsPath))
  130. {
  131. Directory.CreateDirectory(_pluginConfigurationsPath);
  132. }
  133. }
  134. return _pluginConfigurationsPath;
  135. }
  136. }
  137. private string _tempUpdatePath;
  138. /// <summary>
  139. /// Gets the path to where temporary update files will be stored
  140. /// </summary>
  141. /// <value>The plugin configurations path.</value>
  142. public string TempUpdatePath
  143. {
  144. get
  145. {
  146. if (_tempUpdatePath == null)
  147. {
  148. _tempUpdatePath = Path.Combine(ProgramDataPath, "updates");
  149. if (!Directory.Exists(_tempUpdatePath))
  150. {
  151. Directory.CreateDirectory(_tempUpdatePath);
  152. }
  153. }
  154. return _tempUpdatePath;
  155. }
  156. }
  157. /// <summary>
  158. /// The _log directory path
  159. /// </summary>
  160. private string _logDirectoryPath;
  161. /// <summary>
  162. /// Gets the path to the log directory
  163. /// </summary>
  164. /// <value>The log directory path.</value>
  165. public string LogDirectoryPath
  166. {
  167. get
  168. {
  169. if (_logDirectoryPath == null)
  170. {
  171. _logDirectoryPath = Path.Combine(ProgramDataPath, "logs");
  172. if (!Directory.Exists(_logDirectoryPath))
  173. {
  174. Directory.CreateDirectory(_logDirectoryPath);
  175. }
  176. }
  177. return _logDirectoryPath;
  178. }
  179. }
  180. /// <summary>
  181. /// The _configuration directory path
  182. /// </summary>
  183. private string _configurationDirectoryPath;
  184. /// <summary>
  185. /// Gets the path to the application configuration root directory
  186. /// </summary>
  187. /// <value>The configuration directory path.</value>
  188. public string ConfigurationDirectoryPath
  189. {
  190. get
  191. {
  192. if (_configurationDirectoryPath == null)
  193. {
  194. _configurationDirectoryPath = Path.Combine(ProgramDataPath, "config");
  195. if (!Directory.Exists(_configurationDirectoryPath))
  196. {
  197. Directory.CreateDirectory(_configurationDirectoryPath);
  198. }
  199. }
  200. return _configurationDirectoryPath;
  201. }
  202. }
  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 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("%ApplicationData%", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
  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. if (string.IsNullOrEmpty(path))
  274. {
  275. throw new ApplicationException("Unable to determine running assembly location");
  276. }
  277. programDataPath = Path.Combine(path, programDataPath);
  278. programDataPath = Path.GetFullPath(programDataPath);
  279. }
  280. if (!Directory.Exists(programDataPath))
  281. {
  282. Directory.CreateDirectory(programDataPath);
  283. }
  284. return programDataPath;
  285. }
  286. }
  287. }