ResolverHelper.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.ResolveArgs = 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. item.DisplayMediaType = item.GetType().Name;
  35. // Make sure the item has a name
  36. EnsureName(item);
  37. item.DontFetchMeta = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1;
  38. // Make sure DateCreated and DateModified have values
  39. EntityResolutionHelper.EnsureDates(item, args);
  40. }
  41. /// <summary>
  42. /// Ensures the name.
  43. /// </summary>
  44. /// <param name="item">The item.</param>
  45. private static void EnsureName(BaseItem item)
  46. {
  47. // If the subclass didn't supply a name, add it here
  48. if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(item.Path))
  49. {
  50. //we use our resolve args name here to get the name of the containg folder, not actual video file
  51. item.Name = GetMBName(item.ResolveArgs.FileInfo.Name, (item.ResolveArgs.FileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory);
  52. }
  53. }
  54. /// <summary>
  55. /// The MB name regex
  56. /// </summary>
  57. private static readonly Regex MBNameRegex = new Regex("(\\[.*\\])", RegexOptions.Compiled);
  58. /// <summary>
  59. /// Strip out attribute items and return just the name we will use for items
  60. /// </summary>
  61. /// <param name="path">Assumed to be a file or directory path</param>
  62. /// <param name="isDirectory">if set to <c>true</c> [is directory].</param>
  63. /// <returns>The cleaned name</returns>
  64. private static string GetMBName(string path, bool isDirectory)
  65. {
  66. //first just get the file or directory name
  67. var fn = isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path);
  68. //now - strip out anything inside brackets
  69. fn = MBNameRegex.Replace(fn, string.Empty);
  70. return fn;
  71. }
  72. }
  73. }