PlaylistXmlSaver.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.IO;
  2. using System.Xml;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Playlists;
  7. using MediaBrowser.Model.IO;
  8. using Microsoft.Extensions.Logging;
  9. namespace MediaBrowser.LocalMetadata.Savers
  10. {
  11. /// <summary>
  12. /// Playlist xml saver.
  13. /// </summary>
  14. public class PlaylistXmlSaver : BaseXmlSaver
  15. {
  16. /// <summary>
  17. /// The default file name to use when creating a new playlist.
  18. /// </summary>
  19. public const string DefaultPlaylistFilename = "playlist.xml";
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="PlaylistXmlSaver"/> class.
  22. /// </summary>
  23. /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
  24. /// <param name="configurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
  25. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  26. /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
  27. /// <param name="userDataManager">Instance of the <see cref="IUserDataManager"/> interface.</param>
  28. /// <param name="logger">Instance of the <see cref="ILogger{PlaylistXmlSaver}"/> interface.</param>
  29. public PlaylistXmlSaver(IFileSystem fileSystem, IServerConfigurationManager configurationManager, ILibraryManager libraryManager, IUserManager userManager, IUserDataManager userDataManager, ILogger<PlaylistXmlSaver> logger)
  30. : base(fileSystem, configurationManager, libraryManager, userManager, userDataManager, logger)
  31. {
  32. }
  33. /// <inheritdoc />
  34. public override bool IsEnabledFor(BaseItem item, ItemUpdateType updateType)
  35. {
  36. if (!item.SupportsLocalMetadata)
  37. {
  38. return false;
  39. }
  40. return item is Playlist && updateType >= ItemUpdateType.MetadataImport;
  41. }
  42. /// <inheritdoc />
  43. protected override void WriteCustomElements(BaseItem item, XmlWriter writer)
  44. {
  45. var game = (Playlist)item;
  46. if (!string.IsNullOrEmpty(game.PlaylistMediaType))
  47. {
  48. writer.WriteElementString("PlaylistMediaType", game.PlaylistMediaType);
  49. }
  50. }
  51. /// <inheritdoc />
  52. protected override string GetLocalSavePath(BaseItem item)
  53. {
  54. return GetSavePath(item.Path);
  55. }
  56. /// <summary>
  57. /// Get the save path.
  58. /// </summary>
  59. /// <param name="itemPath">The item path.</param>
  60. /// <returns>The save path.</returns>
  61. public static string GetSavePath(string itemPath)
  62. {
  63. var path = itemPath;
  64. if (Playlist.IsPlaylistFile(path))
  65. {
  66. return Path.ChangeExtension(itemPath, ".xml");
  67. }
  68. return Path.Combine(path, DefaultPlaylistFilename);
  69. }
  70. }
  71. }