ResolverHelper.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #nullable enable
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.Providers;
  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="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, 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. if (fileInfo == null)
  42. {
  43. throw new FileNotFoundException("Can't find item path.", item.Path);
  44. }
  45. SetDateCreated(item, fileInfo);
  46. EnsureName(item, fileInfo);
  47. }
  48. /// <summary>
  49. /// Sets the initial item values.
  50. /// </summary>
  51. /// <param name="item">The item.</param>
  52. /// <param name="args">The args.</param>
  53. /// <param name="fileSystem">The file system.</param>
  54. /// <param name="libraryManager">The library manager.</param>
  55. public static void SetInitialItemValues(BaseItem item, ItemResolveArgs args, IFileSystem fileSystem, ILibraryManager libraryManager)
  56. {
  57. // If the resolver didn't specify this
  58. if (string.IsNullOrEmpty(item.Path))
  59. {
  60. item.Path = args.Path;
  61. }
  62. // If the resolver didn't specify this
  63. if (args.Parent != null)
  64. {
  65. item.SetParent(args.Parent);
  66. }
  67. item.Id = libraryManager.GetNewItemId(item.Path, item.GetType());
  68. // Make sure the item has a name
  69. EnsureName(item, args.FileInfo);
  70. item.IsLocked = item.Path.Contains("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) ||
  71. item.GetParents().Any(i => i.IsLocked);
  72. // Make sure DateCreated and DateModified have values
  73. EnsureDates(fileSystem, item, args);
  74. }
  75. /// <summary>
  76. /// Ensures the name.
  77. /// </summary>
  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 = fileInfo.IsDirectory ? fileInfo.Name : Path.GetFileNameWithoutExtension(fileInfo.Name);
  84. }
  85. }
  86. /// <summary>
  87. /// Ensures DateCreated and DateModified have values.
  88. /// </summary>
  89. /// <param name="fileSystem">The file system.</param>
  90. /// <param name="item">The item.</param>
  91. /// <param name="args">The args.</param>
  92. private static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args)
  93. {
  94. // See if a different path came out of the resolver than what went in
  95. if (!fileSystem.AreEqual(args.Path, item.Path))
  96. {
  97. var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
  98. if (childData != null)
  99. {
  100. SetDateCreated(item, childData);
  101. }
  102. else
  103. {
  104. var fileData = fileSystem.GetFileSystemInfo(item.Path);
  105. if (fileData.Exists)
  106. {
  107. SetDateCreated(item, fileData);
  108. }
  109. }
  110. }
  111. else
  112. {
  113. SetDateCreated(item, args.FileInfo);
  114. }
  115. }
  116. private static void SetDateCreated(BaseItem item, FileSystemMetadata? info)
  117. {
  118. var config = BaseItem.ConfigurationManager.GetMetadataConfiguration();
  119. if (config.UseFileCreationTimeForDateAdded)
  120. {
  121. // directoryService.getFile may return null
  122. if (info != null)
  123. {
  124. var dateCreated = info.CreationTimeUtc;
  125. if (dateCreated.Equals(DateTime.MinValue))
  126. {
  127. dateCreated = DateTime.UtcNow;
  128. }
  129. item.DateCreated = dateCreated;
  130. }
  131. }
  132. else
  133. {
  134. item.DateCreated = DateTime.UtcNow;
  135. }
  136. }
  137. }
  138. }