MovieXmlSaver.cs 4.1 KB

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