ResolverHelper.cs 3.1 KB

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