2
0

PlaylistXmlSaver.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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="logger">Instance of the <see cref="ILogger{PlaylistXmlSaver}"/> interface.</param>
  27. public PlaylistXmlSaver(IFileSystem fileSystem, IServerConfigurationManager configurationManager, ILibraryManager libraryManager, ILogger<PlaylistXmlSaver> logger)
  28. : base(fileSystem, configurationManager, libraryManager, logger)
  29. {
  30. }
  31. /// <inheritdoc />
  32. public override bool IsEnabledFor(BaseItem item, ItemUpdateType updateType)
  33. {
  34. if (!item.SupportsLocalMetadata)
  35. {
  36. return false;
  37. }
  38. return item is Playlist && updateType >= ItemUpdateType.MetadataImport;
  39. }
  40. /// <inheritdoc />
  41. protected override void WriteCustomElements(BaseItem item, XmlWriter writer)
  42. {
  43. var game = (Playlist)item;
  44. if (!string.IsNullOrEmpty(game.PlaylistMediaType))
  45. {
  46. writer.WriteElementString("PlaylistMediaType", game.PlaylistMediaType);
  47. }
  48. }
  49. /// <inheritdoc />
  50. protected override string GetLocalSavePath(BaseItem item)
  51. {
  52. return GetSavePath(item.Path);
  53. }
  54. /// <summary>
  55. /// Get the save path.
  56. /// </summary>
  57. /// <param name="itemPath">The item path.</param>
  58. /// <returns>The save path.</returns>
  59. public static string GetSavePath(string itemPath)
  60. {
  61. var path = itemPath;
  62. if (Playlist.IsPlaylistFile(path))
  63. {
  64. return Path.ChangeExtension(itemPath, ".xml");
  65. }
  66. return Path.Combine(path, DefaultPlaylistFilename);
  67. }
  68. }
  69. }