ResolverHelper.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Library;
  4. using System;
  5. using System.IO;
  6. using System.Linq;
  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. /// <param name="fileSystem">The file system.</param>
  21. public static void SetInitialItemValues(BaseItem item, ItemResolveArgs args, IFileSystem fileSystem, ILibraryManager libraryManager)
  22. {
  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 = libraryManager.GetNewItemId(item.Path, item.GetType());
  34. // If the resolver didn't specify this
  35. if (string.IsNullOrEmpty(item.DisplayMediaType))
  36. {
  37. item.DisplayMediaType = item.GetType().Name;
  38. }
  39. // Make sure the item has a name
  40. EnsureName(item, args);
  41. item.IsLocked = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1 ||
  42. item.Parents.Any(i => i.IsLocked);
  43. // Make sure DateCreated and DateModified have values
  44. EntityResolutionHelper.EnsureDates(fileSystem, item, args, true);
  45. }
  46. /// <summary>
  47. /// Ensures the name.
  48. /// </summary>
  49. /// <param name="item">The item.</param>
  50. /// <param name="args">The arguments.</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 = GetDisplayName(args.FileInfo.Name, (args.FileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory);
  58. }
  59. }
  60. /// <summary>
  61. /// Gets the display name.
  62. /// </summary>
  63. /// <param name="path">The path.</param>
  64. /// <param name="isDirectory">if set to <c>true</c> [is directory].</param>
  65. /// <returns>System.String.</returns>
  66. private static string GetDisplayName(string path, bool isDirectory)
  67. {
  68. //first just get the file or directory name
  69. var fn = isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path);
  70. return fn;
  71. }
  72. /// <summary>
  73. /// The MB name regex
  74. /// </summary>
  75. private static readonly Regex MbNameRegex = new Regex(@"(\[.*?\])", RegexOptions.Compiled);
  76. internal static string StripBrackets(string inputString)
  77. {
  78. var output = MbNameRegex.Replace(inputString, string.Empty).Trim();
  79. return Regex.Replace(output, @"\s+", " ");
  80. }
  81. }
  82. }