aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.LocalMetadata/Savers/PlaylistXmlSaver.cs
blob: 847add07f7867c3b0775b654c8fec80cf9d44548 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System.IO;
using System.Threading.Tasks;
using System.Xml;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Playlists;
using MediaBrowser.Model.IO;
using Microsoft.Extensions.Logging;

namespace MediaBrowser.LocalMetadata.Savers
{
    /// <summary>
    /// Playlist xml saver.
    /// </summary>
    public class PlaylistXmlSaver : BaseXmlSaver
    {
        /// <summary>
        /// The default file name to use when creating a new playlist.
        /// </summary>
        public const string DefaultPlaylistFilename = "playlist.xml";

        /// <summary>
        /// Initializes a new instance of the <see cref="PlaylistXmlSaver"/> class.
        /// </summary>
        /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
        /// <param name="configurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
        /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
        /// <param name="logger">Instance of the <see cref="ILogger{PlaylistXmlSaver}"/> interface.</param>
        public PlaylistXmlSaver(IFileSystem fileSystem, IServerConfigurationManager configurationManager, ILibraryManager libraryManager, ILogger<PlaylistXmlSaver> logger)
            : base(fileSystem, configurationManager, libraryManager, logger)
        {
        }

        /// <inheritdoc />
        public override bool IsEnabledFor(BaseItem item, ItemUpdateType updateType)
        {
            if (!item.SupportsLocalMetadata)
            {
                return false;
            }

            return item is Playlist && updateType >= ItemUpdateType.MetadataImport;
        }

        /// <inheritdoc />
        protected override Task WriteCustomElementsAsync(BaseItem item, XmlWriter writer)
        {
            var game = (Playlist)item;

            if (string.IsNullOrEmpty(game.PlaylistMediaType))
            {
                return Task.CompletedTask;
            }

            return writer.WriteElementStringAsync(null, "PlaylistMediaType", null, game.PlaylistMediaType);
        }

        /// <inheritdoc />
        protected override string GetLocalSavePath(BaseItem item)
        {
            return GetSavePath(item.Path);
        }

        /// <summary>
        /// Get the save path.
        /// </summary>
        /// <param name="itemPath">The item path.</param>
        /// <returns>The save path.</returns>
        public static string GetSavePath(string itemPath)
        {
            var path = itemPath;

            if (Playlist.IsPlaylistFile(path))
            {
                return Path.ChangeExtension(itemPath, ".xml");
            }

            return Path.Combine(path, DefaultPlaylistFilename);
        }
    }
}