ResolverHelper.cs 3.5 KB

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