CoreResolutionIgnoreRule.cs 5.7 KB

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