ResolverHelper.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.Providers;
  4. using System;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text.RegularExpressions;
  8. using MediaBrowser.Model.IO;
  9. namespace Emby.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="parent">The parent.</param>
  21. /// <param name="fileSystem">The file system.</param>
  22. /// <param name="libraryManager">The library manager.</param>
  23. /// <param name="directoryService">The directory service.</param>
  24. /// <exception cref="System.ArgumentException">Item must have a path</exception>
  25. public static void SetInitialItemValues(BaseItem item, Folder parent, IFileSystem fileSystem, ILibraryManager libraryManager, IDirectoryService directoryService)
  26. {
  27. // This version of the below method has no ItemResolveArgs, so we have to require the path already being set
  28. if (string.IsNullOrWhiteSpace(item.Path))
  29. {
  30. throw new ArgumentException("Item must have a Path");
  31. }
  32. // If the resolver didn't specify this
  33. if (parent != null)
  34. {
  35. item.SetParent(parent);
  36. }
  37. item.Id = libraryManager.GetNewItemId(item.Path, item.GetType());
  38. item.IsLocked = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1 ||
  39. item.GetParents().Any(i => i.IsLocked);
  40. // Make sure DateCreated and DateModified have values
  41. var fileInfo = directoryService.GetFile(item.Path);
  42. SetDateCreated(item, fileSystem, fileInfo);
  43. EnsureName(item, fileInfo);
  44. }
  45. /// <summary>
  46. /// Sets the initial item values.
  47. /// </summary>
  48. /// <param name="item">The item.</param>
  49. /// <param name="args">The args.</param>
  50. /// <param name="fileSystem">The file system.</param>
  51. /// <param name="libraryManager">The library manager.</param>
  52. public static void SetInitialItemValues(BaseItem item, ItemResolveArgs args, IFileSystem fileSystem, ILibraryManager libraryManager)
  53. {
  54. // If the resolver didn't specify this
  55. if (string.IsNullOrEmpty(item.Path))
  56. {
  57. item.Path = args.Path;
  58. }
  59. // If the resolver didn't specify this
  60. if (args.Parent != null)
  61. {
  62. item.SetParent(args.Parent);
  63. }
  64. item.Id = libraryManager.GetNewItemId(item.Path, item.GetType());
  65. // Make sure the item has a name
  66. EnsureName(item, args.FileInfo);
  67. item.IsLocked = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1 ||
  68. item.GetParents().Any(i => i.IsLocked);
  69. // Make sure DateCreated and DateModified have values
  70. EnsureDates(fileSystem, item, args);
  71. }
  72. /// <summary>
  73. /// Ensures the name.
  74. /// </summary>
  75. /// <param name="item">The item.</param>
  76. /// <param name="fileInfo">The file information.</param>
  77. private static void EnsureName(BaseItem item, FileSystemMetadata fileInfo)
  78. {
  79. // If the subclass didn't supply a name, add it here
  80. if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(item.Path))
  81. {
  82. item.Name = GetDisplayName(fileInfo.Name, fileInfo.IsDirectory);
  83. }
  84. }
  85. /// <summary>
  86. /// Gets the display name.
  87. /// </summary>
  88. /// <param name="path">The path.</param>
  89. /// <param name="isDirectory">if set to <c>true</c> [is directory].</param>
  90. /// <returns>System.String.</returns>
  91. private static string GetDisplayName(string path, bool isDirectory)
  92. {
  93. return isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path);
  94. }
  95. /// <summary>
  96. /// The MB name regex
  97. /// </summary>
  98. private static readonly Regex MbNameRegex = new Regex(@"(\[.*?\])");
  99. internal static string StripBrackets(string inputString)
  100. {
  101. var output = MbNameRegex.Replace(inputString, string.Empty).Trim();
  102. return Regex.Replace(output, @"\s+", " ");
  103. }
  104. /// <summary>
  105. /// Ensures DateCreated and DateModified have values
  106. /// </summary>
  107. /// <param name="fileSystem">The file system.</param>
  108. /// <param name="item">The item.</param>
  109. /// <param name="args">The args.</param>
  110. private static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args)
  111. {
  112. if (fileSystem == null)
  113. {
  114. throw new ArgumentNullException("fileSystem");
  115. }
  116. if (item == null)
  117. {
  118. throw new ArgumentNullException("item");
  119. }
  120. if (args == null)
  121. {
  122. throw new ArgumentNullException("args");
  123. }
  124. // See if a different path came out of the resolver than what went in
  125. if (!string.Equals(args.Path, item.Path, StringComparison.OrdinalIgnoreCase))
  126. {
  127. var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
  128. if (childData != null)
  129. {
  130. SetDateCreated(item, fileSystem, childData);
  131. }
  132. else
  133. {
  134. var fileData = fileSystem.GetFileSystemInfo(item.Path);
  135. if (fileData.Exists)
  136. {
  137. SetDateCreated(item, fileSystem, fileData);
  138. }
  139. }
  140. }
  141. else
  142. {
  143. SetDateCreated(item, fileSystem, args.FileInfo);
  144. }
  145. }
  146. private static void SetDateCreated(BaseItem item, IFileSystem fileSystem, FileSystemMetadata info)
  147. {
  148. var config = BaseItem.ConfigurationManager.GetMetadataConfiguration();
  149. if (config.UseFileCreationTimeForDateAdded)
  150. {
  151. item.DateCreated = fileSystem.GetCreationTimeUtc(info);
  152. }
  153. else
  154. {
  155. item.DateCreated = DateTime.UtcNow;
  156. }
  157. }
  158. }
  159. }