ServerConfigurationManager.cs 9.5 KB

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