ResolverHelper.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Providers;
  7. using MediaBrowser.Model.IO;
  8. namespace Emby.Server.Implementations.Library
  9. {
  10. /// <summary>
  11. /// Class ResolverHelper.
  12. /// </summary>
  13. public static class ResolverHelper
  14. {
  15. /// <summary>
  16. /// Sets the initial item values.
  17. /// </summary>
  18. /// <param name="item">The item.</param>
  19. /// <param name="parent">The parent.</param>
  20. /// <param name="fileSystem">The file system.</param>
  21. /// <param name="libraryManager">The library manager.</param>
  22. /// <param name="directoryService">The directory service.</param>
  23. /// <exception cref="ArgumentException">Item must have a path</exception>
  24. public static void SetInitialItemValues(BaseItem item, Folder parent, IFileSystem fileSystem, ILibraryManager libraryManager, IDirectoryService directoryService)
  25. {
  26. // This version of the below method has no ItemResolveArgs, so we have to require the path already being set
  27. if (string.IsNullOrEmpty(item.Path))
  28. {
  29. throw new ArgumentException("Item must have a Path");
  30. }
  31. // If the resolver didn't specify this
  32. if (parent != null)
  33. {
  34. item.SetParent(parent);
  35. }
  36. item.Id = libraryManager.GetNewItemId(item.Path, item.GetType());
  37. item.IsLocked = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1 ||
  38. item.GetParents().Any(i => i.IsLocked);
  39. // Make sure DateCreated and DateModified have values
  40. var fileInfo = directoryService.GetFile(item.Path);
  41. SetDateCreated(item, fileSystem, fileInfo);
  42. EnsureName(item, item.Path, fileInfo);
  43. }
  44. /// <summary>
  45. /// Sets the initial item values.
  46. /// </summary>
  47. /// <param name="item">The item.</param>
  48. /// <param name="args">The args.</param>
  49. /// <param name="fileSystem">The file system.</param>
  50. /// <param name="libraryManager">The library manager.</param>
  51. public static void SetInitialItemValues(BaseItem item, ItemResolveArgs args, IFileSystem fileSystem, ILibraryManager libraryManager)
  52. {
  53. // If the resolver didn't specify this
  54. if (string.IsNullOrEmpty(item.Path))
  55. {
  56. item.Path = args.Path;
  57. }
  58. // If the resolver didn't specify this
  59. if (args.Parent != null)
  60. {
  61. item.SetParent(args.Parent);
  62. }
  63. item.Id = libraryManager.GetNewItemId(item.Path, item.GetType());
  64. // Make sure the item has a name
  65. EnsureName(item, item.Path, args.FileInfo);
  66. item.IsLocked = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1 ||
  67. item.GetParents().Any(i => i.IsLocked);
  68. // Make sure DateCreated and DateModified have values
  69. EnsureDates(fileSystem, item, args);
  70. }
  71. /// <summary>
  72. /// Ensures the name.
  73. /// </summary>
  74. private static void EnsureName(BaseItem item, string fullPath, FileSystemMetadata fileInfo)
  75. {
  76. // If the subclass didn't supply a name, add it here
  77. if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(fullPath))
  78. {
  79. var fileName = fileInfo == null ? Path.GetFileName(fullPath) : fileInfo.Name;
  80. item.Name = GetDisplayName(fileName, fileInfo != null && fileInfo.IsDirectory);
  81. }
  82. }
  83. /// <summary>
  84. /// Gets the display name.
  85. /// </summary>
  86. /// <param name="path">The path.</param>
  87. /// <param name="isDirectory">if set to <c>true</c> [is directory].</param>
  88. /// <returns>System.String.</returns>
  89. private static string GetDisplayName(string path, bool isDirectory)
  90. {
  91. return isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path);
  92. }
  93. /// <summary>
  94. /// Ensures DateCreated and DateModified have values.
  95. /// </summary>
  96. /// <param name="fileSystem">The file system.</param>
  97. /// <param name="item">The item.</param>
  98. /// <param name="args">The args.</param>
  99. private static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args)
  100. {
  101. if (fileSystem == null)
  102. {
  103. throw new ArgumentNullException(nameof(fileSystem));
  104. }
  105. if (item == null)
  106. {
  107. throw new ArgumentNullException(nameof(item));
  108. }
  109. if (args == null)
  110. {
  111. throw new ArgumentNullException(nameof(args));
  112. }
  113. // See if a different path came out of the resolver than what went in
  114. if (!fileSystem.AreEqual(args.Path, item.Path))
  115. {
  116. var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
  117. if (childData != null)
  118. {
  119. SetDateCreated(item, fileSystem, childData);
  120. }
  121. else
  122. {
  123. var fileData = fileSystem.GetFileSystemInfo(item.Path);
  124. if (fileData.Exists)
  125. {
  126. SetDateCreated(item, fileSystem, fileData);
  127. }
  128. }
  129. }
  130. else
  131. {
  132. SetDateCreated(item, fileSystem, args.FileInfo);
  133. }
  134. }
  135. private static void SetDateCreated(BaseItem item, IFileSystem fileSystem, FileSystemMetadata info)
  136. {
  137. var config = BaseItem.ConfigurationManager.GetMetadataConfiguration();
  138. if (config.UseFileCreationTimeForDateAdded)
  139. {
  140. // directoryService.getFile may return null
  141. if (info != null)
  142. {
  143. var dateCreated = fileSystem.GetCreationTimeUtc(info);
  144. if (dateCreated.Equals(DateTime.MinValue))
  145. {
  146. dateCreated = DateTime.UtcNow;
  147. }
  148. item.DateCreated = dateCreated;
  149. }
  150. }
  151. else
  152. {
  153. item.DateCreated = DateTime.UtcNow;
  154. }
  155. }
  156. }
  157. }