jellyfin/MediaBrowser.LocalMetadata/Savers/BaseXmlSaver.cs

549 lines
19 KiB
C#
Raw Normal View History

using System;
2016-10-26 22:11:31 -07:00
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Xml;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
2016-10-30 00:02:23 -07:00
using MediaBrowser.Controller.Playlists;
2016-10-26 22:11:31 -07:00
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using Microsoft.Extensions.Logging;
2016-10-26 22:11:31 -07:00
namespace MediaBrowser.LocalMetadata.Savers
{
/// <inheritdoc />
2016-10-28 11:45:56 -07:00
public abstract class BaseXmlSaver : IMetadataFileSaver
2016-10-26 22:11:31 -07:00
{
/// <summary>
/// Gets the date added format.
/// </summary>
public const string DateAddedFormat = "yyyy-MM-dd HH:mm:ss";
2016-10-26 22:11:31 -07:00
private static readonly CultureInfo _usCulture = new CultureInfo("en-US");
/// <summary>
/// Initializes a new instance of the <see cref="BaseXmlSaver"/> 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="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
/// <param name="userDataManager">Instance of the <see cref="IUserDataManager"/> interface.</param>
/// <param name="logger">Instance of the <see cref="ILogger{BaseXmlSaver}"/> interface.</param>
2020-06-05 17:15:56 -07:00
public BaseXmlSaver(IFileSystem fileSystem, IServerConfigurationManager configurationManager, ILibraryManager libraryManager, IUserManager userManager, IUserDataManager userDataManager, ILogger<BaseXmlSaver> logger)
2016-10-26 22:11:31 -07:00
{
2016-10-28 11:45:56 -07:00
FileSystem = fileSystem;
ConfigurationManager = configurationManager;
LibraryManager = libraryManager;
UserManager = userManager;
UserDataManager = userDataManager;
2016-10-26 22:11:31 -07:00
Logger = logger;
}
/// <summary>
/// Gets the file system.
/// </summary>
2016-10-26 22:11:31 -07:00
protected IFileSystem FileSystem { get; private set; }
/// <summary>
/// Gets the configuration manager.
/// </summary>
2016-10-26 22:11:31 -07:00
protected IServerConfigurationManager ConfigurationManager { get; private set; }
/// <summary>
/// Gets the library manager.
/// </summary>
2016-10-26 22:11:31 -07:00
protected ILibraryManager LibraryManager { get; private set; }
/// <summary>
/// Gets the user manager.
/// </summary>
2016-10-26 22:11:31 -07:00
protected IUserManager UserManager { get; private set; }
/// <summary>
/// Gets the user data manager.
/// </summary>
2016-10-26 22:11:31 -07:00
protected IUserDataManager UserDataManager { get; private set; }
/// <summary>
/// Gets the logger.
/// </summary>
2020-06-05 17:15:56 -07:00
protected ILogger<BaseXmlSaver> Logger { get; private set; }
2016-10-26 22:11:31 -07:00
/// <inheritdoc />
public string Name => XmlProviderUtils.Name;
2016-10-26 22:11:31 -07:00
/// <inheritdoc />
2018-09-12 10:26:21 -07:00
public string GetSavePath(BaseItem item)
2016-10-26 22:11:31 -07:00
{
return GetLocalSavePath(item);
}
/// <summary>
/// Gets the save path.
/// </summary>
/// <param name="item">The item.</param>
/// <returns>System.String.</returns>
2018-09-12 10:26:21 -07:00
protected abstract string GetLocalSavePath(BaseItem item);
2016-10-26 22:11:31 -07:00
/// <summary>
/// Gets the name of the root element.
/// </summary>
/// <param name="item">The item.</param>
/// <returns>System.String.</returns>
2018-09-12 10:26:21 -07:00
protected virtual string GetRootElementName(BaseItem item)
2016-10-30 00:02:23 -07:00
{
return "Item";
}
2016-10-26 22:11:31 -07:00
/// <summary>
/// Determines whether [is enabled for] [the specified item].
/// </summary>
/// <param name="item">The item.</param>
/// <param name="updateType">Type of the update.</param>
/// <returns><c>true</c> if [is enabled for] [the specified item]; otherwise, <c>false</c>.</returns>
2018-09-12 10:26:21 -07:00
public abstract bool IsEnabledFor(BaseItem item, ItemUpdateType updateType);
2016-10-26 22:11:31 -07:00
/// <inheritdoc />
2018-09-12 10:26:21 -07:00
public void Save(BaseItem item, CancellationToken cancellationToken)
2016-10-26 22:11:31 -07:00
{
var path = GetSavePath(item);
using var memoryStream = new MemoryStream();
Save(item, memoryStream);
2016-10-26 22:11:31 -07:00
memoryStream.Position = 0;
2016-10-26 22:11:31 -07:00
cancellationToken.ThrowIfCancellationRequested();
2016-10-26 22:11:31 -07:00
SaveToFile(memoryStream, path);
2016-10-26 22:11:31 -07:00
}
private void SaveToFile(Stream stream, string path)
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
2018-09-12 10:26:21 -07:00
// On Windows, savint the file will fail if the file is hidden or readonly
FileSystem.SetAttributes(path, false, false);
2016-10-26 22:11:31 -07:00
2020-01-08 09:52:50 -07:00
using (var filestream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
2016-10-26 22:11:31 -07:00
{
stream.CopyTo(filestream);
}
2018-09-12 10:26:21 -07:00
if (ConfigurationManager.Configuration.SaveMetadataHidden)
2016-10-26 22:11:31 -07:00
{
2017-11-15 14:33:04 -07:00
SetHidden(path, true);
}
}
private void SetHidden(string path, bool hidden)
{
try
{
FileSystem.SetHidden(path, hidden);
}
catch (Exception ex)
{
2018-12-20 05:11:26 -07:00
Logger.LogError(ex, "Error setting hidden attribute on {path}", path);
2016-10-26 22:11:31 -07:00
}
}
private void Save(BaseItem item, Stream stream)
2016-10-26 22:11:31 -07:00
{
var settings = new XmlWriterSettings
{
Indent = true,
Encoding = Encoding.UTF8,
CloseOutput = false
};
2019-01-13 13:37:13 -07:00
using (var writer = XmlWriter.Create(stream, settings))
2016-10-26 22:11:31 -07:00
{
var root = GetRootElementName(item);
writer.WriteStartDocument(true);
writer.WriteStartElement(root);
2018-09-12 10:26:21 -07:00
var baseItem = item;
2016-10-26 22:11:31 -07:00
if (baseItem != null)
{
AddCommonNodes(baseItem, writer, LibraryManager);
2016-10-26 22:11:31 -07:00
}
WriteCustomElements(item, writer);
writer.WriteEndElement();
writer.WriteEndDocument();
}
}
/// <summary>
/// Write custom elements.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="writer">The xml writer.</param>
2018-09-12 10:26:21 -07:00
protected abstract void WriteCustomElements(BaseItem item, XmlWriter writer);
2016-10-26 22:11:31 -07:00
/// <summary>
/// Adds common notes.
2016-10-26 22:11:31 -07:00
/// </summary>
/// <param name="item">The item.</param>
/// <param name="writer">The xml writer.</param>
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
public static void AddCommonNodes(BaseItem item, XmlWriter writer, ILibraryManager libraryManager)
2016-10-26 22:11:31 -07:00
{
2016-10-30 00:02:23 -07:00
if (!string.IsNullOrEmpty(item.OfficialRating))
{
writer.WriteElementString("ContentRating", item.OfficialRating);
}
writer.WriteElementString("Added", item.DateCreated.ToLocalTime().ToString("G", CultureInfo.InvariantCulture));
2016-10-30 00:02:23 -07:00
2019-01-27 04:03:43 -07:00
writer.WriteElementString("LockData", item.IsLocked.ToString(CultureInfo.InvariantCulture).ToLowerInvariant());
2016-10-30 00:02:23 -07:00
if (item.LockedFields.Length > 0)
2016-10-30 00:48:34 -07:00
{
writer.WriteElementString("LockedFields", string.Join("|", item.LockedFields));
2016-10-30 00:48:34 -07:00
}
2016-10-30 00:02:23 -07:00
2016-10-30 11:42:45 -07:00
if (item.CriticRating.HasValue)
{
writer.WriteElementString("CriticRating", item.CriticRating.Value.ToString(_usCulture));
2016-10-30 11:42:45 -07:00
}
2016-10-30 00:02:23 -07:00
2016-10-30 11:42:45 -07:00
if (!string.IsNullOrEmpty(item.Overview))
{
writer.WriteElementString("Overview", item.Overview);
}
2016-10-30 00:02:23 -07:00
2016-10-30 21:28:23 -07:00
if (!string.IsNullOrEmpty(item.OriginalTitle))
{
writer.WriteElementString("OriginalTitle", item.OriginalTitle);
}
2016-10-30 21:28:23 -07:00
if (!string.IsNullOrEmpty(item.CustomRating))
{
writer.WriteElementString("CustomRating", item.CustomRating);
}
2016-10-30 00:02:23 -07:00
2016-10-30 21:28:23 -07:00
if (!string.IsNullOrEmpty(item.Name) && !(item is Episode))
{
writer.WriteElementString("LocalTitle", item.Name);
}
2016-10-30 00:02:23 -07:00
2017-05-19 09:36:43 -07:00
var forcedSortName = item.ForcedSortName;
if (!string.IsNullOrEmpty(forcedSortName))
2016-10-30 21:28:23 -07:00
{
2017-05-19 09:36:43 -07:00
writer.WriteElementString("SortTitle", forcedSortName);
2016-10-30 21:28:23 -07:00
}
2016-10-30 00:02:23 -07:00
2016-10-30 21:28:23 -07:00
if (item.PremiereDate.HasValue)
{
if (item is Person)
{
writer.WriteElementString("BirthDate", item.PremiereDate.Value.ToLocalTime().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
2016-10-30 21:28:23 -07:00
}
else if (!(item is Episode))
{
writer.WriteElementString("PremiereDate", item.PremiereDate.Value.ToLocalTime().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
2016-10-30 21:28:23 -07:00
}
}
2016-10-30 00:02:23 -07:00
2016-10-30 21:28:23 -07:00
if (item.EndDate.HasValue)
{
if (item is Person)
{
writer.WriteElementString("DeathDate", item.EndDate.Value.ToLocalTime().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
2016-10-30 21:28:23 -07:00
}
else if (!(item is Episode))
{
writer.WriteElementString("EndDate", item.EndDate.Value.ToLocalTime().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
2016-10-30 21:28:23 -07:00
}
}
2016-10-30 00:02:23 -07:00
if (item.RemoteTrailers.Count > 0)
2016-10-31 10:53:06 -07:00
{
2018-09-12 10:26:21 -07:00
writer.WriteStartElement("Trailers");
2016-10-30 00:02:23 -07:00
2018-09-12 10:26:21 -07:00
foreach (var trailer in item.RemoteTrailers)
{
writer.WriteElementString("Trailer", trailer.Url);
2016-10-31 10:53:06 -07:00
}
2018-09-12 10:26:21 -07:00
writer.WriteEndElement();
2016-10-31 10:53:06 -07:00
}
2016-10-30 00:02:23 -07:00
if (item.ProductionLocations.Length > 0)
2016-11-01 08:30:15 -07:00
{
writer.WriteStartElement("Countries");
2016-10-30 00:02:23 -07:00
2016-11-01 08:30:15 -07:00
foreach (var name in item.ProductionLocations)
{
writer.WriteElementString("Country", name);
}
2016-10-30 00:02:23 -07:00
2016-11-01 08:30:15 -07:00
writer.WriteEndElement();
}
2016-10-30 00:02:23 -07:00
2016-10-31 10:53:06 -07:00
var hasDisplayOrder = item as IHasDisplayOrder;
if (hasDisplayOrder != null && !string.IsNullOrEmpty(hasDisplayOrder.DisplayOrder))
{
writer.WriteElementString("DisplayOrder", hasDisplayOrder.DisplayOrder);
}
2016-11-01 08:30:15 -07:00
if (item.CommunityRating.HasValue)
{
writer.WriteElementString("Rating", item.CommunityRating.Value.ToString(_usCulture));
2016-11-01 08:30:15 -07:00
}
2016-10-30 00:02:23 -07:00
2016-10-31 10:53:06 -07:00
if (item.ProductionYear.HasValue && !(item is Person))
{
writer.WriteElementString("ProductionYear", item.ProductionYear.Value.ToString(_usCulture));
2016-10-31 10:53:06 -07:00
}
2016-10-30 00:02:23 -07:00
2016-11-01 08:30:15 -07:00
var hasAspectRatio = item as IHasAspectRatio;
if (hasAspectRatio != null)
{
if (!string.IsNullOrEmpty(hasAspectRatio.AspectRatio))
{
writer.WriteElementString("AspectRatio", hasAspectRatio.AspectRatio);
}
}
if (!string.IsNullOrEmpty(item.PreferredMetadataLanguage))
{
writer.WriteElementString("Language", item.PreferredMetadataLanguage);
}
2016-11-01 08:30:15 -07:00
if (!string.IsNullOrEmpty(item.PreferredMetadataCountryCode))
{
writer.WriteElementString("CountryCode", item.PreferredMetadataCountryCode);
}
// Use original runtime here, actual file runtime later in MediaInfo
var runTimeTicks = item.RunTimeTicks;
if (runTimeTicks.HasValue)
{
var timespan = TimeSpan.FromTicks(runTimeTicks!.Value);
2016-11-01 08:30:15 -07:00
writer.WriteElementString("RunningTime", Math.Floor(timespan.TotalMinutes).ToString(_usCulture));
2016-11-01 08:30:15 -07:00
}
if (item.ProviderIds != null)
{
foreach (var providerKey in item.ProviderIds.Keys)
{
var providerId = item.ProviderIds[providerKey];
if (!string.IsNullOrEmpty(providerId))
{
writer.WriteElementString(providerKey + "Id", providerId);
}
}
}
if (!string.IsNullOrWhiteSpace(item.Tagline))
{
writer.WriteStartElement("Taglines");
writer.WriteElementString("Tagline", item.Tagline);
writer.WriteEndElement();
}
2018-09-12 10:26:21 -07:00
if (item.Genres.Length > 0)
2016-11-01 08:30:15 -07:00
{
writer.WriteStartElement("Genres");
foreach (var genre in item.Genres)
{
writer.WriteElementString("Genre", genre);
}
writer.WriteEndElement();
}
if (item.Studios.Length > 0)
2016-11-01 08:30:15 -07:00
{
writer.WriteStartElement("Studios");
foreach (var studio in item.Studios)
{
writer.WriteElementString("Studio", studio);
}
writer.WriteEndElement();
}
2016-10-30 00:02:23 -07:00
if (item.Tags.Length > 0)
2016-10-31 12:09:18 -07:00
{
writer.WriteStartElement("Tags");
2016-10-30 00:02:23 -07:00
2016-10-31 12:09:18 -07:00
foreach (var tag in item.Tags)
{
writer.WriteElementString("Tag", tag);
}
2016-10-30 00:02:23 -07:00
2016-10-31 12:09:18 -07:00
writer.WriteEndElement();
}
2016-10-30 00:02:23 -07:00
2016-11-01 08:30:15 -07:00
var people = libraryManager.GetPeople(item);
2016-10-30 00:02:23 -07:00
2016-11-01 08:30:15 -07:00
if (people.Count > 0)
{
writer.WriteStartElement("Persons");
2016-10-30 00:02:23 -07:00
2016-11-01 08:30:15 -07:00
foreach (var person in people)
{
writer.WriteStartElement("Person");
writer.WriteElementString("Name", person.Name);
writer.WriteElementString("Type", person.Type);
writer.WriteElementString("Role", person.Role);
2016-10-30 00:02:23 -07:00
2016-11-01 08:30:15 -07:00
if (person.SortOrder.HasValue)
{
writer.WriteElementString("SortOrder", person.SortOrder.Value.ToString(_usCulture));
2016-11-01 08:30:15 -07:00
}
2016-10-30 00:02:23 -07:00
2016-11-01 08:30:15 -07:00
writer.WriteEndElement();
}
2016-10-30 00:02:23 -07:00
2016-11-01 08:30:15 -07:00
writer.WriteEndElement();
}
2016-10-30 00:02:23 -07:00
2016-10-31 10:53:06 -07:00
var boxset = item as BoxSet;
if (boxset != null)
{
AddLinkedChildren(boxset, writer, "CollectionItems", "CollectionItem");
}
2016-10-30 00:02:23 -07:00
2016-10-31 10:53:06 -07:00
var playlist = item as Playlist;
2018-09-12 10:26:21 -07:00
if (playlist != null && !Playlist.IsPlaylistFile(playlist.Path))
2016-10-31 10:53:06 -07:00
{
AddLinkedChildren(playlist, writer, "PlaylistItems", "PlaylistItem");
}
2016-10-30 00:02:23 -07:00
2016-10-31 10:53:06 -07:00
var hasShares = item as IHasShares;
if (hasShares != null)
{
AddShares(hasShares, writer);
}
2016-10-30 21:28:23 -07:00
AddMediaInfo(item, writer);
2016-10-30 00:02:23 -07:00
}
/// <summary>
/// Add shares.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="writer">The xml writer.</param>
2016-10-31 10:53:06 -07:00
public static void AddShares(IHasShares item, XmlWriter writer)
2016-10-30 00:02:23 -07:00
{
2016-10-31 10:53:06 -07:00
writer.WriteStartElement("Shares");
2016-10-30 00:02:23 -07:00
2016-10-31 10:53:06 -07:00
foreach (var share in item.Shares)
{
writer.WriteStartElement("Share");
2016-10-30 00:02:23 -07:00
2016-10-31 10:53:06 -07:00
writer.WriteElementString("UserId", share.UserId);
2019-01-27 04:03:43 -07:00
writer.WriteElementString(
"CanEdit",
share.CanEdit.ToString(CultureInfo.InvariantCulture).ToLowerInvariant());
2016-10-30 00:02:23 -07:00
2016-10-31 10:53:06 -07:00
writer.WriteEndElement();
}
2016-10-30 00:02:23 -07:00
2016-10-31 10:53:06 -07:00
writer.WriteEndElement();
2016-10-30 00:02:23 -07:00
}
/// <summary>
/// Appends the media info.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="writer">The xml writer.</param>
/// <typeparam name="T">Type of item.</typeparam>
2016-10-30 21:28:23 -07:00
public static void AddMediaInfo<T>(T item, XmlWriter writer)
2016-10-30 00:02:23 -07:00
where T : BaseItem
{
if (item is Video video)
2016-10-30 00:02:23 -07:00
{
if (video.Video3DFormat.HasValue)
{
switch (video.Video3DFormat.Value)
{
case Video3DFormat.FullSideBySide:
2016-10-30 21:28:23 -07:00
writer.WriteElementString("Format3D", "FSBS");
2016-10-30 00:02:23 -07:00
break;
case Video3DFormat.FullTopAndBottom:
2016-10-30 21:28:23 -07:00
writer.WriteElementString("Format3D", "FTAB");
2016-10-30 00:02:23 -07:00
break;
case Video3DFormat.HalfSideBySide:
2016-10-30 21:28:23 -07:00
writer.WriteElementString("Format3D", "HSBS");
2016-10-30 00:02:23 -07:00
break;
case Video3DFormat.HalfTopAndBottom:
2016-10-30 21:28:23 -07:00
writer.WriteElementString("Format3D", "HTAB");
2016-10-30 00:02:23 -07:00
break;
case Video3DFormat.MVC:
2016-10-30 21:28:23 -07:00
writer.WriteElementString("Format3D", "MVC");
2016-10-30 00:02:23 -07:00
break;
}
}
}
}
/// <summary>
/// ADd linked children.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="writer">The xml writer.</param>
/// <param name="pluralNodeName">The plural node name.</param>
/// <param name="singularNodeName">The singular node name.</param>
2016-10-31 10:53:06 -07:00
public static void AddLinkedChildren(Folder item, XmlWriter writer, string pluralNodeName, string singularNodeName)
2016-10-30 00:02:23 -07:00
{
2016-10-31 10:53:06 -07:00
var items = item.LinkedChildren
.Where(i => i.Type == LinkedChildType.Manual)
.ToList();
2016-10-30 00:02:23 -07:00
2016-10-31 10:53:06 -07:00
if (items.Count == 0)
{
return;
}
2016-10-30 00:02:23 -07:00
2016-10-31 10:53:06 -07:00
writer.WriteStartElement(pluralNodeName);
2016-10-30 00:02:23 -07:00
2016-10-31 10:53:06 -07:00
foreach (var link in items)
{
2018-09-12 10:26:21 -07:00
if (!string.IsNullOrWhiteSpace(link.Path) || !string.IsNullOrWhiteSpace(link.LibraryItemId))
2016-10-31 10:53:06 -07:00
{
writer.WriteStartElement(singularNodeName);
2018-09-12 10:26:21 -07:00
if (!string.IsNullOrWhiteSpace(link.Path))
{
writer.WriteElementString("Path", link.Path);
}
if (!string.IsNullOrWhiteSpace(link.LibraryItemId))
{
writer.WriteElementString("ItemId", link.LibraryItemId);
}
2016-10-31 10:53:06 -07:00
writer.WriteEndElement();
}
}
2016-10-30 00:02:23 -07:00
2016-10-31 10:53:06 -07:00
writer.WriteEndElement();
2016-10-26 22:11:31 -07:00
}
2018-09-12 10:26:21 -07:00
private bool IsPersonType(PersonInfo person, string type)
2016-10-26 22:11:31 -07:00
{
return string.Equals(person.Type, type, StringComparison.OrdinalIgnoreCase) || string.Equals(person.Role, type, StringComparison.OrdinalIgnoreCase);
}
}
}