CoreResolutionIgnoreRule.cs 5.7 KB

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