2021-04-20 20:59:51 +00:00
|
|
|
#nullable enable
|
2020-08-22 19:56:24 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2021-04-20 20:59:51 +00:00
|
|
|
using System;
|
2019-01-13 20:01:16 +00:00
|
|
|
using System.Linq;
|
2018-12-27 23:27:57 +00:00
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Entities
|
|
|
|
{
|
|
|
|
public static class BaseItemExtensions
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the image path.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
/// <param name="imageType">Type of the image.</param>
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
public static string GetImagePath(this BaseItem item, ImageType imageType)
|
|
|
|
{
|
|
|
|
return item.GetImagePath(imageType, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool HasImage(this BaseItem item, ImageType imageType)
|
|
|
|
{
|
|
|
|
return item.HasImage(imageType, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sets the image path.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
/// <param name="imageType">Type of the image.</param>
|
|
|
|
/// <param name="file">The file.</param>
|
|
|
|
public static void SetImagePath(this BaseItem item, ImageType imageType, FileSystemMetadata file)
|
|
|
|
{
|
|
|
|
item.SetImagePath(imageType, 0, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sets the image path.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
/// <param name="imageType">Type of the image.</param>
|
|
|
|
/// <param name="file">The file.</param>
|
|
|
|
public static void SetImagePath(this BaseItem item, ImageType imageType, string file)
|
|
|
|
{
|
|
|
|
if (file.StartsWith("http", System.StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2020-10-12 18:05:11 +00:00
|
|
|
item.SetImage(
|
|
|
|
new ItemImageInfo
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
Path = file,
|
|
|
|
Type = imageType
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item.SetImagePath(imageType, BaseItem.FileSystem.GetFileInfo(file));
|
|
|
|
}
|
|
|
|
}
|
2019-01-07 23:27:46 +00:00
|
|
|
|
2018-12-30 00:12:33 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Copies all properties on object. Skips properties that do not exist.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="source">The source object.</param>
|
|
|
|
/// <param name="dest">The destination object.</param>
|
2019-01-07 23:27:46 +00:00
|
|
|
public static void DeepCopy<T, TU>(this T source, TU dest)
|
2021-04-20 20:59:51 +00:00
|
|
|
where T : BaseItem
|
|
|
|
where TU : BaseItem
|
2018-12-30 00:12:33 +00:00
|
|
|
{
|
2021-04-20 20:59:51 +00:00
|
|
|
if (source == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(source));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dest == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(dest));
|
|
|
|
}
|
|
|
|
|
2019-04-02 16:17:50 +00:00
|
|
|
var destProps = typeof(TU).GetProperties().Where(x => x.CanWrite).ToList();
|
2018-12-30 00:12:33 +00:00
|
|
|
|
2019-04-02 16:17:50 +00:00
|
|
|
foreach (var sourceProp in typeof(T).GetProperties())
|
2018-12-30 00:12:33 +00:00
|
|
|
{
|
2019-04-02 16:17:50 +00:00
|
|
|
// We should be able to write to the property
|
|
|
|
// for both the source and destination type
|
|
|
|
// This is only false when the derived type hides the base member
|
|
|
|
// (which we shouldn't copy anyway)
|
|
|
|
if (!sourceProp.CanRead || !sourceProp.CanWrite)
|
2018-12-30 00:12:33 +00:00
|
|
|
{
|
2019-04-02 16:17:50 +00:00
|
|
|
continue;
|
2018-12-30 00:12:33 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 16:17:50 +00:00
|
|
|
var v = sourceProp.GetValue(source);
|
|
|
|
if (v == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2018-12-30 00:12:33 +00:00
|
|
|
|
2019-04-02 16:17:50 +00:00
|
|
|
var p = destProps.Find(x => x.Name == sourceProp.Name);
|
|
|
|
if (p != null)
|
|
|
|
{
|
|
|
|
p.SetValue(dest, v);
|
|
|
|
}
|
|
|
|
}
|
2018-12-30 00:12:33 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 00:24:30 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Copies all properties on newly created object. Skips properties that do not exist.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="source">The source object.</param>
|
2019-01-07 23:27:46 +00:00
|
|
|
public static TU DeepCopy<T, TU>(this T source)
|
2021-04-20 20:59:51 +00:00
|
|
|
where T : BaseItem
|
|
|
|
where TU : BaseItem, new()
|
2018-12-30 00:24:30 +00:00
|
|
|
{
|
|
|
|
var dest = new TU();
|
|
|
|
source.DeepCopy(dest);
|
|
|
|
return dest;
|
|
|
|
}
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
}
|