ResolverHelper.cs 7.3 KB

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