PlaylistXmlSaver.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.IO;
  2. using System.Threading.Tasks;
  3. using System.Xml;
  4. using Jellyfin.Data.Enums;
  5. using MediaBrowser.Controller.Configuration;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Controller.Playlists;
  9. using MediaBrowser.Model.IO;
  10. using Microsoft.Extensions.Logging;
  11. namespace MediaBrowser.LocalMetadata.Savers
  12. {
  13. /// <summary>
  14. /// Playlist xml saver.
  15. /// </summary>
  16. public class PlaylistXmlSaver : BaseXmlSaver
  17. {
  18. /// <summary>
  19. /// The default file name to use when creating a new playlist.
  20. /// </summary>
  21. public const string DefaultPlaylistFilename = "playlist.xml";
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="PlaylistXmlSaver"/> class.
  24. /// </summary>
  25. /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
  26. /// <param name="configurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
  27. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  28. /// <param name="logger">Instance of the <see cref="ILogger{PlaylistXmlSaver}"/> interface.</param>
  29. public PlaylistXmlSaver(IFileSystem fileSystem, IServerConfigurationManager configurationManager, ILibraryManager libraryManager, ILogger<PlaylistXmlSaver> logger)
  30. : base(fileSystem, configurationManager, libraryManager, 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 async Task WriteCustomElementsAsync(BaseItem item, XmlWriter writer)
  44. {
  45. var game = (Playlist)item;
  46. if (game.PlaylistMediaType == MediaType.Unknown)
  47. {
  48. return;
  49. }
  50. await writer.WriteElementStringAsync(null, "PlaylistMediaType", null, game.PlaylistMediaType.ToString()).ConfigureAwait(false);
  51. }
  52. /// <inheritdoc />
  53. protected override string GetLocalSavePath(BaseItem item)
  54. {
  55. return GetSavePath(item.Path);
  56. }
  57. /// <summary>
  58. /// Get the save path.
  59. /// </summary>
  60. /// <param name="itemPath">The item path.</param>
  61. /// <returns>The save path.</returns>
  62. public static string GetSavePath(string itemPath)
  63. {
  64. var path = itemPath;
  65. if (Playlist.IsPlaylistFile(path))
  66. {
  67. return Path.ChangeExtension(itemPath, ".xml");
  68. }
  69. return Path.Combine(path, DefaultPlaylistFilename);
  70. }
  71. }
  72. }