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.IsNullOrEmpty(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. /// Ensures DateCreated and DateModified have values
  96. /// </summary>
  97. /// <param name="fileSystem">The file system.</param>
  98. /// <param name="item">The item.</param>
  99. /// <param name="args">The args.</param>
  100. private static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args)
  101. {
  102. if (fileSystem == null)
  103. {
  104. throw new ArgumentNullException("fileSystem");
  105. }
  106. if (item == null)
  107. {
  108. throw new ArgumentNullException("item");
  109. }
  110. if (args == null)
  111. {
  112. throw new ArgumentNullException("args");
  113. }
  114. // See if a different path came out of the resolver than what went in
  115. if (!fileSystem.AreEqual(args.Path, item.Path))
  116. {
  117. var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
  118. if (childData != null)
  119. {
  120. SetDateCreated(item, fileSystem, childData);
  121. }
  122. else
  123. {
  124. var fileData = fileSystem.GetFileSystemInfo(item.Path);
  125. if (fileData.Exists)
  126. {
  127. SetDateCreated(item, fileSystem, fileData);
  128. }
  129. }
  130. }
  131. else
  132. {
  133. SetDateCreated(item, fileSystem, args.FileInfo);
  134. }
  135. }
  136. private static void SetDateCreated(BaseItem item, IFileSystem fileSystem, FileSystemMetadata info)
  137. {
  138. var config = BaseItem.ConfigurationManager.GetMetadataConfiguration();
  139. if (config.UseFileCreationTimeForDateAdded)
  140. {
  141. // directoryService.getFile may return null
  142. if (info != null)
  143. {
  144. var dateCreated = fileSystem.GetCreationTimeUtc(info);
  145. if (dateCreated.Equals(DateTime.MinValue))
  146. {
  147. dateCreated = DateTime.UtcNow;
  148. }
  149. item.DateCreated = dateCreated;
  150. }
  151. }
  152. else
  153. {
  154. item.DateCreated = DateTime.UtcNow;
  155. }
  156. }
  157. }
  158. }