| | 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 | | { |
| 812 | 16 | | var ignoreFile = new FileInfo(Path.Join(directory.FullName, ".ignore")); |
| 812 | 17 | | if (ignoreFile.Exists) |
| | 18 | | { |
| 0 | 19 | | return ignoreFile; |
| | 20 | | } |
| | 21 | |
|
| 812 | 22 | | var parentDir = directory.Parent; |
| 812 | 23 | | if (parentDir is null) |
| | 24 | | { |
| 119 | 25 | | return null; |
| | 26 | | } |
| | 27 | |
|
| 693 | 28 | | return FindIgnoreFile(parentDir); |
| | 29 | | } |
| | 30 | |
|
| | 31 | | /// <inheritdoc /> |
| | 32 | | public bool ShouldIgnore(FileSystemMetadata fileInfo, BaseItem? parent) |
| | 33 | | { |
| 85 | 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 | | { |
| 119 | 45 | | if (fileInfo.IsDirectory) |
| | 46 | | { |
| 61 | 47 | | var dirIgnoreFile = FindIgnoreFile(new DirectoryInfo(fileInfo.FullName)); |
| 61 | 48 | | if (dirIgnoreFile is null) |
| | 49 | | { |
| 61 | 50 | | return false; |
| | 51 | | } |
| | 52 | |
|
| | 53 | | // Fast path in case the ignore files isn't a symlink and is empty |
| 0 | 54 | | if ((dirIgnoreFile.Attributes & FileAttributes.ReparsePoint) == 0 |
| 0 | 55 | | && dirIgnoreFile.Length == 0) |
| | 56 | | { |
| 0 | 57 | | return true; |
| | 58 | | } |
| | 59 | |
|
| | 60 | | // ignore the directory only if the .ignore file is empty |
| | 61 | | // evaluate individual files otherwise |
| 0 | 62 | | return string.IsNullOrWhiteSpace(GetFileContent(dirIgnoreFile)); |
| | 63 | | } |
| | 64 | |
|
| 58 | 65 | | var parentDirPath = Path.GetDirectoryName(fileInfo.FullName); |
| 58 | 66 | | if (string.IsNullOrEmpty(parentDirPath)) |
| | 67 | | { |
| 0 | 68 | | return false; |
| | 69 | | } |
| | 70 | |
|
| 58 | 71 | | var folder = new DirectoryInfo(parentDirPath); |
| 58 | 72 | | var ignoreFile = FindIgnoreFile(folder); |
| 58 | 73 | | if (ignoreFile is null) |
| | 74 | | { |
| 58 | 75 | | return false; |
| | 76 | | } |
| | 77 | |
|
| 0 | 78 | | string ignoreFileString = GetFileContent(ignoreFile); |
| | 79 | |
|
| 0 | 80 | | if (string.IsNullOrWhiteSpace(ignoreFileString)) |
| | 81 | | { |
| | 82 | | // Ignore directory if we just have the file |
| 0 | 83 | | return true; |
| | 84 | | } |
| | 85 | |
|
| | 86 | | // If file has content, base ignoring off the content .gitignore-style rules |
| 0 | 87 | | var ignoreRules = ignoreFileString.Split('\n', StringSplitOptions.RemoveEmptyEntries); |
| 0 | 88 | | var ignore = new Ignore.Ignore(); |
| 0 | 89 | | ignore.Add(ignoreRules); |
| | 90 | |
|
| 0 | 91 | | return ignore.IsIgnored(fileInfo.FullName); |
| | 92 | | } |
| | 93 | |
|
| | 94 | | private static string GetFileContent(FileInfo dirIgnoreFile) |
| | 95 | | { |
| 0 | 96 | | using (var reader = dirIgnoreFile.OpenText()) |
| | 97 | | { |
| 0 | 98 | | return reader.ReadToEnd(); |
| | 99 | | } |
| 0 | 100 | | } |
| | 101 | | } |