< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.Library.DotIgnoreIgnoreRule
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/Library/DotIgnoreIgnoreRule.cs
Line coverage
56%
Covered lines: 17
Uncovered lines: 13
Coverable lines: 30
Total lines: 94
Line coverage: 56.6%
Branch coverage
57%
Covered branches: 8
Total branches: 14
Branch coverage: 57.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FindIgnoreFile(...)75%4485.71%
ShouldIgnore(...)100%11100%
IsIgnored(...)50%211052.63%
GetFileContent(...)100%210%

File(s)

/srv/git/jellyfin/Emby.Server.Implementations/Library/DotIgnoreIgnoreRule.cs

#LineLine coverage
 1using System;
 2using System.IO;
 3using MediaBrowser.Controller.Entities;
 4using MediaBrowser.Controller.Resolvers;
 5using MediaBrowser.Model.IO;
 6
 7namespace Emby.Server.Implementations.Library;
 8
 9/// <summary>
 10/// Resolver rule class for ignoring files via .ignore.
 11/// </summary>
 12public class DotIgnoreIgnoreRule : IResolverIgnoreRule
 13{
 14    private static FileInfo? FindIgnoreFile(DirectoryInfo directory)
 15    {
 87516        var ignoreFile = new FileInfo(Path.Join(directory.FullName, ".ignore"));
 87517        if (ignoreFile.Exists)
 18        {
 019            return ignoreFile;
 20        }
 21
 87522        var parentDir = directory.Parent;
 87523        if (parentDir is null)
 24        {
 12625            return null;
 26        }
 27
 74928        return FindIgnoreFile(parentDir);
 29    }
 30
 31    /// <inheritdoc />
 32    public bool ShouldIgnore(FileSystemMetadata fileInfo, BaseItem? parent)
 33    {
 9234        return IsIgnored(fileInfo, parent);
 35    }
 36
 37    /// <summary>
 38    /// Checks whether or not the file is ignored.
 39    /// </summary>
 40    /// <param name="fileInfo">The file information.</param>
 41    /// <param name="parent">The parent BaseItem.</param>
 42    /// <returns>True if the file should be ignored.</returns>
 43    public static bool IsIgnored(FileSystemMetadata fileInfo, BaseItem? parent)
 44    {
 12645        if (fileInfo.IsDirectory)
 46        {
 6547            var dirIgnoreFile = FindIgnoreFile(new DirectoryInfo(fileInfo.FullName));
 6548            if (dirIgnoreFile is null)
 49            {
 6550                return false;
 51            }
 52
 53            // ignore the directory only if the .ignore file is empty
 54            // evaluate individual files otherwise
 055            return string.IsNullOrWhiteSpace(GetFileContent(dirIgnoreFile));
 56        }
 57
 6158        var parentDirPath = Path.GetDirectoryName(fileInfo.FullName);
 6159        if (string.IsNullOrEmpty(parentDirPath))
 60        {
 061            return false;
 62        }
 63
 6164        var folder = new DirectoryInfo(parentDirPath);
 6165        var ignoreFile = FindIgnoreFile(folder);
 6166        if (ignoreFile is null)
 67        {
 6168            return false;
 69        }
 70
 071        string ignoreFileString = GetFileContent(ignoreFile);
 72
 073        if (string.IsNullOrWhiteSpace(ignoreFileString))
 74        {
 75            // Ignore directory if we just have the file
 076            return true;
 77        }
 78
 79        // If file has content, base ignoring off the content .gitignore-style rules
 080        var ignoreRules = ignoreFileString.Split('\n', StringSplitOptions.RemoveEmptyEntries);
 081        var ignore = new Ignore.Ignore();
 082        ignore.Add(ignoreRules);
 83
 084        return ignore.IsIgnored(fileInfo.FullName);
 85    }
 86
 87    private static string GetFileContent(FileInfo dirIgnoreFile)
 88    {
 089        using (var reader = dirIgnoreFile.OpenText())
 90        {
 091            return reader.ReadToEnd();
 92        }
 093    }
 94}