ResolverHelper.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.IO;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Resolvers;
  7. using System;
  8. using System.IO;
  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. item.ResetResolveArgs(args);
  26. // If the resolver didn't specify this
  27. if (string.IsNullOrEmpty(item.Path))
  28. {
  29. item.Path = args.Path;
  30. }
  31. // If the resolver didn't specify this
  32. if (args.Parent != null)
  33. {
  34. item.Parent = args.Parent;
  35. }
  36. item.Id = item.Path.GetMBId(item.GetType());
  37. // If the resolver didn't specify this
  38. if (string.IsNullOrEmpty(item.DisplayMediaType))
  39. {
  40. item.DisplayMediaType = item.GetType().Name;
  41. }
  42. // Make sure the item has a name
  43. EnsureName(item);
  44. item.DontFetchMeta = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1;
  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)
  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(item.ResolveArgs.FileInfo.Name, (item.ResolveArgs.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 = MBNameRegex.Replace(fn, string.Empty);
  77. return fn;
  78. }
  79. }
  80. }