1using System;
2using System.IO;
3using System.Text.RegularExpressions;
4using MediaBrowser.Controller.Entities;
5using MediaBrowser.Controller.IO;
6using MediaBrowser.Controller.Resolvers;
7using MediaBrowser.Model.IO;
8
9namespace Emby.Server.Implementations.Library;
…lines 10–67 unchanged (58 lines)
68 }
69
70 private static bool CheckIgnoreRules(string path, string ignoreFileContent, bool isDirectory)
71 {
72 // If file has content, base ignoring off the content .gitignore-style rules
73 var rules = ignoreFileContent.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
74 return CheckIgnoreRules(path, rules, isDirectory);
75 }
76
77 /// <summary>
78 /// Checks whether a path should be ignored based on an array of ignore rules.
79 /// </summary>
80 /// <param name="path">The path to check.</param>
81 /// <param name="rules">The array of ignore rules.</param>
82 /// <param name="isDirectory">Whether the path is a directory.</param>
83 /// <returns>True if the path should be ignored.</returns>
84 internal static bool CheckIgnoreRules(string path, string[] rules, bool isDirectory)
85 => CheckIgnoreRules(path, rules, isDirectory, IsWindows);
86
87 /// <summary>
88 /// Checks whether a path should be ignored based on an array of ignore rules.
89 /// </summary>
90 /// <param name="path">The path to check.</param>
91 /// <param name="rules">The array of ignore rules.</param>
92 /// <param name="isDirectory">Whether the path is a directory.</param>
93 /// <param name="normalizePath">Whether to normalize backslashes to forward slashes (for Windows paths).</param>
94 /// <returns>True if the path should be ignored.</returns>
95 internal static bool CheckIgnoreRules(string path, string[] rules, bool isDirectory, bool normalizePath)
96 {
97 var ignore = new Ignore.Ignore();
98
99 // Add each rule individually to catch and skip invalid patterns
100 var validRulesAdded = 0;
101 foreach (var rule in rules)
102 {
103 try
104 {
105 ignore.Add(rule);
106 validRulesAdded++;
107 }
108 catch (RegexParseException)
109 {
110 // Ignore invalid patterns
111 }
112 }
113
114 // If no valid rules were added, fall back to ignoring everything (like an empty .ignore file)
115 if (validRulesAdded == 0)
116 {
117 return true;
118 }
119
120 // Mitigate the problem of the Ignore library not handling Windows paths correctly.
121 // See https://github.com/jellyfin/jellyfin/issues/15484
122 var pathToCheck = normalizePath ? path.NormalizePath('/') : path;
123
124 // Add trailing slash for directories to match "folder/"
125 if (isDirectory)
126 {
127 pathToCheck = string.Concat(pathToCheck.AsSpan().TrimEnd('/'), "/");
128 }
129
130 return ignore.IsIgnored(pathToCheck);
131 }
132
133 private static string GetFileContent(FileInfo ignoreFile)
134 {
135 ignoreFile = FileSystemHelper.ResolveLinkTarget(ignoreFile, returnFinalTarget: true) ?? ignoreFile;
136 return ignoreFile.Exists
137 ? File.ReadAllText(ignoreFile.FullName)
…lines 138–141 unchanged (4 lines)