2
0

BaseItemExtensions.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Linq;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Model.IO;
  6. namespace MediaBrowser.Controller.Entities
  7. {
  8. public static class BaseItemExtensions
  9. {
  10. /// <summary>
  11. /// Gets the image path.
  12. /// </summary>
  13. /// <param name="item">The item.</param>
  14. /// <param name="imageType">Type of the image.</param>
  15. /// <returns>System.String.</returns>
  16. public static string GetImagePath(this BaseItem item, ImageType imageType)
  17. {
  18. return item.GetImagePath(imageType, 0);
  19. }
  20. public static bool HasImage(this BaseItem item, ImageType imageType)
  21. {
  22. return item.HasImage(imageType, 0);
  23. }
  24. /// <summary>
  25. /// Sets the image path.
  26. /// </summary>
  27. /// <param name="item">The item.</param>
  28. /// <param name="imageType">Type of the image.</param>
  29. /// <param name="file">The file.</param>
  30. public static void SetImagePath(this BaseItem item, ImageType imageType, FileSystemMetadata file)
  31. {
  32. item.SetImagePath(imageType, 0, file);
  33. }
  34. /// <summary>
  35. /// Sets the image path.
  36. /// </summary>
  37. /// <param name="item">The item.</param>
  38. /// <param name="imageType">Type of the image.</param>
  39. /// <param name="file">The file.</param>
  40. public static void SetImagePath(this BaseItem item, ImageType imageType, string file)
  41. {
  42. if (file.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  43. {
  44. item.SetImage(
  45. new ItemImageInfo
  46. {
  47. Path = file,
  48. Type = imageType
  49. },
  50. 0);
  51. }
  52. else
  53. {
  54. item.SetImagePath(imageType, BaseItem.FileSystem.GetFileInfo(file));
  55. }
  56. }
  57. /// <summary>
  58. /// Copies all properties on object. Skips properties that do not exist.
  59. /// </summary>
  60. /// <param name="source">The source object.</param>
  61. /// <param name="dest">The destination object.</param>
  62. /// <typeparam name="T">Source type.</typeparam>
  63. /// <typeparam name="TU">Destination type.</typeparam>
  64. public static void DeepCopy<T, TU>(this T source, TU dest)
  65. where T : BaseItem
  66. where TU : BaseItem
  67. {
  68. ArgumentNullException.ThrowIfNull(source);
  69. ArgumentNullException.ThrowIfNull(dest);
  70. var destProps = typeof(TU).GetProperties().Where(x => x.CanWrite).ToList();
  71. foreach (var sourceProp in typeof(T).GetProperties())
  72. {
  73. // We should be able to write to the property
  74. // for both the source and destination type
  75. // This is only false when the derived type hides the base member
  76. // (which we shouldn't copy anyway)
  77. if (!sourceProp.CanRead || !sourceProp.CanWrite)
  78. {
  79. continue;
  80. }
  81. var v = sourceProp.GetValue(source);
  82. if (v is null)
  83. {
  84. continue;
  85. }
  86. var p = destProps.Find(x => x.Name == sourceProp.Name);
  87. p?.SetValue(dest, v);
  88. }
  89. }
  90. /// <summary>
  91. /// Copies all properties on newly created object. Skips properties that do not exist.
  92. /// </summary>
  93. /// <param name="source">The source object.</param>
  94. /// <typeparam name="T">Source type.</typeparam>
  95. /// <typeparam name="TU">Destination type.</typeparam>
  96. /// <returns>Destination object.</returns>
  97. public static TU DeepCopy<T, TU>(this T source)
  98. where T : BaseItem
  99. where TU : BaseItem, new()
  100. {
  101. var dest = new TU();
  102. source.DeepCopy(dest);
  103. return dest;
  104. }
  105. }
  106. }