The Free Software Media System - Server Backend & API
Use glob patterns to ignore files
Erik Rigtorpf144acdc9614
diff --git a/Emby.Server.Implementations/Emby.Server.Implementations.csproj b/Emby.Server.Implementations/Emby.Server.Implementations.csprojindex 44fc932e39..bab9e1c177 100644--- a/Emby.Server.Implementations/Emby.Server.Implementations.csproj+++ b/Emby.Server.Implementations/Emby.Server.Implementations.csproj@@ -43,6 +43,7 @@<PackageReference Include="ServiceStack.Text.Core" Version="5.8.0" /><PackageReference Include="sharpcompress" Version="0.25.0" /><PackageReference Include="SQLitePCL.pretty.netstandard" Version="2.1.0" />+ <PackageReference Include="DotNet.Glob" Version="3.0.9" /></ItemGroup><ItemGroup>diff --git a/Emby.Server.Implementations/IO/LibraryMonitor.cs b/Emby.Server.Implementations/IO/LibraryMonitor.csindex 5a1eb43bcb..eb5e190aab 100644--- a/Emby.Server.Implementations/IO/LibraryMonitor.cs+++ b/Emby.Server.Implementations/IO/LibraryMonitor.cs@@ -11,6 +11,7 @@ using MediaBrowser.Controller.Entities;using MediaBrowser.Controller.Library;using MediaBrowser.Controller.Plugins;using MediaBrowser.Model.IO;+using Emby.Server.Implementations.Library;using Microsoft.Extensions.Logging;namespace Emby.Server.Implementations.IO@@ -37,38 +38,6 @@ namespace Emby.Server.Implementations.IO/// </summary>private readonly ConcurrentDictionary<string, string> _tempIgnoredPaths = new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase);- /// <summary>- /// Any file name ending in any of these will be ignored by the watchers.- /// </summary>- private static readonly HashSet<string> _alwaysIgnoreFiles = new HashSet<string>(StringComparer.OrdinalIgnoreCase)- {- "small.jpg",- "albumart.jpg",-- // WMC temp recording directories that will constantly be written to- "TempRec",- "TempSBE"- };-- private static readonly string[] _alwaysIgnoreSubstrings = new string[]- {- // Synology- "eaDir",- "#recycle",- ".wd_tv",- ".actors"- };-- private static readonly HashSet<string> _alwaysIgnoreExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)- {- // thumbs.db- ".db",-- // bts sync files- ".bts",- ".sync"- };-/// <summary>/// Add the path to our temporary ignore list. Use when writing to a path within our listening scope./// </summary>@@ -395,12 +364,7 @@ namespace Emby.Server.Implementations.IOthrow new ArgumentNullException(nameof(path));}- var filename = Path.GetFileName(path);-- var monitorPath = !string.IsNullOrEmpty(filename) &&- !_alwaysIgnoreFiles.Contains(filename) &&- !_alwaysIgnoreExtensions.Contains(Path.GetExtension(path)) &&- _alwaysIgnoreSubstrings.All(i => path.IndexOf(i, StringComparison.OrdinalIgnoreCase) == -1);+ var monitorPath = !IgnorePatterns.ShouldIgnore(path);// Ignore certain filesvar tempIgnorePaths = _tempIgnoredPaths.Keys.ToList();diff --git a/Emby.Server.Implementations/Library/CoreResolutionIgnoreRule.cs b/Emby.Server.Implementations/Library/CoreResolutionIgnoreRule.csindex bc1398332d..218e5a0c6d 100644--- a/Emby.Server.Implementations/Library/CoreResolutionIgnoreRule.cs+++ b/Emby.Server.Implementations/Library/CoreResolutionIgnoreRule.cs@@ -1,7 +1,5 @@using System;using System.IO;-using System.Linq;-using System.Text.RegularExpressions;using MediaBrowser.Controller.Entities;using MediaBrowser.Controller.Library;using MediaBrowser.Controller.Resolvers;@@ -16,32 +14,6 @@ namespace Emby.Server.Implementations.Library{private readonly ILibraryManager _libraryManager;- /// <summary>- /// Any folder named in this list will be ignored- /// </summary>- private static readonly string[] _ignoreFolders =- {- "metadata",- "ps3_update",- "ps3_vprm",- "extrafanart",- "extrathumbs",- ".actors",- ".wd_tv",-- // Synology- "@eaDir",- "eaDir",- "#recycle",-- // Qnap- "@Recycle",- ".@__thumb",- "$RECYCLE.BIN",- "System Volume Information",- ".grab",- };-/// <summary>/// Initializes a new instance of the <see cref="CoreResolutionIgnoreRule"/> class./// </summary>@@ -60,23 +32,15 @@ namespace Emby.Server.Implementations.Libraryreturn false;}- var filename = fileInfo.Name;-- // Ignore hidden files on UNIX- if (Environment.OSVersion.Platform != PlatformID.Win32NT- && filename[0] == '.')+ if (IgnorePatterns.ShouldIgnore(fileInfo.FullName)){return true;}+ var filename = fileInfo.Name;+if (fileInfo.IsDirectory){- // Ignore any folders in our list- if (_ignoreFolders.Contains(filename, StringComparer.OrdinalIgnoreCase))- {- return true;- }-if (parent != null){// Ignore trailer folders but allow it at the collection level@@ -109,11 +73,6 @@ namespace Emby.Server.Implementations.Libraryreturn true;}}-- // Ignore samples- Match m = Regex.Match(filename, @"\bsample\b", RegexOptions.IgnoreCase);-- return m.Success;}return false;diff --git a/Emby.Server.Implementations/Library/IgnorePatterns.cs b/Emby.Server.Implementations/Library/IgnorePatterns.csnew file mode 100644index 0000000000..49a36495af--- /dev/null+++ b/Emby.Server.Implementations/Library/IgnorePatterns.cs@@ -0,0 +1,73 @@+using System.Linq;+using DotNet.Globbing;++namespace Emby.Server.Implementations.Library+{+ /// <summary>+ /// Glob patterns for files to ignore+ /// </summary>+ public static class IgnorePatterns+ {+ /// <summary>+ /// Files matching these glob patterns will be ignored+ /// </summary>+ public static readonly string[] Patterns = new string[]+ {+ "**/small.jpg",+ "**/albumart.jpg",+ "**/*sample*",++ // Directories+ "**/metadata/**",+ "**/ps3_update/**",+ "**/ps3_vprm/**",+ "**/extrafanart/**",+ "**/extrathumbs/**",+ "**/.actors/**",+ "**/.wd_tv/**",++ // WMC temp recording directories that will constantly be written to+ "**/TempRec/**",+ "**/TempSBE/**",++ // Synology+ "**/eaDir/**",+ "**/@eaDir/**",+ "**/#recycle/**",++ // Qnap+ "**/@Recycle/**",+ "**/.@__thumb/**",+ "**/$RECYCLE.BIN/**",+ "**/System Volume Information/**",+ "**/.grab/**",++ // Unix hidden files and directories+ "**/.*/**",++ // thumbs.db+ "**/thumbs.db",++ // bts sync files+ "**/*.bts",+ "**/*.sync",+ };++ private static readonly GlobOptions _globOptions = new GlobOptions+ {+ Evaluation = {+ CaseInsensitive = true+ }+ };++ private static readonly Glob[] _globs = Patterns.Select(p => Glob.Parse(p, _globOptions)).ToArray();++ /// <summary>+ /// Returns true if the supplied path should be ignored+ /// </summary>+ public static bool ShouldIgnore(string path)+ {+ return _globs.Any(g => g.IsMatch(path));+ }+ }+}diff --git a/tests/Jellyfin.Server.Implementations.Tests/Library/IgnorePatternsTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Library/IgnorePatternsTests.csnew file mode 100644index 0000000000..26dee38c6d--- /dev/null+++ b/tests/Jellyfin.Server.Implementations.Tests/Library/IgnorePatternsTests.cs@@ -0,0 +1,21 @@+using Emby.Server.Implementations.Library;+using Xunit;++namespace Jellyfin.Server.Implementations.Tests.Library+{+ public class IgnorePatternsTests+ {+ [Theory]+ [InlineData("/media/small.jpg", true)]+ [InlineData("/media/movies/#Recycle/test.txt", true)]+ [InlineData("/media/movies/#recycle/", true)]+ [InlineData("thumbs.db", true)]+ [InlineData(@"C:\media\movies\movie.avi", false)]+ [InlineData("/media/.hiddendir/file.mp4", true)]+ [InlineData("/media/dir/.hiddenfile.mp4", true)]+ public void PathIgnored(string path, bool expected)+ {+ Assert.Equal(expected, IgnorePatterns.ShouldIgnore(path));+ }+ }+}