| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | using MediaBrowser.Controller.Entities; |
| | | 4 | | using MediaBrowser.Controller.IO; |
| | | 5 | | using MediaBrowser.Controller.Resolvers; |
| | | 6 | | using MediaBrowser.Model.IO; |
| | | 7 | | |
| | | 8 | | namespace Emby.Server.Implementations.Library; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Resolver rule class for ignoring files via .ignore. |
| | | 12 | | /// </summary> |
| | | 13 | | public class DotIgnoreIgnoreRule : IResolverIgnoreRule |
| | | 14 | | { |
| | 0 | 15 | | private static readonly bool IsWindows = OperatingSystem.IsWindows(); |
| | | 16 | | |
| | | 17 | | private static FileInfo? FindIgnoreFile(DirectoryInfo directory) |
| | | 18 | | { |
| | 1802 | 19 | | for (var current = directory; current is not null; current = current.Parent) |
| | | 20 | | { |
| | 785 | 21 | | var ignorePath = Path.Join(current.FullName, ".ignore"); |
| | 785 | 22 | | if (File.Exists(ignorePath)) |
| | | 23 | | { |
| | 0 | 24 | | return new FileInfo(ignorePath); |
| | | 25 | | } |
| | | 26 | | } |
| | | 27 | | |
| | 116 | 28 | | return null; |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | 82 | 32 | | public bool ShouldIgnore(FileSystemMetadata fileInfo, BaseItem? parent) => IsIgnored(fileInfo, parent); |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Checks whether or not the file is ignored. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="fileInfo">The file information.</param> |
| | | 38 | | /// <param name="parent">The parent BaseItem.</param> |
| | | 39 | | /// <returns>True if the file should be ignored.</returns> |
| | | 40 | | public static bool IsIgnored(FileSystemMetadata fileInfo, BaseItem? parent) |
| | | 41 | | { |
| | 116 | 42 | | var searchDirectory = fileInfo.IsDirectory |
| | 116 | 43 | | ? new DirectoryInfo(fileInfo.FullName) |
| | 116 | 44 | | : new DirectoryInfo(Path.GetDirectoryName(fileInfo.FullName) ?? string.Empty); |
| | | 45 | | |
| | 116 | 46 | | if (string.IsNullOrEmpty(searchDirectory.FullName)) |
| | | 47 | | { |
| | 0 | 48 | | return false; |
| | | 49 | | } |
| | | 50 | | |
| | 116 | 51 | | var ignoreFile = FindIgnoreFile(searchDirectory); |
| | 116 | 52 | | if (ignoreFile is null) |
| | | 53 | | { |
| | 116 | 54 | | return false; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | // Fast path in case the ignore files isn't a symlink and is empty |
| | 0 | 58 | | if (ignoreFile.LinkTarget is null && ignoreFile.Length == 0) |
| | | 59 | | { |
| | | 60 | | // Ignore directory if we just have the file |
| | 0 | 61 | | return true; |
| | | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | var content = GetFileContent(ignoreFile); |
| | 0 | 65 | | return string.IsNullOrWhiteSpace(content) |
| | 0 | 66 | | || CheckIgnoreRules(fileInfo.FullName, content, fileInfo.IsDirectory); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | private static bool CheckIgnoreRules(string path, string ignoreFileContent, bool isDirectory) |
| | | 70 | | { |
| | | 71 | | // If file has content, base ignoring off the content .gitignore-style rules |
| | 0 | 72 | | var rules = ignoreFileContent.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries |
| | 0 | 73 | | var ignore = new Ignore.Ignore(); |
| | 0 | 74 | | ignore.Add(rules); |
| | | 75 | | |
| | | 76 | | // Mitigate the problem of the Ignore library not handling Windows paths correctly. |
| | | 77 | | // See https://github.com/jellyfin/jellyfin/issues/15484 |
| | 0 | 78 | | var pathToCheck = IsWindows ? path.NormalizePath('/') : path; |
| | | 79 | | |
| | | 80 | | // Add trailing slash for directories to match "folder/" |
| | 0 | 81 | | if (isDirectory) |
| | | 82 | | { |
| | 0 | 83 | | pathToCheck = string.Concat(pathToCheck.AsSpan().TrimEnd('/'), "/"); |
| | | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | return ignore.IsIgnored(pathToCheck); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | private static string GetFileContent(FileInfo ignoreFile) |
| | | 90 | | { |
| | 0 | 91 | | ignoreFile = FileSystemHelper.ResolveLinkTarget(ignoreFile, returnFinalTarget: true) ?? ignoreFile; |
| | 0 | 92 | | return ignoreFile.Exists |
| | 0 | 93 | | ? File.ReadAllText(ignoreFile.FullName) |
| | 0 | 94 | | : string.Empty; |
| | | 95 | | } |
| | | 96 | | } |