From e01ed8ad3c92b8ff9b38d75309cb1638ad95b821 Mon Sep 17 00:00:00 2001 From: Divyansh Srivastava Date: Tue, 5 May 2020 16:07:32 -0700 Subject: [PATCH] Fix for indexer symbol error (#2702) * Removed error sent as result in indexer plugin * Added code to filter query based on reserved keyword regex --- .../Plugins/Microsoft.Plugin.Indexer/Main.cs | 124 +++++++++--------- 1 file changed, 65 insertions(+), 59 deletions(-) diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Indexer/Main.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Indexer/Main.cs index f579cba84f..97196f95c8 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Indexer/Main.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Indexer/Main.cs @@ -12,6 +12,8 @@ using Microsoft.Plugin.Indexer.SearchHelper; using Microsoft.Search.Interop; using Microsoft.PowerToys.Settings.UI.Lib; using System.Windows.Controls; +using Wox.Infrastructure.Logger; +using System.Text.RegularExpressions; namespace Microsoft.Plugin.Indexer { @@ -30,6 +32,9 @@ namespace Microsoft.Plugin.Indexer // To access Windows Search functionalities private readonly WindowsSearchAPI _api = new WindowsSearchAPI(); + // Reserved keywords in oleDB + private string ReservedStringPattern = @"^[\/\\\$\%]+$"; + private IContextMenu _contextMenuLoader; // To save the configurations of plugins @@ -50,67 +55,68 @@ namespace Microsoft.Plugin.Indexer _settings.MaxSearchCount = 50; } - try - { - var searchResultsList = _api.Search(searchQuery, maxCount: _settings.MaxSearchCount).ToList(); - foreach (var searchResult in searchResultsList) - { - var path = searchResult.Path; + var regexMatch = Regex.Match(searchQuery, ReservedStringPattern); - string workingDir = null; - if (_settings.UseLocationAsWorkingDir) - workingDir = Path.GetDirectoryName(path); - - Result r = new Result(); - r.Title = searchResult.Title; - r.SubTitle = "Search: " + path; - r.IcoPath = path; - r.Action = c => - { - bool hide; - try - { - Process.Start(new ProcessStartInfo - { - FileName = path, - UseShellExecute = true, - WorkingDirectory = workingDir - }); - hide = true; - } - catch (Win32Exception) - { - var name = $"Plugin: {_context.CurrentPluginMetadata.Name}"; - var msg = "Can't Open this file"; - _context.API.ShowMsg(name, msg, string.Empty); - hide = false; - } - return hide; - }; - r.ContextData = searchResult; - - //If the result is a directory, then it's display should show a directory. - if(Directory.Exists(path)) + if (!regexMatch.Success) + { + try + { + var searchResultsList = _api.Search(searchQuery, maxCount: _settings.MaxSearchCount).ToList(); + foreach (var searchResult in searchResultsList) { - r.QueryTextDisplay = path; - } - - results.Add(r); - } - } - catch(InvalidOperationException) - { - //The connection has closed, internal error of ExecuteReader() - //Not showing this exception to the users - } - catch (Exception ex) - { - results.Add(new Result - { - Title = ex.ToString(), - IcoPath = "Images\\WindowsIndexerImg.bmp" - }); - } + var path = searchResult.Path; + + string workingDir = null; + if (_settings.UseLocationAsWorkingDir) + workingDir = Path.GetDirectoryName(path); + + Result r = new Result(); + r.Title = searchResult.Title; + r.SubTitle = "Search: " + path; + r.IcoPath = path; + r.Action = c => + { + bool hide; + try + { + Process.Start(new ProcessStartInfo + { + FileName = path, + UseShellExecute = true, + WorkingDirectory = workingDir + }); + hide = true; + } + catch (Win32Exception) + { + var name = $"Plugin: {_context.CurrentPluginMetadata.Name}"; + var msg = "Can't Open this file"; + _context.API.ShowMsg(name, msg, string.Empty); + hide = false; + } + return hide; + }; + r.ContextData = searchResult; + + //If the result is a directory, then it's display should show a directory. + if (Directory.Exists(path)) + { + r.QueryTextDisplay = path; + } + + results.Add(r); + } + } + catch (InvalidOperationException) + { + //The connection has closed, internal error of ExecuteReader() + //Not showing this exception to the users + } + catch (Exception ex) + { + Log.Info(ex.ToString()); + } + } } return results;