ResolverHelper.cs 5.5 KB

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