diff --git a/src/main/java/net/pms/store/container/MediaLibraryFolder.java b/src/main/java/net/pms/store/container/MediaLibraryFolder.java index a259b1fe480..910c9687f77 100644 --- a/src/main/java/net/pms/store/container/MediaLibraryFolder.java +++ b/src/main/java/net/pms/store/container/MediaLibraryFolder.java @@ -251,14 +251,15 @@ public synchronized void doRefreshChildren() { populatedFilesListFromDb = MediaTableFiles.getStrings(connection, firstSql); } case FILES_NOSORT_DEDUPED -> { - populatedFilesListFromDb = new ArrayList<>(); - filesListFromDb = new ArrayList<>(); + Set populatedFiles = new LinkedHashSet<>(); + List deduplicatedFiles = new ArrayList<>(); for (File item : MediaTableFiles.getFiles(connection, firstSql)) { - if (!populatedFilesListFromDb.contains(item.getAbsolutePath())) { - filesListFromDb.add(item); - populatedFilesListFromDb.add(item.getAbsolutePath()); + if (populatedFiles.add(item.getAbsolutePath())) { + deduplicatedFiles.add(item); } } + populatedFilesListFromDb = new ArrayList<>(populatedFiles); + filesListFromDb = deduplicatedFiles; } case EPISODES -> { filesListFromDb = MediaTableFiles.getFiles(connection, firstSql); @@ -374,28 +375,23 @@ public synchronized void doRefreshChildren() { } Set newFiles = new LinkedHashSet<>(); Set newVirtualFolders = new LinkedHashSet<>(); - List oldFiles = new ArrayList<>(); - List oldVirtualFolders = new ArrayList<>(); + boolean replaceChildren = filesListFromDb != null || virtualFoldersListFromDb != null; if (filesListFromDb != null) { - getChildren().forEach(oldFiles::add); - for (File file : filesListFromDb) { newFiles.add(file); } } if (virtualFoldersListFromDb != null) { - getChildren().forEach(oldVirtualFolders::add); - for (String f : virtualFoldersListFromDb) { newVirtualFolders.add(f); } } - oldFiles.forEach(fileResource -> getChildren().remove(fileResource)); - - oldVirtualFolders.forEach(virtualFolderResource -> getChildren().remove(virtualFolderResource)); + if (replaceChildren) { + getChildren().clear(); + } // Add filters at the top if (expectedOutput == TEXTS_NOSORT_WITH_FILTERS || expectedOutput == TEXTS_WITH_FILTERS || expectedOutput == FILES_WITH_FILTERS || expectedOutput == TVSERIES_WITH_FILTERS || expectedOutput == EMPTY_FILES_WITH_FILTERS) { diff --git a/src/main/java/net/pms/util/UMSUtils.java b/src/main/java/net/pms/util/UMSUtils.java index 1e1ebb3e918..2cfa891408d 100644 --- a/src/main/java/net/pms/util/UMSUtils.java +++ b/src/main/java/net/pms/util/UMSUtils.java @@ -24,6 +24,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; import net.pms.PMS; @@ -39,6 +40,7 @@ import org.apache.commons.text.StringEscapeUtils; public class UMSUtils { + private static final Pattern YOUTUBE_URL_PATTERN = Pattern.compile("(youtube\\.|youtu\\.be)"); /** * This class is not meant to be instantiated. @@ -76,48 +78,55 @@ public static void filterResourcesByName(List resources, String s if (resources == null || searchString == null) { return; } - searchString = searchString.toLowerCase(); - for (int i = resources.size() - 1; i >= 0; i--) { - StoreResource res = resources.get(i); - - if (res.isSearched()) { - continue; + String normalizedSearchString = searchString.toLowerCase(Locale.ROOT); + if (isExpectOneResult) { + for (int i = resources.size() - 1; i >= 0; i--) { + StoreResource resource = resources.get(i); + if (!resource.isSearched() && resourceMatches(resource, normalizedSearchString, isExactMatch)) { + resources.clear(); + resources.add(resource); + return; + } } - - boolean keep; - if (isExactMatch) { - keep = res.getName().toLowerCase().equals(searchString); - } else { - keep = res.getName().toLowerCase().contains(searchString); + List searchedResources = new ArrayList<>(); + for (StoreResource resource : resources) { + if (resource.isSearched()) { + searchedResources.add(resource); + } } + resources.clear(); + resources.addAll(searchedResources); + return; + } - final MediaInfo media = res.getMediaInfo(); - - if (keep && isExpectOneResult) { - resources.clear(); - resources.add(res); - break; + List matchingResources = new ArrayList<>(resources.size()); + for (StoreResource resource : resources) { + if (resource.isSearched() || resourceMatches(resource, normalizedSearchString, isExactMatch)) { + matchingResources.add(resource); } + } + resources.clear(); + resources.addAll(matchingResources); + } - if (!keep) { - if (media != null && media.hasAudioMetadata()) { - MediaAudioMetadata audioMetadata = media.getAudioMetadata(); - if (audioMetadata.getAlbum() != null) { - keep |= audioMetadata.getAlbum().toLowerCase().contains(searchString); - } - //TODO maciekberry: check whether it makes sense to use Album Artist - if (audioMetadata.getArtist() != null) { - keep |= audioMetadata.getArtist().toLowerCase().contains(searchString); - } - if (audioMetadata.getSongname() != null) { - keep |= audioMetadata.getSongname().toLowerCase().contains(searchString); - } - } - } - if (!keep) { - resources.remove(i); - } + private static boolean resourceMatches(StoreResource resource, String searchString, boolean exactMatch) { + String resourceName = resource.getName().toLowerCase(Locale.ROOT); + if (exactMatch ? resourceName.equals(searchString) : resourceName.contains(searchString)) { + return true; } + + MediaInfo media = resource.getMediaInfo(); + if (media == null || !media.hasAudioMetadata()) { + return false; + } + MediaAudioMetadata audioMetadata = media.getAudioMetadata(); + return containsIgnoreCase(audioMetadata.getAlbum(), searchString) || + containsIgnoreCase(audioMetadata.getArtist(), searchString) || + containsIgnoreCase(audioMetadata.getSongname(), searchString); + } + + private static boolean containsIgnoreCase(String value, String normalizedSearchString) { + return value != null && value.toLowerCase(Locale.ROOT).contains(normalizedSearchString); } public static String playedDurationStr(String current, String duration) { @@ -279,7 +288,7 @@ public static JsonArray getArraysAsJsonArrayOfObjects(String[] values, String[] * @return an array of objects in the format: [ { "value": "bar", "label": * "foo" }, ... ] */ - public static synchronized JsonArray getListsAsJsonArrayOfObjects(List values, List labels, JsonArray jsonArray) { + public static JsonArray getListsAsJsonArrayOfObjects(List values, List labels, JsonArray jsonArray) { if (jsonArray == null) { jsonArray = new JsonArray(); } @@ -297,9 +306,7 @@ public static synchronized JsonArray getListsAsJsonArrayOfObjects(List v } public static boolean isYouTubeURL(String youTubeUrl) { - String pattern = "(youtube\\.|youtu\\.be)"; - Pattern compiledPattern = Pattern.compile(pattern); - Matcher matcher = compiledPattern.matcher(youTubeUrl); + Matcher matcher = YOUTUBE_URL_PATTERN.matcher(youTubeUrl); return matcher.find(); } } diff --git a/src/test/java/net/pms/util/UMSUtilsTest.java b/src/test/java/net/pms/util/UMSUtilsTest.java index ba4e7576ee9..6ec10669cee 100644 --- a/src/test/java/net/pms/util/UMSUtilsTest.java +++ b/src/test/java/net/pms/util/UMSUtilsTest.java @@ -16,9 +16,14 @@ */ package net.pms.util; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; import net.pms.PMS; import net.pms.TestHelper; import net.pms.configuration.UmsConfiguration; +import net.pms.media.MediaInfo; +import net.pms.store.StoreResource; import org.apache.commons.configuration2.ex.ConfigurationException; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.BeforeEach; @@ -51,4 +56,86 @@ public void testIsYouTubeURL() throws Exception { assertTrue(UMSUtils.isYouTubeURL("https://www.youtube.com/shorts/jsBOx45iBX0")); assertFalse(UMSUtils.isYouTubeURL("https://www.somewebsite.com/shorts/jsBOx45iBX0")); } + + @Test + public void testFilterResourcesByNameDoesNotLoadMediaForNameMatch() { + TestResource matchingResource = new TestResource("Matching resource"); + TestResource otherResource = new TestResource("Other resource"); + List resources = new ArrayList<>(List.of(matchingResource, otherResource)); + + UMSUtils.filterResourcesByName(resources, "matching", false, false); + + assertEquals(List.of(matchingResource), resources); + assertEquals(0, matchingResource.getMediaInfoCalls); + assertEquals(1, otherResource.getMediaInfoCalls); + } + + @Test + public void testFilterResourcesByNameUsesLocaleIndependentCaseConversion() { + Locale previousLocale = Locale.getDefault(); + try { + Locale.setDefault(Locale.forLanguageTag("tr-TR")); + TestResource resource = new TestResource("TITLE"); + List resources = new ArrayList<>(List.of(resource)); + + UMSUtils.filterResourcesByName(resources, "title", false, true); + + assertEquals(List.of(resource), resources); + } finally { + Locale.setDefault(previousLocale); + } + } + + @Test + public void testFilterResourcesByNameExpectOneResultKeepsLastMatch() { + TestResource first = new TestResource("match first"); + TestResource last = new TestResource("match last"); + List resources = new ArrayList<>(List.of(first, last)); + + UMSUtils.filterResourcesByName(resources, "match", true, false); + + assertEquals(List.of(last), resources); + assertEquals(0, last.getMediaInfoCalls); + } + + private static final class TestResource extends StoreResource { + private final String name; + private int getMediaInfoCalls; + + private TestResource(String name) { + super(null); + this.name = name; + } + + @Override + public String getName() { + return name; + } + + @Override + public String getSystemName() { + return name; + } + + @Override + public long length() { + return 0; + } + + @Override + public boolean isFolder() { + return false; + } + + @Override + public boolean isValid() { + return true; + } + + @Override + public MediaInfo getMediaInfo() { + getMediaInfoCalls++; + return null; + } + } }