ServerConfigurationManager.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. namespace MediaBrowser.Server.Implementations.Configuration
  19. {
  20. /// <summary>
  21. /// Class ServerConfigurationManager
  22. /// </summary>
  23. public class ServerConfigurationManager : BaseConfigurationManager, IServerConfigurationManager
  24. {
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="ServerConfigurationManager" /> class.
  27. /// </summary>
  28. /// <param name="applicationPaths">The application paths.</param>
  29. /// <param name="logManager">The log manager.</param>
  30. /// <param name="xmlSerializer">The XML serializer.</param>
  31. public ServerConfigurationManager(IApplicationPaths applicationPaths, ILogManager logManager, IXmlSerializer xmlSerializer)
  32. : base(applicationPaths, logManager, xmlSerializer)
  33. {
  34. UpdateItemsByNamePath();
  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. UpdateMetadataPath();
  69. base.OnConfigurationUpdated();
  70. }
  71. public override void AddParts(IEnumerable<IConfigurationFactory> factories)
  72. {
  73. base.AddParts(factories);
  74. UpdateTranscodingTempPath();
  75. }
  76. /// <summary>
  77. /// Updates the items by name path.
  78. /// </summary>
  79. private void UpdateItemsByNamePath()
  80. {
  81. if (!Configuration.MergeMetadataAndImagesByName)
  82. {
  83. ((ServerApplicationPaths)ApplicationPaths).ItemsByNamePath = string.IsNullOrEmpty(Configuration.ItemsByNamePath) ?
  84. null :
  85. Configuration.ItemsByNamePath;
  86. }
  87. }
  88. /// <summary>
  89. /// Updates the metadata path.
  90. /// </summary>
  91. private void UpdateMetadataPath()
  92. {
  93. string metadataPath;
  94. if (string.IsNullOrWhiteSpace(Configuration.MetadataPath))
  95. {
  96. metadataPath = GetInternalMetadataPath();
  97. }
  98. else if (Configuration.EnableCustomPathSubFolders)
  99. {
  100. metadataPath = Path.Combine(Configuration.MetadataPath, "metadata");
  101. }
  102. else
  103. {
  104. metadataPath = Configuration.MetadataPath;
  105. }
  106. ((ServerApplicationPaths)ApplicationPaths).InternalMetadataPath = metadataPath;
  107. if (Configuration.MergeMetadataAndImagesByName)
  108. {
  109. ((ServerApplicationPaths)ApplicationPaths).ItemsByNamePath = ((ServerApplicationPaths)ApplicationPaths).InternalMetadataPath;
  110. }
  111. }
  112. private string GetInternalMetadataPath()
  113. {
  114. if (Configuration.EnableStandaloneMetadata)
  115. {
  116. return Path.Combine(ApplicationPaths.ProgramDataPath, "metadata");
  117. }
  118. return null;
  119. }
  120. /// <summary>
  121. /// Updates the transcoding temporary path.
  122. /// </summary>
  123. private void UpdateTranscodingTempPath()
  124. {
  125. var encodingConfig = this.GetConfiguration<EncodingOptions>("encoding");
  126. ((ServerApplicationPaths)ApplicationPaths).TranscodingTempPath = string.IsNullOrEmpty(encodingConfig.TranscodingTempPath) ?
  127. null :
  128. Path.Combine(encodingConfig.TranscodingTempPath, "transcoding-temp");
  129. }
  130. protected override void OnNamedConfigurationUpdated(string key, object configuration)
  131. {
  132. base.OnNamedConfigurationUpdated(key, configuration);
  133. if (string.Equals(key, "encoding", StringComparison.OrdinalIgnoreCase))
  134. {
  135. UpdateTranscodingTempPath();
  136. }
  137. }
  138. /// <summary>
  139. /// Replaces the configuration.
  140. /// </summary>
  141. /// <param name="newConfiguration">The new configuration.</param>
  142. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  143. public override void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration)
  144. {
  145. var newConfig = (ServerConfiguration)newConfiguration;
  146. ValidateItemByNamePath(newConfig);
  147. ValidatePathSubstitutions(newConfig);
  148. ValidateMetadataPath(newConfig);
  149. EventHelper.FireEventIfNotNull(ConfigurationUpdating, this, new GenericEventArgs<ServerConfiguration> { Argument = newConfig }, Logger);
  150. base.ReplaceConfiguration(newConfiguration);
  151. }
  152. private void ValidatePathSubstitutions(ServerConfiguration newConfig)
  153. {
  154. foreach (var map in newConfig.PathSubstitutions)
  155. {
  156. if (string.IsNullOrWhiteSpace(map.From) || string.IsNullOrWhiteSpace(map.To))
  157. {
  158. throw new ArgumentException("Invalid path substitution");
  159. }
  160. }
  161. }
  162. /// <summary>
  163. /// Replaces the item by name path.
  164. /// </summary>
  165. /// <param name="newConfig">The new configuration.</param>
  166. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  167. private void ValidateItemByNamePath(ServerConfiguration newConfig)
  168. {
  169. var newPath = newConfig.ItemsByNamePath;
  170. if (!string.IsNullOrWhiteSpace(newPath)
  171. && !string.Equals(Configuration.ItemsByNamePath ?? string.Empty, newPath))
  172. {
  173. // Validate
  174. if (!_fileSystem.DirectoryExists(newPath))
  175. {
  176. throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
  177. }
  178. }
  179. }
  180. /// <summary>
  181. /// Validates the metadata path.
  182. /// </summary>
  183. /// <param name="newConfig">The new configuration.</param>
  184. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  185. private void ValidateMetadataPath(ServerConfiguration newConfig)
  186. {
  187. var newPath = newConfig.MetadataPath;
  188. if (!string.IsNullOrWhiteSpace(newPath)
  189. && !string.Equals(Configuration.MetadataPath ?? string.Empty, newPath))
  190. {
  191. // Validate
  192. if (!_fileSystem.DirectoryExists(newPath))
  193. {
  194. throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
  195. }
  196. }
  197. }
  198. public void DisableMetadataService(string service)
  199. {
  200. DisableMetadataService(typeof(Movie), Configuration, service);
  201. DisableMetadataService(typeof(Episode), Configuration, service);
  202. DisableMetadataService(typeof(Series), Configuration, service);
  203. DisableMetadataService(typeof(Season), Configuration, service);
  204. DisableMetadataService(typeof(MusicArtist), Configuration, service);
  205. DisableMetadataService(typeof(MusicAlbum), Configuration, service);
  206. DisableMetadataService(typeof(MusicVideo), Configuration, service);
  207. DisableMetadataService(typeof(Video), Configuration, service);
  208. }
  209. private void DisableMetadataService(Type type, ServerConfiguration config, string service)
  210. {
  211. var options = GetMetadataOptions(type, config);
  212. if (!options.DisabledMetadataSavers.Contains(service, StringComparer.OrdinalIgnoreCase))
  213. {
  214. var list = options.DisabledMetadataSavers.ToList();
  215. list.Add(service);
  216. options.DisabledMetadataSavers = list.ToArray();
  217. }
  218. }
  219. private MetadataOptions GetMetadataOptions(Type type, ServerConfiguration config)
  220. {
  221. var options = config.MetadataOptions
  222. .FirstOrDefault(i => string.Equals(i.ItemType, type.Name, StringComparison.OrdinalIgnoreCase));
  223. if (options == null)
  224. {
  225. var list = config.MetadataOptions.ToList();
  226. options = new MetadataOptions
  227. {
  228. ItemType = type.Name
  229. };
  230. list.Add(options);
  231. config.MetadataOptions = list.ToArray();
  232. }
  233. return options;
  234. }
  235. }
  236. }