ResolverHelper.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /// <param name="args">The arguments.</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 = GetDisplayName(args.FileInfo.Name, (args.FileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory);
  59. }
  60. }
  61. /// <summary>
  62. /// Gets the display name.
  63. /// </summary>
  64. /// <param name="path">The path.</param>
  65. /// <param name="isDirectory">if set to <c>true</c> [is directory].</param>
  66. /// <returns>System.String.</returns>
  67. private static string GetDisplayName(string path, bool isDirectory)
  68. {
  69. //first just get the file or directory name
  70. var fn = isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path);
  71. return fn;
  72. }
  73. /// <summary>
  74. /// The MB name regex
  75. /// </summary>
  76. private static readonly Regex MbNameRegex = new Regex(@"(\[.*?\])", RegexOptions.Compiled);
  77. internal static string StripBrackets(string inputString)
  78. {
  79. var output = MbNameRegex.Replace(inputString, string.Empty).Trim();
  80. return Regex.Replace(output, @"\s+", " ");
  81. }
  82. }
  83. }