BaseItemExtensions.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Linq;
  2. using MediaBrowser.Model.Entities;
  3. using MediaBrowser.Model.IO;
  4. namespace MediaBrowser.Controller.Entities
  5. {
  6. public static class BaseItemExtensions
  7. {
  8. /// <summary>
  9. /// Gets the image path.
  10. /// </summary>
  11. /// <param name="item">The item.</param>
  12. /// <param name="imageType">Type of the image.</param>
  13. /// <returns>System.String.</returns>
  14. public static string GetImagePath(this BaseItem item, ImageType imageType)
  15. {
  16. return item.GetImagePath(imageType, 0);
  17. }
  18. public static bool HasImage(this BaseItem item, ImageType imageType)
  19. {
  20. return item.HasImage(imageType, 0);
  21. }
  22. /// <summary>
  23. /// Sets the image path.
  24. /// </summary>
  25. /// <param name="item">The item.</param>
  26. /// <param name="imageType">Type of the image.</param>
  27. /// <param name="file">The file.</param>
  28. public static void SetImagePath(this BaseItem item, ImageType imageType, FileSystemMetadata file)
  29. {
  30. item.SetImagePath(imageType, 0, file);
  31. }
  32. /// <summary>
  33. /// Sets the image path.
  34. /// </summary>
  35. /// <param name="item">The item.</param>
  36. /// <param name="imageType">Type of the image.</param>
  37. /// <param name="file">The file.</param>
  38. public static void SetImagePath(this BaseItem item, ImageType imageType, string file)
  39. {
  40. if (file.StartsWith("http", System.StringComparison.OrdinalIgnoreCase))
  41. {
  42. item.SetImage(new ItemImageInfo
  43. {
  44. Path = file,
  45. Type = imageType
  46. }, 0);
  47. }
  48. else
  49. {
  50. item.SetImagePath(imageType, BaseItem.FileSystem.GetFileInfo(file));
  51. }
  52. }
  53. /// <summary>
  54. /// Copies all properties on object. Skips properties that do not exist.
  55. /// </summary>
  56. /// <param name="source">The source object.</param>
  57. /// <param name="dest">The destination object.</param>
  58. public static void DeepCopy<T, TU>(this T source, TU dest)
  59. where T : BaseItem
  60. where TU : BaseItem
  61. {
  62. var sourceProps = typeof(T).GetProperties().Where(x => x.CanRead).ToList();
  63. var destProps = typeof(TU).GetProperties()
  64. .Where(x => x.CanWrite)
  65. .ToList();
  66. foreach (var sourceProp in sourceProps)
  67. {
  68. if (destProps.Any(x => x.Name == sourceProp.Name))
  69. {
  70. var p = destProps.First(x => x.Name == sourceProp.Name);
  71. p.SetValue(dest, sourceProp.GetValue(source, null), null);
  72. }
  73. }
  74. }
  75. /// <summary>
  76. /// Copies all properties on newly created object. Skips properties that do not exist.
  77. /// </summary>
  78. /// <param name="source">The source object.</param>
  79. public static TU DeepCopy<T, TU>(this T source)
  80. where T : BaseItem
  81. where TU : BaseItem, new()
  82. {
  83. var dest = new TU();
  84. source.DeepCopy(dest);
  85. return dest;
  86. }
  87. }
  88. }