2
0

ServerConfigurationManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. using System.Collections.Generic;
  2. using MediaBrowser.Common.Configuration;
  3. using MediaBrowser.Common.Events;
  4. using MediaBrowser.Common.Implementations.Configuration;
  5. using MediaBrowser.Controller;
  6. using MediaBrowser.Controller.Configuration;
  7. using MediaBrowser.Controller.Entities;
  8. using MediaBrowser.Controller.Entities.Audio;
  9. using MediaBrowser.Controller.Entities.Movies;
  10. using MediaBrowser.Controller.Entities.TV;
  11. using MediaBrowser.Model.Configuration;
  12. using MediaBrowser.Model.Events;
  13. using MediaBrowser.Model.Logging;
  14. using MediaBrowser.Model.Serialization;
  15. using System;
  16. using System.IO;
  17. using System.Linq;
  18. using CommonIO;
  19. namespace MediaBrowser.Server.Implementations.Configuration
  20. {
  21. /// <summary>
  22. /// Class ServerConfigurationManager
  23. /// </summary>
  24. public class ServerConfigurationManager : BaseConfigurationManager, IServerConfigurationManager
  25. {
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="ServerConfigurationManager" /> class.
  28. /// </summary>
  29. /// <param name="applicationPaths">The application paths.</param>
  30. /// <param name="logManager">The log manager.</param>
  31. /// <param name="xmlSerializer">The XML serializer.</param>
  32. /// <param name="fileSystem">The file system.</param>
  33. public ServerConfigurationManager(IApplicationPaths applicationPaths, ILogManager logManager, IXmlSerializer xmlSerializer, IFileSystem fileSystem)
  34. : base(applicationPaths, logManager, xmlSerializer, fileSystem)
  35. {
  36. UpdateItemsByNamePath();
  37. UpdateMetadataPath();
  38. }
  39. public event EventHandler<GenericEventArgs<ServerConfiguration>> ConfigurationUpdating;
  40. /// <summary>
  41. /// Gets the type of the configuration.
  42. /// </summary>
  43. /// <value>The type of the configuration.</value>
  44. protected override Type ConfigurationType
  45. {
  46. get { return typeof(ServerConfiguration); }
  47. }
  48. /// <summary>
  49. /// Gets the application paths.
  50. /// </summary>
  51. /// <value>The application paths.</value>
  52. public IServerApplicationPaths ApplicationPaths
  53. {
  54. get { return (IServerApplicationPaths)CommonApplicationPaths; }
  55. }
  56. /// <summary>
  57. /// Gets the configuration.
  58. /// </summary>
  59. /// <value>The configuration.</value>
  60. public ServerConfiguration Configuration
  61. {
  62. get { return (ServerConfiguration)CommonConfiguration; }
  63. }
  64. /// <summary>
  65. /// Called when [configuration updated].
  66. /// </summary>
  67. protected override void OnConfigurationUpdated()
  68. {
  69. UpdateItemsByNamePath();
  70. UpdateMetadataPath();
  71. base.OnConfigurationUpdated();
  72. }
  73. public override void AddParts(IEnumerable<IConfigurationFactory> factories)
  74. {
  75. base.AddParts(factories);
  76. UpdateTranscodingTempPath();
  77. }
  78. /// <summary>
  79. /// Updates the items by name path.
  80. /// </summary>
  81. private void UpdateItemsByNamePath()
  82. {
  83. if (!Configuration.MergeMetadataAndImagesByName)
  84. {
  85. ((ServerApplicationPaths)ApplicationPaths).ItemsByNamePath = string.IsNullOrEmpty(Configuration.ItemsByNamePath) ?
  86. null :
  87. Configuration.ItemsByNamePath;
  88. }
  89. }
  90. /// <summary>
  91. /// Updates the metadata path.
  92. /// </summary>
  93. private void UpdateMetadataPath()
  94. {
  95. string metadataPath;
  96. if (string.IsNullOrWhiteSpace(Configuration.MetadataPath))
  97. {
  98. metadataPath = GetInternalMetadataPath();
  99. }
  100. else if (Configuration.EnableCustomPathSubFolders)
  101. {
  102. metadataPath = Path.Combine(Configuration.MetadataPath, "metadata");
  103. }
  104. else
  105. {
  106. metadataPath = Configuration.MetadataPath;
  107. }
  108. ((ServerApplicationPaths)ApplicationPaths).InternalMetadataPath = metadataPath;
  109. ((ServerApplicationPaths)ApplicationPaths).ItemsByNamePath = ((ServerApplicationPaths)ApplicationPaths).InternalMetadataPath;
  110. }
  111. private string GetInternalMetadataPath()
  112. {
  113. return Path.Combine(ApplicationPaths.ProgramDataPath, "metadata");
  114. }
  115. /// <summary>
  116. /// Updates the transcoding temporary path.
  117. /// </summary>
  118. private void UpdateTranscodingTempPath()
  119. {
  120. var encodingConfig = this.GetConfiguration<EncodingOptions>("encoding");
  121. ((ServerApplicationPaths)ApplicationPaths).TranscodingTempPath = string.IsNullOrEmpty(encodingConfig.TranscodingTempPath) ?
  122. null :
  123. Path.Combine(encodingConfig.TranscodingTempPath, "transcoding-temp");
  124. }
  125. protected override void OnNamedConfigurationUpdated(string key, object configuration)
  126. {
  127. base.OnNamedConfigurationUpdated(key, configuration);
  128. if (string.Equals(key, "encoding", StringComparison.OrdinalIgnoreCase))
  129. {
  130. UpdateTranscodingTempPath();
  131. }
  132. }
  133. /// <summary>
  134. /// Replaces the configuration.
  135. /// </summary>
  136. /// <param name="newConfiguration">The new configuration.</param>
  137. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  138. public override void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration)
  139. {
  140. var newConfig = (ServerConfiguration)newConfiguration;
  141. ValidateItemByNamePath(newConfig);
  142. ValidatePathSubstitutions(newConfig);
  143. ValidateMetadataPath(newConfig);
  144. ValidateSslCertificate(newConfig);
  145. EventHelper.FireEventIfNotNull(ConfigurationUpdating, this, new GenericEventArgs<ServerConfiguration> { Argument = newConfig }, Logger);
  146. base.ReplaceConfiguration(newConfiguration);
  147. }
  148. /// <summary>
  149. /// Validates the SSL certificate.
  150. /// </summary>
  151. /// <param name="newConfig">The new configuration.</param>
  152. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  153. private void ValidateSslCertificate(BaseApplicationConfiguration newConfig)
  154. {
  155. var serverConfig = (ServerConfiguration)newConfig;
  156. var newPath = serverConfig.CertificatePath;
  157. if (!string.IsNullOrWhiteSpace(newPath)
  158. && !string.Equals(Configuration.CertificatePath ?? string.Empty, newPath))
  159. {
  160. // Validate
  161. if (!FileSystem.FileExists(newPath))
  162. {
  163. throw new FileNotFoundException(string.Format("Certificate file '{0}' does not exist.", newPath));
  164. }
  165. }
  166. }
  167. private void ValidatePathSubstitutions(ServerConfiguration newConfig)
  168. {
  169. foreach (var map in newConfig.PathSubstitutions)
  170. {
  171. if (string.IsNullOrWhiteSpace(map.From) || string.IsNullOrWhiteSpace(map.To))
  172. {
  173. throw new ArgumentException("Invalid path substitution");
  174. }
  175. }
  176. }
  177. /// <summary>
  178. /// Replaces the item by name path.
  179. /// </summary>
  180. /// <param name="newConfig">The new configuration.</param>
  181. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  182. private void ValidateItemByNamePath(ServerConfiguration newConfig)
  183. {
  184. var newPath = newConfig.ItemsByNamePath;
  185. if (!string.IsNullOrWhiteSpace(newPath)
  186. && !string.Equals(Configuration.ItemsByNamePath ?? string.Empty, newPath))
  187. {
  188. // Validate
  189. if (!FileSystem.DirectoryExists(newPath))
  190. {
  191. throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
  192. }
  193. EnsureWriteAccess(newPath);
  194. }
  195. }
  196. /// <summary>
  197. /// Validates the metadata path.
  198. /// </summary>
  199. /// <param name="newConfig">The new configuration.</param>
  200. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  201. private void ValidateMetadataPath(ServerConfiguration newConfig)
  202. {
  203. var newPath = newConfig.MetadataPath;
  204. if (!string.IsNullOrWhiteSpace(newPath)
  205. && !string.Equals(Configuration.MetadataPath ?? string.Empty, newPath))
  206. {
  207. // Validate
  208. if (!FileSystem.DirectoryExists(newPath))
  209. {
  210. throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
  211. }
  212. EnsureWriteAccess(newPath);
  213. }
  214. }
  215. public void DisableMetadataService(string service)
  216. {
  217. DisableMetadataService(typeof(Movie), Configuration, service);
  218. DisableMetadataService(typeof(Episode), Configuration, service);
  219. DisableMetadataService(typeof(Series), Configuration, service);
  220. DisableMetadataService(typeof(Season), Configuration, service);
  221. DisableMetadataService(typeof(MusicArtist), Configuration, service);
  222. DisableMetadataService(typeof(MusicAlbum), Configuration, service);
  223. DisableMetadataService(typeof(MusicVideo), Configuration, service);
  224. DisableMetadataService(typeof(Video), Configuration, service);
  225. }
  226. private void DisableMetadataService(Type type, ServerConfiguration config, string service)
  227. {
  228. var options = GetMetadataOptions(type, config);
  229. if (!options.DisabledMetadataSavers.Contains(service, StringComparer.OrdinalIgnoreCase))
  230. {
  231. var list = options.DisabledMetadataSavers.ToList();
  232. list.Add(service);
  233. options.DisabledMetadataSavers = list.ToArray();
  234. }
  235. }
  236. private MetadataOptions GetMetadataOptions(Type type, ServerConfiguration config)
  237. {
  238. var options = config.MetadataOptions
  239. .FirstOrDefault(i => string.Equals(i.ItemType, type.Name, StringComparison.OrdinalIgnoreCase));
  240. if (options == null)
  241. {
  242. var list = config.MetadataOptions.ToList();
  243. options = new MetadataOptions
  244. {
  245. ItemType = type.Name
  246. };
  247. list.Add(options);
  248. config.MetadataOptions = list.ToArray();
  249. }
  250. return options;
  251. }
  252. }
  253. }