ResolverHelper.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. using MediaBrowser.Controller.Resolvers;
  7. namespace MediaBrowser.Server.Implementations.Library
  8. {
  9. /// <summary>
  10. /// Class ResolverHelper
  11. /// </summary>
  12. public static class ResolverHelper
  13. {
  14. /// <summary>
  15. /// Sets the initial item values.
  16. /// </summary>
  17. /// <param name="item">The item.</param>
  18. /// <param name="args">The args.</param>
  19. public static void SetInitialItemValues(BaseItem item, ItemResolveArgs args)
  20. {
  21. item.ResolveArgs = args;
  22. // If the resolver didn't specify this
  23. if (string.IsNullOrEmpty(item.Path))
  24. {
  25. item.Path = args.Path;
  26. }
  27. // If the resolver didn't specify this
  28. if (args.Parent != null)
  29. {
  30. item.Parent = args.Parent;
  31. }
  32. item.Id = item.Path.GetMBId(item.GetType());
  33. item.DisplayMediaType = item.GetType().Name;
  34. // Make sure the item has a name
  35. EnsureName(item);
  36. // Make sure DateCreated and DateModified have values
  37. EntityResolutionHelper.EnsureDates(item, args);
  38. }
  39. /// <summary>
  40. /// Ensures the name.
  41. /// </summary>
  42. /// <param name="item">The item.</param>
  43. private static void EnsureName(BaseItem item)
  44. {
  45. // If the subclass didn't supply a name, add it here
  46. if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(item.Path))
  47. {
  48. //we use our resolve args name here to get the name of the containg folder, not actual video file
  49. item.Name = GetMBName(item.ResolveArgs.FileInfo.cFileName, item.ResolveArgs.FileInfo.IsDirectory);
  50. }
  51. }
  52. /// <summary>
  53. /// The MB name regex
  54. /// </summary>
  55. private static readonly Regex MBNameRegex = new Regex("(\\[.*\\])", RegexOptions.Compiled);
  56. /// <summary>
  57. /// Strip out attribute items and return just the name we will use for items
  58. /// </summary>
  59. /// <param name="path">Assumed to be a file or directory path</param>
  60. /// <param name="isDirectory">if set to <c>true</c> [is directory].</param>
  61. /// <returns>The cleaned name</returns>
  62. private static string GetMBName(string path, bool isDirectory)
  63. {
  64. //first just get the file or directory name
  65. var fn = isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path);
  66. //now - strip out anything inside brackets
  67. fn = MBNameRegex.Replace(fn, string.Empty);
  68. return fn;
  69. }
  70. }
  71. }