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