ResolverHelper.cs 6.6 KB

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