|
1 | | -using NPOI.HSSF.UserModel; // For .xls |
| 1 | +using NPOI.HPSF; |
| 2 | +using NPOI.HSSF.UserModel; // For .xls |
2 | 3 | using NPOI.SS.UserModel; |
3 | 4 | using NPOI.XSSF.UserModel; // For .xlsx |
4 | 5 |
|
5 | 6 | namespace GitContentSearch |
6 | 7 | { |
7 | 8 | public class FileSearcher : IFileSearcher |
8 | | - { |
9 | | - public bool SearchInFile(string fileName, string searchString) |
10 | | - { |
11 | | - if (IsTextFile(fileName)) |
12 | | - { |
13 | | - return SearchInTextFile(fileName, searchString); |
14 | | - } |
15 | | - else |
16 | | - { |
17 | | - return SearchInExcel(fileName, searchString); |
18 | | - } |
19 | | - } |
| 9 | + { |
| 10 | + public bool SearchInFile(string fileName, string searchString) |
| 11 | + { |
| 12 | + if (IsTextFile(fileName)) |
| 13 | + { |
| 14 | + return SearchInTextFile(fileName, searchString); |
| 15 | + } |
| 16 | + else |
| 17 | + { |
| 18 | + return SearchInExcel(fileName, searchString); |
| 19 | + } |
| 20 | + } |
20 | 21 |
|
21 | | - public bool SearchInStream(Stream stream, string searchString, bool isBinary) |
22 | | - { |
23 | | - // Save the current position to restore it later |
24 | | - long originalPosition = stream.Position; |
25 | | - |
26 | | - try |
27 | | - { |
28 | | - if (isBinary) |
29 | | - { |
30 | | - stream.Position = 0; |
31 | | - // For Excel files |
32 | | - IWorkbook workbook; |
33 | | - try |
34 | | - { |
35 | | - workbook = new XSSFWorkbook(stream); // Try XLSX format first |
36 | | - } |
37 | | - catch |
38 | | - { |
39 | | - stream.Position = 0; |
40 | | - workbook = new HSSFWorkbook(stream); // Fall back to XLS format |
41 | | - } |
| 22 | + public bool SearchInStream(Stream stream, string searchString, string extension) |
| 23 | + { |
| 24 | + if (IsTextFile(stream, extension)) |
| 25 | + { |
| 26 | + return SearchInTextStream(stream, searchString); |
| 27 | + } |
| 28 | + else |
| 29 | + { |
| 30 | + return SearchInExcel(stream, searchString, extension); |
| 31 | + } |
| 32 | + } |
42 | 33 |
|
43 | | - using (workbook) |
44 | | - { |
45 | | - for (int i = 0; i < workbook.NumberOfSheets; i++) |
46 | | - { |
47 | | - var sheet = workbook.GetSheetAt(i); |
48 | | - if (sheet == null) continue; |
49 | | - |
50 | | - foreach (IRow row in sheet) |
51 | | - { |
52 | | - if (row == null) continue; |
53 | | - |
54 | | - foreach (ICell cell in row) |
55 | | - { |
56 | | - if (cell == null) continue; |
57 | | - |
58 | | - var cellValue = GetCellValueAsString(cell); |
59 | | - if (!string.IsNullOrEmpty(cellValue) && |
60 | | - cellValue.Contains(searchString, StringComparison.OrdinalIgnoreCase)) |
61 | | - { |
62 | | - return true; |
63 | | - } |
64 | | - } |
65 | | - } |
66 | | - } |
67 | | - } |
68 | | - return false; |
69 | | - } |
70 | | - else |
71 | | - { |
72 | | - // For text files |
73 | | - stream.Position = 0; |
74 | | - using var reader = new StreamReader(stream, leaveOpen: true); |
75 | | - string? line; |
76 | | - while ((line = reader.ReadLine()) != null) |
77 | | - { |
78 | | - if (line.Contains(searchString, StringComparison.OrdinalIgnoreCase)) |
79 | | - { |
80 | | - return true; |
81 | | - } |
82 | | - } |
83 | | - return false; |
84 | | - } |
85 | | - } |
86 | | - finally |
87 | | - { |
88 | | - // Restore the original position |
89 | | - try { stream.Position = originalPosition; } catch { } |
90 | | - } |
91 | | - } |
| 34 | + public bool SearchInTextStream(Stream stream, string searchString) |
| 35 | + { |
| 36 | + try |
| 37 | + { |
| 38 | + stream.Position = 0; |
| 39 | + using var reader = new StreamReader(stream); |
| 40 | + string line; |
| 41 | + while ((line = reader.ReadLine()) != null) |
| 42 | + { |
| 43 | + if (line.Contains(searchString, StringComparison.OrdinalIgnoreCase)) |
| 44 | + { |
| 45 | + return true; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + catch (Exception ex) |
| 50 | + { |
| 51 | + return false; |
| 52 | + } |
| 53 | + return false; |
| 54 | + } |
92 | 55 |
|
93 | | - public bool IsTextFile(string filePath) |
| 56 | + public bool IsTextFile(Stream stream, string extension) |
| 57 | + { |
| 58 | + if (IsTextStream(stream)) |
| 59 | + { |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | + return extension switch |
| 64 | + { |
| 65 | + ".xls" or ".xlsx" => false, |
| 66 | + ".txt" or ".cs" or ".json" or ".xml" or ".config" or ".md" or ".yml" or ".yaml" => true, |
| 67 | + _ => true // Default to text for unknown extensions |
| 68 | + }; |
| 69 | + } |
| 70 | + |
| 71 | + public bool IsTextFile(string filePath) |
94 | 72 | { |
95 | 73 | // For file paths that exist on disk |
96 | 74 | if (File.Exists(filePath)) |
@@ -155,30 +133,129 @@ public string GetCellValueAsString(ICell cell) |
155 | 133 | }; |
156 | 134 | } |
157 | 135 |
|
158 | | - public bool SearchInTextFile(string fileName, string searchString) |
| 136 | + public bool SearchInExcel(Stream stream, string searchString, string extension) |
159 | 137 | { |
160 | | - try |
161 | | - { |
162 | | - using var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read); |
163 | | - return SearchInStream(stream, searchString, false); |
164 | | - } |
165 | | - catch (Exception) |
166 | | - { |
167 | | - return false; |
168 | | - } |
169 | | - } |
| 138 | + try |
| 139 | + { |
| 140 | + stream.Position = 0; |
| 141 | + IWorkbook workbook; |
170 | 142 |
|
171 | | - public bool SearchInExcel(string fileName, string searchString) |
172 | | - { |
173 | | - try |
174 | | - { |
175 | | - using var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read); |
176 | | - return SearchInStream(stream, searchString, true); |
177 | | - } |
178 | | - catch (Exception) |
179 | | - { |
180 | | - return false; |
181 | | - } |
182 | | - } |
183 | | - } |
| 143 | + if (extension == ".xls") |
| 144 | + { |
| 145 | + workbook = new HSSFWorkbook(stream); |
| 146 | + } |
| 147 | + else if (extension == ".xlsx") |
| 148 | + { |
| 149 | + workbook = new XSSFWorkbook(stream); |
| 150 | + } |
| 151 | + else |
| 152 | + { |
| 153 | + Console.WriteLine($"Unsupported file extension: {extension}"); |
| 154 | + return false; |
| 155 | + } |
| 156 | + |
| 157 | + for (int i = 0; i < workbook.NumberOfSheets; i++) |
| 158 | + { |
| 159 | + var sheet = workbook.GetSheetAt(i); |
| 160 | + if (sheet == null) continue; |
| 161 | + |
| 162 | + foreach (IRow row in sheet) |
| 163 | + { |
| 164 | + if (row == null) continue; |
| 165 | + |
| 166 | + foreach (ICell cell in row.Cells) |
| 167 | + { |
| 168 | + if (cell == null) continue; |
| 169 | + |
| 170 | + string cellValue = GetCellValueAsString(cell); |
| 171 | + if (cellValue != null && cellValue.Contains(searchString, StringComparison.OrdinalIgnoreCase)) |
| 172 | + { |
| 173 | + return true; |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + catch |
| 180 | + { |
| 181 | + return false; |
| 182 | + } |
| 183 | + |
| 184 | + return false; |
| 185 | + } |
| 186 | + |
| 187 | + public bool SearchInTextFile(string fileName, string searchString) |
| 188 | + { |
| 189 | + try |
| 190 | + { |
| 191 | + foreach (var line in File.ReadLines(fileName)) |
| 192 | + { |
| 193 | + if (line.Contains(searchString, StringComparison.OrdinalIgnoreCase)) |
| 194 | + { |
| 195 | + return true; |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + catch |
| 200 | + { |
| 201 | + return false; |
| 202 | + } |
| 203 | + |
| 204 | + return false; |
| 205 | + } |
| 206 | + |
| 207 | + public bool SearchInExcel(string fileName, string searchString) |
| 208 | + { |
| 209 | + try |
| 210 | + { |
| 211 | + using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) |
| 212 | + { |
| 213 | + IWorkbook workbook; |
| 214 | + string extension = Path.GetExtension(fileName).ToLower(); |
| 215 | + |
| 216 | + if (extension == ".xls") |
| 217 | + { |
| 218 | + workbook = new HSSFWorkbook(fileStream); |
| 219 | + } |
| 220 | + else if (extension == ".xlsx") |
| 221 | + { |
| 222 | + workbook = new XSSFWorkbook(fileStream); |
| 223 | + } |
| 224 | + else |
| 225 | + { |
| 226 | + Console.WriteLine($"Unsupported file extension: {extension}"); |
| 227 | + return false; |
| 228 | + } |
| 229 | + |
| 230 | + for (int i = 0; i < workbook.NumberOfSheets; i++) |
| 231 | + { |
| 232 | + var sheet = workbook.GetSheetAt(i); |
| 233 | + if (sheet == null) continue; |
| 234 | + |
| 235 | + foreach (IRow row in sheet) |
| 236 | + { |
| 237 | + if (row == null) continue; |
| 238 | + |
| 239 | + foreach (ICell cell in row.Cells) |
| 240 | + { |
| 241 | + if (cell == null) continue; |
| 242 | + |
| 243 | + string cellValue = GetCellValueAsString(cell); |
| 244 | + if (cellValue != null && cellValue.Contains(searchString, StringComparison.OrdinalIgnoreCase)) |
| 245 | + { |
| 246 | + return true; |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + catch |
| 254 | + { |
| 255 | + return false; |
| 256 | + } |
| 257 | + |
| 258 | + return false; |
| 259 | + } |
| 260 | + } |
184 | 261 | } |
0 commit comments