BaseItemResolver.cs 3.6 KB

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