MovieXmlSaver.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.Movies;
  4. using MediaBrowser.Controller.Entities.TV;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Persistence;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Security;
  10. using System.Text;
  11. using System.Threading;
  12. namespace MediaBrowser.LocalMetadata.Savers
  13. {
  14. /// <summary>
  15. /// Saves movie.xml for movies, trailers and music videos
  16. /// </summary>
  17. public class MovieXmlSaver : IMetadataFileSaver
  18. {
  19. private readonly IItemRepository _itemRepository;
  20. private readonly IServerConfigurationManager _config;
  21. private readonly ILibraryManager _libraryManager;
  22. public MovieXmlSaver(IItemRepository itemRepository, IServerConfigurationManager config, ILibraryManager libraryManager)
  23. {
  24. _itemRepository = itemRepository;
  25. _config = config;
  26. _libraryManager = libraryManager;
  27. }
  28. public string Name
  29. {
  30. get
  31. {
  32. return XmlProviderUtils.Name;
  33. }
  34. }
  35. /// <summary>
  36. /// Determines whether [is enabled for] [the specified item].
  37. /// </summary>
  38. /// <param name="item">The item.</param>
  39. /// <param name="updateType">Type of the update.</param>
  40. /// <returns><c>true</c> if [is enabled for] [the specified item]; otherwise, <c>false</c>.</returns>
  41. public bool IsEnabledFor(IHasMetadata item, ItemUpdateType updateType)
  42. {
  43. if (!item.SupportsLocalMetadata)
  44. {
  45. return false;
  46. }
  47. var video = item as Video;
  48. // Check parent for null to avoid running this against things like video backdrops
  49. if (video != null && !(item is Episode) && !video.IsOwnedItem)
  50. {
  51. // If it's a plain video, skip if content type is unset (unless editing)
  52. if (video.GetType() == typeof(Video))
  53. {
  54. if (updateType < ItemUpdateType.MetadataEdit && string.IsNullOrEmpty(_libraryManager.GetContentType(video)))
  55. {
  56. return false;
  57. }
  58. }
  59. return updateType >= ItemUpdateType.MetadataDownload;
  60. }
  61. return false;
  62. }
  63. /// <summary>
  64. /// Saves the specified item.
  65. /// </summary>
  66. /// <param name="item">The item.</param>
  67. /// <param name="cancellationToken">The cancellation token.</param>
  68. /// <returns>Task.</returns>
  69. public void Save(IHasMetadata item, CancellationToken cancellationToken)
  70. {
  71. var video = (Video)item;
  72. var builder = new StringBuilder();
  73. builder.Append("<Title>");
  74. XmlSaverHelpers.AddCommonNodes(video, builder);
  75. var musicVideo = item as MusicVideo;
  76. if (musicVideo != null)
  77. {
  78. if (musicVideo.Artists.Count > 0)
  79. {
  80. builder.Append("<Artist>" + SecurityElement.Escape(string.Join(";", musicVideo.Artists.ToArray())) + "</Artist>");
  81. }
  82. if (!string.IsNullOrEmpty(musicVideo.Album))
  83. {
  84. builder.Append("<Album>" + SecurityElement.Escape(musicVideo.Album) + "</Album>");
  85. }
  86. }
  87. var movie = item as Movie;
  88. if (movie != null)
  89. {
  90. if (!string.IsNullOrEmpty(movie.TmdbCollectionName))
  91. {
  92. builder.Append("<TmdbCollectionName>" + SecurityElement.Escape(movie.TmdbCollectionName) + "</TmdbCollectionName>");
  93. }
  94. }
  95. XmlSaverHelpers.AddMediaInfo(video, builder, _itemRepository);
  96. builder.Append("</Title>");
  97. var xmlFilePath = GetSavePath(item);
  98. XmlSaverHelpers.Save(builder, xmlFilePath, new List<string>
  99. {
  100. // Deprecated. No longer saving in this field.
  101. "IMDBrating",
  102. // Deprecated. No longer saving in this field.
  103. "Description",
  104. "Artist",
  105. "Album",
  106. "TmdbCollectionName"
  107. }, _config);
  108. }
  109. public string GetSavePath(IHasMetadata item)
  110. {
  111. return GetMovieSavePath((Video)item);
  112. }
  113. public static string GetMovieSavePath(Video item)
  114. {
  115. if (item.IsInMixedFolder)
  116. {
  117. return Path.ChangeExtension(item.Path, ".xml");
  118. }
  119. return Path.Combine(item.ContainingFolderPath, "movie.xml");
  120. }
  121. }
  122. }