2
0

PlaylistXmlSaver.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.IO;
  2. using System.Threading.Tasks;
  3. using System.Xml;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.Playlists;
  8. using MediaBrowser.Model.IO;
  9. using Microsoft.Extensions.Logging;
  10. namespace MediaBrowser.LocalMetadata.Savers
  11. {
  12. /// <summary>
  13. /// Playlist xml saver.
  14. /// </summary>
  15. public class PlaylistXmlSaver : BaseXmlSaver
  16. {
  17. /// <summary>
  18. /// The default file name to use when creating a new playlist.
  19. /// </summary>
  20. public const string DefaultPlaylistFilename = "playlist.xml";
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="PlaylistXmlSaver"/> class.
  23. /// </summary>
  24. /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
  25. /// <param name="configurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
  26. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  27. /// <param name="logger">Instance of the <see cref="ILogger{PlaylistXmlSaver}"/> interface.</param>
  28. public PlaylistXmlSaver(IFileSystem fileSystem, IServerConfigurationManager configurationManager, ILibraryManager libraryManager, ILogger<PlaylistXmlSaver> logger)
  29. : base(fileSystem, configurationManager, libraryManager, logger)
  30. {
  31. }
  32. /// <inheritdoc />
  33. public override bool IsEnabledFor(BaseItem item, ItemUpdateType updateType)
  34. {
  35. if (!item.SupportsLocalMetadata)
  36. {
  37. return false;
  38. }
  39. return item is Playlist && updateType >= ItemUpdateType.MetadataImport;
  40. }
  41. /// <inheritdoc />
  42. protected override Task WriteCustomElementsAsync(BaseItem item, XmlWriter writer)
  43. {
  44. var game = (Playlist)item;
  45. if (string.IsNullOrEmpty(game.PlaylistMediaType))
  46. {
  47. return Task.CompletedTask;
  48. }
  49. return writer.WriteElementStringAsync(null, "PlaylistMediaType", null, game.PlaylistMediaType);
  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. }