ResolverHelper.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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, item.Path, 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, item.Path, 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. private static void EnsureName(BaseItem item, string fullPath, FileSystemMetadata fileInfo)
  76. {
  77. // If the subclass didn't supply a name, add it here
  78. if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(fullPath))
  79. {
  80. var fileName = fileInfo == null ? Path.GetFileName(fullPath) : fileInfo.Name;
  81. item.Name = GetDisplayName(fileName, fileInfo != null && fileInfo.IsDirectory);
  82. }
  83. }
  84. /// <summary>
  85. /// Gets the display name.
  86. /// </summary>
  87. /// <param name="path">The path.</param>
  88. /// <param name="isDirectory">if set to <c>true</c> [is directory].</param>
  89. /// <returns>System.String.</returns>
  90. private static string GetDisplayName(string path, bool isDirectory)
  91. {
  92. return isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path);
  93. }
  94. /// <summary>
  95. /// The MB name regex
  96. /// </summary>
  97. private static readonly Regex MbNameRegex = new Regex(@"(\[.*?\])");
  98. internal static string StripBrackets(string inputString)
  99. {
  100. var output = MbNameRegex.Replace(inputString, string.Empty).Trim();
  101. return Regex.Replace(output, @"\s+", " ");
  102. }
  103. /// <summary>
  104. /// Ensures DateCreated and DateModified have values
  105. /// </summary>
  106. /// <param name="fileSystem">The file system.</param>
  107. /// <param name="item">The item.</param>
  108. /// <param name="args">The args.</param>
  109. private static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args)
  110. {
  111. if (fileSystem == null)
  112. {
  113. throw new ArgumentNullException("fileSystem");
  114. }
  115. if (item == null)
  116. {
  117. throw new ArgumentNullException("item");
  118. }
  119. if (args == null)
  120. {
  121. throw new ArgumentNullException("args");
  122. }
  123. // See if a different path came out of the resolver than what went in
  124. if (!string.Equals(args.Path, item.Path, StringComparison.OrdinalIgnoreCase))
  125. {
  126. var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
  127. if (childData != null)
  128. {
  129. SetDateCreated(item, fileSystem, childData);
  130. }
  131. else
  132. {
  133. var fileData = fileSystem.GetFileSystemInfo(item.Path);
  134. if (fileData.Exists)
  135. {
  136. SetDateCreated(item, fileSystem, fileData);
  137. }
  138. }
  139. }
  140. else
  141. {
  142. SetDateCreated(item, fileSystem, args.FileInfo);
  143. }
  144. }
  145. private static void SetDateCreated(BaseItem item, IFileSystem fileSystem, FileSystemMetadata info)
  146. {
  147. var config = BaseItem.ConfigurationManager.GetMetadataConfiguration();
  148. if (config.UseFileCreationTimeForDateAdded)
  149. {
  150. // directoryService.getFile may return null
  151. if (info != null)
  152. {
  153. item.DateCreated = fileSystem.GetCreationTimeUtc(info);
  154. }
  155. }
  156. else
  157. {
  158. item.DateCreated = DateTime.UtcNow;
  159. }
  160. }
  161. }
  162. }