ResolverHelper.cs 3.4 KB

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