ResolverHelper.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Controller.Providers;
  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="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.Parent = parent;
  36. }
  37. item.Id = libraryManager.GetNewItemId(item.Path, item.GetType());
  38. // If the resolver didn't specify this
  39. if (string.IsNullOrEmpty(item.DisplayMediaType))
  40. {
  41. item.DisplayMediaType = item.GetType().Name;
  42. }
  43. item.IsLocked = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1 ||
  44. item.Parents.Any(i => i.IsLocked);
  45. // Make sure DateCreated and DateModified have values
  46. var fileInfo = directoryService.GetFile(item.Path);
  47. item.DateModified = fileSystem.GetLastWriteTimeUtc(fileInfo);
  48. SetDateCreated(item, fileSystem, fileInfo);
  49. EnsureName(item, fileInfo);
  50. }
  51. /// <summary>
  52. /// Sets the initial item values.
  53. /// </summary>
  54. /// <param name="item">The item.</param>
  55. /// <param name="args">The args.</param>
  56. /// <param name="fileSystem">The file system.</param>
  57. /// <param name="libraryManager">The library manager.</param>
  58. public static void SetInitialItemValues(BaseItem item, ItemResolveArgs args, IFileSystem fileSystem, ILibraryManager libraryManager)
  59. {
  60. // If the resolver didn't specify this
  61. if (string.IsNullOrEmpty(item.Path))
  62. {
  63. item.Path = args.Path;
  64. }
  65. // If the resolver didn't specify this
  66. if (args.Parent != null)
  67. {
  68. item.Parent = args.Parent;
  69. }
  70. item.Id = libraryManager.GetNewItemId(item.Path, item.GetType());
  71. // If the resolver didn't specify this
  72. if (string.IsNullOrEmpty(item.DisplayMediaType))
  73. {
  74. item.DisplayMediaType = item.GetType().Name;
  75. }
  76. // Make sure the item has a name
  77. EnsureName(item, args.FileInfo);
  78. item.IsLocked = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1 ||
  79. item.Parents.Any(i => i.IsLocked);
  80. // Make sure DateCreated and DateModified have values
  81. EnsureDates(fileSystem, item, args, true);
  82. }
  83. /// <summary>
  84. /// Ensures the name.
  85. /// </summary>
  86. /// <param name="item">The item.</param>
  87. /// <param name="fileInfo">The file information.</param>
  88. private static void EnsureName(BaseItem item, FileSystemInfo fileInfo)
  89. {
  90. // If the subclass didn't supply a name, add it here
  91. if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(item.Path))
  92. {
  93. item.Name = GetDisplayName(fileInfo.Name, (fileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory);
  94. }
  95. }
  96. /// <summary>
  97. /// Gets the display name.
  98. /// </summary>
  99. /// <param name="path">The path.</param>
  100. /// <param name="isDirectory">if set to <c>true</c> [is directory].</param>
  101. /// <returns>System.String.</returns>
  102. private static string GetDisplayName(string path, bool isDirectory)
  103. {
  104. return isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path);
  105. }
  106. /// <summary>
  107. /// The MB name regex
  108. /// </summary>
  109. private static readonly Regex MbNameRegex = new Regex(@"(\[.*?\])", RegexOptions.Compiled);
  110. internal static string StripBrackets(string inputString)
  111. {
  112. var output = MbNameRegex.Replace(inputString, string.Empty).Trim();
  113. return Regex.Replace(output, @"\s+", " ");
  114. }
  115. /// <summary>
  116. /// Ensures DateCreated and DateModified have values
  117. /// </summary>
  118. /// <param name="fileSystem">The file system.</param>
  119. /// <param name="item">The item.</param>
  120. /// <param name="args">The args.</param>
  121. /// <param name="includeCreationTime">if set to <c>true</c> [include creation time].</param>
  122. private static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args, bool includeCreationTime)
  123. {
  124. if (fileSystem == null)
  125. {
  126. throw new ArgumentNullException("fileSystem");
  127. }
  128. if (item == null)
  129. {
  130. throw new ArgumentNullException("item");
  131. }
  132. if (args == null)
  133. {
  134. throw new ArgumentNullException("args");
  135. }
  136. // See if a different path came out of the resolver than what went in
  137. if (!string.Equals(args.Path, item.Path, StringComparison.OrdinalIgnoreCase))
  138. {
  139. var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
  140. if (childData != null)
  141. {
  142. if (includeCreationTime)
  143. {
  144. SetDateCreated(item, fileSystem, childData);
  145. }
  146. item.DateModified = fileSystem.GetLastWriteTimeUtc(childData);
  147. }
  148. else
  149. {
  150. var fileData = fileSystem.GetFileSystemInfo(item.Path);
  151. if (fileData.Exists)
  152. {
  153. if (includeCreationTime)
  154. {
  155. SetDateCreated(item, fileSystem, fileData);
  156. }
  157. item.DateModified = fileSystem.GetLastWriteTimeUtc(fileData);
  158. }
  159. }
  160. }
  161. else
  162. {
  163. if (includeCreationTime)
  164. {
  165. SetDateCreated(item, fileSystem, args.FileInfo);
  166. }
  167. item.DateModified = fileSystem.GetLastWriteTimeUtc(args.FileInfo);
  168. }
  169. }
  170. private static void SetDateCreated(BaseItem item, IFileSystem fileSystem, FileSystemInfo info)
  171. {
  172. var config = BaseItem.ConfigurationManager.GetMetadataConfiguration();
  173. if (config.UseFileCreationTimeForDateAdded)
  174. {
  175. item.DateCreated = fileSystem.GetCreationTimeUtc(info);
  176. }
  177. else
  178. {
  179. item.DateCreated = DateTime.UtcNow;
  180. }
  181. }
  182. }
  183. }