2
0

ServerConfigurationManager.cs 9.8 KB

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