BaseItemResolver.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using MediaBrowser.Controller.Events;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Controller.IO;
  7. using System.Collections.Generic;
  8. namespace MediaBrowser.Controller.Resolvers
  9. {
  10. public abstract class BaseItemResolver<T> : IBaseItemResolver
  11. where T : BaseItem, new()
  12. {
  13. protected virtual T Resolve(ItemResolveEventArgs args)
  14. {
  15. return null;
  16. }
  17. public virtual ResolverPriority Priority
  18. {
  19. get
  20. {
  21. return ResolverPriority.First;
  22. }
  23. }
  24. /// <summary>
  25. /// Sets initial values on the newly resolved item
  26. /// </summary>
  27. protected virtual void SetInitialItemValues(T item, ItemResolveEventArgs args)
  28. {
  29. // If the subclass didn't specify this
  30. if (string.IsNullOrEmpty(item.Path))
  31. {
  32. item.Path = args.Path;
  33. }
  34. // If the subclass didn't specify this
  35. if (args.Parent != null)
  36. {
  37. item.Parent = args.Parent;
  38. }
  39. item.Id = Kernel.GetMD5(item.Path);
  40. }
  41. public BaseItem ResolvePath(ItemResolveEventArgs args)
  42. {
  43. T item = Resolve(args);
  44. if (item != null)
  45. {
  46. // Set initial values on the newly resolved item
  47. SetInitialItemValues(item, args);
  48. // Make sure the item has a name
  49. EnsureName(item);
  50. // Make sure DateCreated and DateModified have values
  51. EnsureDates(item, args);
  52. }
  53. return item;
  54. }
  55. private void EnsureName(T item)
  56. {
  57. // If the subclass didn't supply a name, add it here
  58. if (string.IsNullOrEmpty(item.Name))
  59. {
  60. item.Name = Path.GetFileNameWithoutExtension(item.Path);
  61. }
  62. }
  63. /// <summary>
  64. /// Ensures DateCreated and DateModified have values
  65. /// </summary>
  66. private void EnsureDates(T item, ItemResolveEventArgs args)
  67. {
  68. if (!Path.IsPathRooted(item.Path))
  69. {
  70. return;
  71. }
  72. WIN32_FIND_DATA fileData = args.FileData;
  73. // See if a different path came out of the resolver than what went in
  74. if (!args.Path.Equals(item.Path, StringComparison.OrdinalIgnoreCase))
  75. {
  76. KeyValuePair<string, WIN32_FIND_DATA>? childData = args.GetFileSystemEntry(item.Path, null);
  77. if (childData != null)
  78. {
  79. fileData = childData.Value.Value;
  80. }
  81. else
  82. {
  83. fileData = FileData.GetFileData(item.Path);
  84. }
  85. }
  86. item.DateCreated = fileData.CreationTime;
  87. item.DateModified = fileData.LastWriteTime;
  88. }
  89. }
  90. /// <summary>
  91. /// Weed this to keep a list of resolvers, since Resolvers are built with generics
  92. /// </summary>
  93. public interface IBaseItemResolver
  94. {
  95. BaseItem ResolvePath(ItemResolveEventArgs args);
  96. ResolverPriority Priority { get; }
  97. }
  98. public enum ResolverPriority
  99. {
  100. First = 1,
  101. Second = 2,
  102. Third = 3,
  103. Last = 4
  104. }
  105. }