| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | using MediaBrowser.Controller.Entities; |
| | | 4 | | using MediaBrowser.Controller.Resolvers; |
| | | 5 | | using MediaBrowser.Model.IO; |
| | | 6 | | |
| | | 7 | | namespace Emby.Server.Implementations.Library; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Resolver rule class for ignoring files via .ignore. |
| | | 11 | | /// </summary> |
| | | 12 | | public class DotIgnoreIgnoreRule : IResolverIgnoreRule |
| | | 13 | | { |
| | | 14 | | private static FileInfo? FindIgnoreFile(DirectoryInfo directory) |
| | | 15 | | { |
| | 821 | 16 | | var ignoreFile = new FileInfo(Path.Join(directory.FullName, ".ignore")); |
| | 821 | 17 | | if (ignoreFile.Exists) |
| | | 18 | | { |
| | 0 | 19 | | return ignoreFile; |
| | | 20 | | } |
| | | 21 | | |
| | 821 | 22 | | var parentDir = directory.Parent; |
| | 821 | 23 | | if (parentDir is null) |
| | | 24 | | { |
| | 120 | 25 | | return null; |
| | | 26 | | } |
| | | 27 | | |
| | 701 | 28 | | return FindIgnoreFile(parentDir); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public bool ShouldIgnore(FileSystemMetadata fileInfo, BaseItem? parent) |
| | | 33 | | { |
| | 86 | 34 | | 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 | | { |
| | 120 | 45 | | if (fileInfo.IsDirectory) |
| | | 46 | | { |
| | 63 | 47 | | var dirIgnoreFile = FindIgnoreFile(new DirectoryInfo(fileInfo.FullName)); |
| | 63 | 48 | | if (dirIgnoreFile is null) |
| | | 49 | | { |
| | 63 | 50 | | return false; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | // Fast path in case the ignore files isn't a symlink and is empty |
| | 0 | 54 | | if (dirIgnoreFile.LinkTarget is null && dirIgnoreFile.Length == 0) |
| | | 55 | | { |
| | 0 | 56 | | return true; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | // ignore the directory only if the .ignore file is empty |
| | | 60 | | // evaluate individual files otherwise |
| | 0 | 61 | | return string.IsNullOrWhiteSpace(GetFileContent(dirIgnoreFile)); |
| | | 62 | | } |
| | | 63 | | |
| | 57 | 64 | | var parentDirPath = Path.GetDirectoryName(fileInfo.FullName); |
| | 57 | 65 | | if (string.IsNullOrEmpty(parentDirPath)) |
| | | 66 | | { |
| | 0 | 67 | | return false; |
| | | 68 | | } |
| | | 69 | | |
| | 57 | 70 | | var folder = new DirectoryInfo(parentDirPath); |
| | 57 | 71 | | var ignoreFile = FindIgnoreFile(folder); |
| | 57 | 72 | | if (ignoreFile is null) |
| | | 73 | | { |
| | 57 | 74 | | return false; |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | string ignoreFileString = GetFileContent(ignoreFile); |
| | | 78 | | |
| | 0 | 79 | | if (string.IsNullOrWhiteSpace(ignoreFileString)) |
| | | 80 | | { |
| | | 81 | | // Ignore directory if we just have the file |
| | 0 | 82 | | return true; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | // If file has content, base ignoring off the content .gitignore-style rules |
| | 0 | 86 | | var ignoreRules = ignoreFileString.Split('\n', StringSplitOptions.RemoveEmptyEntries); |
| | 0 | 87 | | var ignore = new Ignore.Ignore(); |
| | 0 | 88 | | ignore.Add(ignoreRules); |
| | | 89 | | |
| | 0 | 90 | | return ignore.IsIgnored(fileInfo.FullName); |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | private static string GetFileContent(FileInfo dirIgnoreFile) |
| | | 94 | | { |
| | 0 | 95 | | dirIgnoreFile = (FileInfo?)dirIgnoreFile.ResolveLinkTarget(returnFinalTarget: true) ?? dirIgnoreFile; |
| | 0 | 96 | | if (!dirIgnoreFile.Exists) |
| | | 97 | | { |
| | 0 | 98 | | return string.Empty; |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | using (var reader = dirIgnoreFile.OpenText()) |
| | | 102 | | { |
| | 0 | 103 | | return reader.ReadToEnd(); |
| | | 104 | | } |
| | 0 | 105 | | } |
| | | 106 | | } |