CoreResolutionIgnoreRule.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.Resolvers;
  8. using MediaBrowser.Model.Extensions;
  9. using MediaBrowser.Model.IO;
  10. using Microsoft.Extensions.Logging;
  11. namespace Emby.Server.Implementations.Library
  12. {
  13. /// <summary>
  14. /// Provides the core resolver ignore rules
  15. /// </summary>
  16. public class CoreResolutionIgnoreRule : IResolverIgnoreRule
  17. {
  18. private readonly IFileSystem _fileSystem;
  19. private readonly ILibraryManager _libraryManager;
  20. private readonly ILogger _logger;
  21. private bool _ignoreDotPrefix;
  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 Dictionary<string, 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. // Qnap
  39. "@Recycle",
  40. ".@__thumb",
  41. "$RECYCLE.BIN",
  42. "System Volume Information",
  43. ".grab",
  44. // macos
  45. ".AppleDouble"
  46. }.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  47. public CoreResolutionIgnoreRule(IFileSystem fileSystem, ILibraryManager libraryManager, ILogger logger)
  48. {
  49. _fileSystem = fileSystem;
  50. _libraryManager = libraryManager;
  51. _logger = logger;
  52. _ignoreDotPrefix = Environment.OSVersion.Platform != PlatformID.Win32NT;
  53. }
  54. /// <summary>
  55. /// Shoulds the ignore.
  56. /// </summary>
  57. /// <param name="fileInfo">The file information.</param>
  58. /// <param name="parent">The parent.</param>
  59. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  60. public bool ShouldIgnore(FileSystemMetadata fileInfo, BaseItem parent)
  61. {
  62. // Don't ignore top level folders
  63. if (fileInfo.IsDirectory && parent is AggregateFolder)
  64. {
  65. return false;
  66. }
  67. var filename = fileInfo.Name;
  68. var path = fileInfo.FullName;
  69. // Handle mac .DS_Store
  70. // https://github.com/MediaBrowser/MediaBrowser/issues/427
  71. if (_ignoreDotPrefix)
  72. {
  73. if (filename.IndexOf('.') == 0)
  74. {
  75. return true;
  76. }
  77. }
  78. // Ignore hidden files and folders
  79. //if (fileInfo.IsHidden)
  80. //{
  81. // if (parent == null)
  82. // {
  83. // var parentFolderName = Path.GetFileName(_fileSystem.GetDirectoryName(path));
  84. // if (string.Equals(parentFolderName, BaseItem.ThemeSongsFolderName, StringComparison.OrdinalIgnoreCase))
  85. // {
  86. // return false;
  87. // }
  88. // if (string.Equals(parentFolderName, BaseItem.ThemeVideosFolderName, StringComparison.OrdinalIgnoreCase))
  89. // {
  90. // return false;
  91. // }
  92. // }
  93. // // Sometimes these are marked hidden
  94. // if (_fileSystem.IsRootPath(path))
  95. // {
  96. // return false;
  97. // }
  98. // return true;
  99. //}
  100. if (fileInfo.IsDirectory)
  101. {
  102. // Ignore any folders in our list
  103. if (IgnoreFolders.ContainsKey(filename))
  104. {
  105. return true;
  106. }
  107. if (parent != null)
  108. {
  109. // Ignore trailer folders but allow it at the collection level
  110. if (string.Equals(filename, BaseItem.TrailerFolderName, StringComparison.OrdinalIgnoreCase) &&
  111. !(parent is AggregateFolder) && !(parent is UserRootFolder))
  112. {
  113. return true;
  114. }
  115. if (string.Equals(filename, BaseItem.ThemeVideosFolderName, StringComparison.OrdinalIgnoreCase))
  116. {
  117. return true;
  118. }
  119. if (string.Equals(filename, BaseItem.ThemeSongsFolderName, StringComparison.OrdinalIgnoreCase))
  120. {
  121. return true;
  122. }
  123. }
  124. }
  125. else
  126. {
  127. if (parent != null)
  128. {
  129. // Don't resolve these into audio files
  130. if (string.Equals(Path.GetFileNameWithoutExtension(filename), BaseItem.ThemeSongFilename) && _libraryManager.IsAudioFile(filename))
  131. {
  132. return true;
  133. }
  134. }
  135. // Ignore samples
  136. var sampleFilename = " " + filename.Replace(".", " ", StringComparison.OrdinalIgnoreCase)
  137. .Replace("-", " ", StringComparison.OrdinalIgnoreCase)
  138. .Replace("_", " ", StringComparison.OrdinalIgnoreCase)
  139. .Replace("!", " ", StringComparison.OrdinalIgnoreCase);
  140. if (sampleFilename.IndexOf(" sample ", StringComparison.OrdinalIgnoreCase) != -1)
  141. {
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. }
  148. }