BaseItemResolver.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Library;
  4. using System.IO;
  5. using System.Text.RegularExpressions;
  6. namespace MediaBrowser.Controller.Resolvers
  7. {
  8. /// <summary>
  9. /// Class BaseItemResolver
  10. /// </summary>
  11. /// <typeparam name="T"></typeparam>
  12. public abstract class BaseItemResolver<T> : IBaseItemResolver
  13. where T : BaseItem, new()
  14. {
  15. /// <summary>
  16. /// Resolves the specified args.
  17. /// </summary>
  18. /// <param name="args">The args.</param>
  19. /// <returns>`0.</returns>
  20. protected virtual T Resolve(ItemResolveArgs args)
  21. {
  22. return null;
  23. }
  24. /// <summary>
  25. /// Gets the priority.
  26. /// </summary>
  27. /// <value>The priority.</value>
  28. public virtual ResolverPriority Priority
  29. {
  30. get
  31. {
  32. return ResolverPriority.First;
  33. }
  34. }
  35. /// <summary>
  36. /// Sets initial values on the newly resolved item
  37. /// </summary>
  38. /// <param name="item">The item.</param>
  39. /// <param name="args">The args.</param>
  40. protected virtual void SetInitialItemValues(T item, ItemResolveArgs args)
  41. {
  42. // If the subclass didn't specify this
  43. if (string.IsNullOrEmpty(item.Path))
  44. {
  45. item.Path = args.Path;
  46. }
  47. // If the subclass didn't specify this
  48. if (args.Parent != null)
  49. {
  50. item.Parent = args.Parent;
  51. }
  52. item.Id = item.Path.GetMBId(item.GetType());
  53. item.DisplayMediaType = item.GetType().Name;
  54. }
  55. /// <summary>
  56. /// Resolves the path.
  57. /// </summary>
  58. /// <param name="args">The args.</param>
  59. /// <returns>BaseItem.</returns>
  60. public BaseItem ResolvePath(ItemResolveArgs args)
  61. {
  62. T item = Resolve(args);
  63. if (item != null)
  64. {
  65. // Set the args on the item
  66. item.ResolveArgs = args;
  67. // Set initial values on the newly resolved item
  68. SetInitialItemValues(item, args);
  69. // Make sure the item has a name
  70. EnsureName(item);
  71. // Make sure DateCreated and DateModified have values
  72. EntityResolutionHelper.EnsureDates(item, args);
  73. }
  74. return item;
  75. }
  76. /// <summary>
  77. /// Ensures the name.
  78. /// </summary>
  79. /// <param name="item">The item.</param>
  80. private void EnsureName(T item)
  81. {
  82. // If the subclass didn't supply a name, add it here
  83. if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(item.Path))
  84. {
  85. //we use our resolve args name here to get the name of the containg folder, not actual video file
  86. item.Name = GetMBName(item.ResolveArgs.FileInfo.cFileName, item.ResolveArgs.FileInfo.IsDirectory);
  87. }
  88. }
  89. /// <summary>
  90. /// The MB name regex
  91. /// </summary>
  92. private static readonly Regex MBNameRegex = new Regex("(\\[.*\\])", RegexOptions.Compiled);
  93. /// <summary>
  94. /// Strip out attribute items and return just the name we will use for items
  95. /// </summary>
  96. /// <param name="path">Assumed to be a file or directory path</param>
  97. /// <param name="isDirectory">if set to <c>true</c> [is directory].</param>
  98. /// <returns>The cleaned name</returns>
  99. private static string GetMBName(string path, bool isDirectory)
  100. {
  101. //first just get the file or directory name
  102. var fn = isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path);
  103. //now - strip out anything inside brackets
  104. fn = MBNameRegex.Replace(fn, string.Empty);
  105. return fn;
  106. }
  107. }
  108. /// <summary>
  109. /// Weed this to keep a list of resolvers, since Resolvers are built with generics
  110. /// </summary>
  111. public interface IBaseItemResolver
  112. {
  113. /// <summary>
  114. /// Resolves the path.
  115. /// </summary>
  116. /// <param name="args">The args.</param>
  117. /// <returns>BaseItem.</returns>
  118. BaseItem ResolvePath(ItemResolveArgs args);
  119. /// <summary>
  120. /// Gets the priority.
  121. /// </summary>
  122. /// <value>The priority.</value>
  123. ResolverPriority Priority { get; }
  124. }
  125. /// <summary>
  126. /// Enum ResolverPriority
  127. /// </summary>
  128. public enum ResolverPriority
  129. {
  130. /// <summary>
  131. /// The first
  132. /// </summary>
  133. First = 1,
  134. /// <summary>
  135. /// The second
  136. /// </summary>
  137. Second = 2,
  138. /// <summary>
  139. /// The third
  140. /// </summary>
  141. Third = 3,
  142. /// <summary>
  143. /// The last
  144. /// </summary>
  145. Last = 4
  146. }
  147. }