ResolverHelper.cs 2.8 KB

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