Use glob patterns to ignore files

This commit is contained in:
Erik Rigtorp 2020-05-12 14:33:06 -07:00
parent f33876e7e3
commit f144acdc96
5 changed files with 100 additions and 82 deletions

View file

@ -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>

View file

@ -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.IO
throw 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 files
var tempIgnorePaths = _tempIgnoredPaths.Keys.ToList();

View file

@ -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.Library
return 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.Library
return true;
}
}
// Ignore samples
Match m = Regex.Match(filename, @"\bsample\b", RegexOptions.IgnoreCase);
return m.Success;
}
return false;

View file

@ -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));
}
}
}

View file

@ -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));
}
}
}