BaseItemExtensions.cs 3.5 KB

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