| | 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 | | { |
| 690 | 16 | | var ignoreFile = new FileInfo(Path.Join(directory.FullName, ".ignore")); |
| 690 | 17 | | if (ignoreFile.Exists) |
| | 18 | | { |
| 0 | 19 | | return ignoreFile; |
| | 20 | | } |
| | 21 | |
|
| 690 | 22 | | var parentDir = directory.Parent; |
| 690 | 23 | | if (parentDir is null) |
| | 24 | | { |
| 112 | 25 | | return null; |
| | 26 | | } |
| | 27 | |
|
| 578 | 28 | | return FindIgnoreFile(parentDir); |
| | 29 | | } |
| | 30 | |
|
| | 31 | | /// <inheritdoc /> |
| | 32 | | public bool ShouldIgnore(FileSystemMetadata fileInfo, BaseItem? parent) |
| | 33 | | { |
| 78 | 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 | | { |
| 112 | 45 | | var parentDirPath = Path.GetDirectoryName(fileInfo.FullName); |
| 112 | 46 | | if (string.IsNullOrEmpty(parentDirPath)) |
| | 47 | | { |
| 0 | 48 | | return false; |
| | 49 | | } |
| | 50 | |
|
| 112 | 51 | | var folder = new DirectoryInfo(parentDirPath); |
| 112 | 52 | | var ignoreFile = FindIgnoreFile(folder); |
| 112 | 53 | | if (ignoreFile is null) |
| | 54 | | { |
| 112 | 55 | | return false; |
| | 56 | | } |
| | 57 | |
|
| | 58 | | string ignoreFileString; |
| 0 | 59 | | using (var reader = ignoreFile.OpenText()) |
| | 60 | | { |
| 0 | 61 | | ignoreFileString = reader.ReadToEnd(); |
| 0 | 62 | | } |
| | 63 | |
|
| 0 | 64 | | if (string.IsNullOrEmpty(ignoreFileString)) |
| | 65 | | { |
| | 66 | | // Ignore directory if we just have the file |
| 0 | 67 | | return true; |
| | 68 | | } |
| | 69 | |
|
| | 70 | | // If file has content, base ignoring off the content .gitignore-style rules |
| 0 | 71 | | var ignoreRules = ignoreFileString.Split('\n', StringSplitOptions.RemoveEmptyEntries); |
| 0 | 72 | | var ignore = new Ignore.Ignore(); |
| 0 | 73 | | ignore.Add(ignoreRules); |
| | 74 | |
|
| 0 | 75 | | return ignore.IsIgnored(fileInfo.FullName); |
| | 76 | | } |
| | 77 | | } |