ServerConfigurationManager.cs 10 KB

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