BaseItemResolver.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using MediaBrowser.Controller.Events;
  5. using MediaBrowser.Controller.Providers;
  6. using MediaBrowser.Model.Entities;
  7. namespace MediaBrowser.Controller.Resolvers
  8. {
  9. public abstract class BaseItemResolver<T> : IBaseItemResolver
  10. where T : BaseItem, new ()
  11. {
  12. protected virtual T Resolve(ItemResolveEventArgs args)
  13. {
  14. return null;
  15. }
  16. /// <summary>
  17. /// Sets initial values on the newly resolved item
  18. /// </summary>
  19. protected virtual void SetItemValues(T item, ItemResolveEventArgs args)
  20. {
  21. // If the subclass didn't specify this
  22. if (string.IsNullOrEmpty(item.Path))
  23. {
  24. item.Path = args.Path;
  25. }
  26. // If the subclass didn't specify this
  27. if (args.Parent != null)
  28. {
  29. item.Parent = args.Parent;
  30. }
  31. item.Id = Kernel.GetMD5(item.Path);
  32. }
  33. public async Task<BaseItem> ResolvePath(ItemResolveEventArgs args)
  34. {
  35. T item = Resolve(args);
  36. if (item != null)
  37. {
  38. // Set initial values on the newly resolved item
  39. SetItemValues(item, args);
  40. // Make sure the item has a name
  41. EnsureName(item);
  42. // Make sure DateCreated and DateModified have values
  43. EnsureDates(item);
  44. await FetchMetadataFromProviders(item, args);
  45. }
  46. return item;
  47. }
  48. private async Task FetchMetadataFromProviders(T item, ItemResolveEventArgs args)
  49. {
  50. foreach (BaseMetadataProvider provider in Kernel.Instance.MetadataProviders)
  51. {
  52. if (provider.Supports(item))
  53. {
  54. await provider.Fetch(item, args);
  55. }
  56. }
  57. }
  58. private void EnsureName(T item)
  59. {
  60. // If the subclass didn't supply a name, add it here
  61. if (string.IsNullOrEmpty(item.Name))
  62. {
  63. item.Name = Path.GetFileNameWithoutExtension(item.Path);
  64. }
  65. }
  66. /// <summary>
  67. /// Ensures DateCreated and DateModified have values
  68. /// </summary>
  69. private void EnsureDates(T item)
  70. {
  71. // If the subclass didn't supply dates, add them here
  72. if (item.DateCreated == DateTime.MinValue)
  73. {
  74. item.DateCreated = Path.IsPathRooted(item.Path) ? File.GetCreationTime(item.Path) : DateTime.Now;
  75. }
  76. if (item.DateModified == DateTime.MinValue)
  77. {
  78. item.DateModified = Path.IsPathRooted(item.Path) ? File.GetLastWriteTime(item.Path) : DateTime.Now;
  79. }
  80. }
  81. }
  82. /// <summary>
  83. /// Weed this to keep a list of resolvers, since Resolvers are built with generics
  84. /// </summary>
  85. public interface IBaseItemResolver
  86. {
  87. Task<BaseItem> ResolvePath(ItemResolveEventArgs args);
  88. }
  89. }