CoreResolutionIgnoreRule.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using MediaBrowser.Model.Extensions;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Controller.Resolvers;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using MediaBrowser.Controller.IO;
  10. using MediaBrowser.Model.IO;
  11. using MediaBrowser.Model.Logging;
  12. namespace Emby.Server.Implementations.Library
  13. {
  14. /// <summary>
  15. /// Provides the core resolver ignore rules
  16. /// </summary>
  17. public class CoreResolutionIgnoreRule : IResolverIgnoreRule
  18. {
  19. private readonly IFileSystem _fileSystem;
  20. private readonly ILibraryManager _libraryManager;
  21. private readonly ILogger _logger;
  22. /// <summary>
  23. /// Any folder named in this list will be ignored - can be added to at runtime for extensibility
  24. /// </summary>
  25. public static readonly List<string> IgnoreFolders = new List<string>
  26. {
  27. "metadata",
  28. "ps3_update",
  29. "ps3_vprm",
  30. "extrafanart",
  31. "extrathumbs",
  32. ".actors",
  33. ".wd_tv",
  34. // Synology
  35. "@eaDir",
  36. "eaDir",
  37. "#recycle"
  38. };
  39. public CoreResolutionIgnoreRule(IFileSystem fileSystem, ILibraryManager libraryManager, ILogger logger)
  40. {
  41. _fileSystem = fileSystem;
  42. _libraryManager = libraryManager;
  43. _logger = logger;
  44. }
  45. /// <summary>
  46. /// Shoulds the ignore.
  47. /// </summary>
  48. /// <param name="fileInfo">The file information.</param>
  49. /// <param name="parent">The parent.</param>
  50. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  51. public bool ShouldIgnore(FileSystemMetadata fileInfo, BaseItem parent)
  52. {
  53. // Don't ignore top level folders
  54. if (fileInfo.IsDirectory && parent is AggregateFolder)
  55. {
  56. return false;
  57. }
  58. var filename = fileInfo.Name;
  59. var isHidden = fileInfo.IsHidden;
  60. var path = fileInfo.FullName;
  61. // Handle mac .DS_Store
  62. // https://github.com/MediaBrowser/MediaBrowser/issues/427
  63. if (filename.IndexOf("._", StringComparison.OrdinalIgnoreCase) == 0)
  64. {
  65. return true;
  66. }
  67. // Ignore hidden files and folders
  68. if (isHidden)
  69. {
  70. if (parent == null)
  71. {
  72. var parentFolderName = Path.GetFileName(_fileSystem.GetDirectoryName(path));
  73. if (string.Equals(parentFolderName, BaseItem.ThemeSongsFolderName, StringComparison.OrdinalIgnoreCase))
  74. {
  75. return false;
  76. }
  77. if (string.Equals(parentFolderName, BaseItem.ThemeVideosFolderName, StringComparison.OrdinalIgnoreCase))
  78. {
  79. return false;
  80. }
  81. }
  82. // Sometimes these are marked hidden
  83. if (_fileSystem.IsRootPath(path))
  84. {
  85. return false;
  86. }
  87. return true;
  88. }
  89. if (fileInfo.IsDirectory)
  90. {
  91. // Ignore any folders in our list
  92. if (IgnoreFolders.Contains(filename, StringComparer.OrdinalIgnoreCase))
  93. {
  94. return true;
  95. }
  96. if (parent != null)
  97. {
  98. // Ignore trailer folders but allow it at the collection level
  99. if (string.Equals(filename, BaseItem.TrailerFolderName, StringComparison.OrdinalIgnoreCase) &&
  100. !(parent is AggregateFolder) && !(parent is UserRootFolder))
  101. {
  102. return true;
  103. }
  104. if (string.Equals(filename, BaseItem.ThemeVideosFolderName, StringComparison.OrdinalIgnoreCase))
  105. {
  106. return true;
  107. }
  108. if (string.Equals(filename, BaseItem.ThemeSongsFolderName, StringComparison.OrdinalIgnoreCase))
  109. {
  110. return true;
  111. }
  112. }
  113. }
  114. else
  115. {
  116. if (parent != null)
  117. {
  118. // Don't resolve these into audio files
  119. if (string.Equals(_fileSystem.GetFileNameWithoutExtension(filename), BaseItem.ThemeSongFilename) && _libraryManager.IsAudioFile(filename))
  120. {
  121. return true;
  122. }
  123. }
  124. // Ignore samples
  125. var sampleFilename = " " + filename.Replace(".", " ", StringComparison.OrdinalIgnoreCase)
  126. .Replace("-", " ", StringComparison.OrdinalIgnoreCase)
  127. .Replace("_", " ", StringComparison.OrdinalIgnoreCase)
  128. .Replace("!", " ", StringComparison.OrdinalIgnoreCase);
  129. if (sampleFilename.IndexOf(" sample ", StringComparison.OrdinalIgnoreCase) != -1)
  130. {
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. }
  137. }