2
0

ResolverHelper.cs 5.6 KB

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