From 53f4009b5dc64b1aa679bfd28056cc7712c57d39 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Mon, 7 Feb 2022 14:41:01 +0000 Subject: [PATCH] evaluate regex queries --- .../com/muwire/core/files/FileManager.groovy | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/core/src/main/groovy/com/muwire/core/files/FileManager.groovy b/core/src/main/groovy/com/muwire/core/files/FileManager.groovy index 9cedf51a..0eafb63b 100644 --- a/core/src/main/groovy/com/muwire/core/files/FileManager.groovy +++ b/core/src/main/groovy/com/muwire/core/files/FileManager.groovy @@ -2,6 +2,9 @@ package com.muwire.core.files import java.nio.file.Path import java.util.function.Predicate +import java.util.regex.Matcher +import java.util.regex.Pattern +import java.util.regex.PatternSyntaxException import java.util.stream.Collectors import com.muwire.core.EventBus @@ -289,6 +292,34 @@ class FileManager { found.each { it.hit(e.persona, e.timestamp, "Hash Search") } re = new ResultsEvent(results: found, uuid: e.uuid, searchEvent: e) } + } else if (e.regex) { + // check if valid regex + try { + Pattern pattern = Pattern.compile(e.searchTerms[0]) + Set results + synchronized (fileToSharedFile) { + results = fileToSharedFile.values().stream().filter{ + Matcher matcher = pattern.matcher(it.getFile().getName()) + if (matcher.matches()) + return true + if (e.searchComments && it.getComment() != null) { + matcher = pattern.matcher(DataUtil.readi18nString(Base64.decode(it.getComment()))) + if (matcher.matches()) + return true + } + if (e.searchPaths && settings.showPaths) { + matcher = pattern.matcher(it.getCachedVisiblePath()) + if (matcher.matches()) + return true + } + return false + }.collect(Collectors.toSet()) + } + results.each {it.hit(e.persona, e.timestamp, "/${e.searchTerms[0]}/")} + re = new ResultsEvent(results: results.asList(), uuid: e.uuid, searchEvent: e) + } catch (PatternSyntaxException bad) { + log.info("invalid regex received $e") + } } else { def names = index.search e.searchTerms Set files = new HashSet<>()