MovieXmlSaver.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. public MovieXmlSaver(IItemRepository itemRepository, IServerConfigurationManager config)
  22. {
  23. _itemRepository = itemRepository;
  24. _config = config;
  25. }
  26. public string Name
  27. {
  28. get
  29. {
  30. return XmlProviderUtils.Name;
  31. }
  32. }
  33. /// <summary>
  34. /// Determines whether [is enabled for] [the specified item].
  35. /// </summary>
  36. /// <param name="item">The item.</param>
  37. /// <param name="updateType">Type of the update.</param>
  38. /// <returns><c>true</c> if [is enabled for] [the specified item]; otherwise, <c>false</c>.</returns>
  39. public bool IsEnabledFor(IHasMetadata item, ItemUpdateType updateType)
  40. {
  41. if (!item.SupportsLocalMetadata)
  42. {
  43. return false;
  44. }
  45. var video = item as Video;
  46. // Check parent for null to avoid running this against things like video backdrops
  47. if (video != null && !(item is Episode) && !video.IsOwnedItem)
  48. {
  49. return updateType >= ItemUpdateType.MetadataDownload;
  50. }
  51. return false;
  52. }
  53. /// <summary>
  54. /// Saves the specified item.
  55. /// </summary>
  56. /// <param name="item">The item.</param>
  57. /// <param name="cancellationToken">The cancellation token.</param>
  58. /// <returns>Task.</returns>
  59. public void Save(IHasMetadata item, CancellationToken cancellationToken)
  60. {
  61. var video = (Video)item;
  62. var builder = new StringBuilder();
  63. builder.Append("<Title>");
  64. XmlSaverHelpers.AddCommonNodes(video, builder);
  65. var musicVideo = item as MusicVideo;
  66. if (musicVideo != null)
  67. {
  68. if (musicVideo.Artists.Count > 0)
  69. {
  70. builder.Append("<Artist>" + SecurityElement.Escape(string.Join(";", musicVideo.Artists.ToArray())) + "</Artist>");
  71. }
  72. if (!string.IsNullOrEmpty(musicVideo.Album))
  73. {
  74. builder.Append("<Album>" + SecurityElement.Escape(musicVideo.Album) + "</Album>");
  75. }
  76. }
  77. var movie = item as Movie;
  78. if (movie != null)
  79. {
  80. if (!string.IsNullOrEmpty(movie.TmdbCollectionName))
  81. {
  82. builder.Append("<TmdbCollectionName>" + SecurityElement.Escape(movie.TmdbCollectionName) + "</TmdbCollectionName>");
  83. }
  84. }
  85. XmlSaverHelpers.AddMediaInfo(video, builder, _itemRepository);
  86. builder.Append("</Title>");
  87. var xmlFilePath = GetSavePath(item);
  88. XmlSaverHelpers.Save(builder, xmlFilePath, new List<string>
  89. {
  90. // Deprecated. No longer saving in this field.
  91. "IMDBrating",
  92. // Deprecated. No longer saving in this field.
  93. "Description",
  94. "Artist",
  95. "Album",
  96. "TmdbCollectionName"
  97. }, _config);
  98. }
  99. public string GetSavePath(IHasMetadata item)
  100. {
  101. return GetMovieSavePath((Video)item);
  102. }
  103. public static string GetMovieSavePath(Video item)
  104. {
  105. if (item.IsInMixedFolder)
  106. {
  107. return Path.ChangeExtension(item.Path, ".xml");
  108. }
  109. return Path.Combine(item.ContainingFolderPath, "movie.xml");
  110. }
  111. }
  112. }