Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions src/main/java/net/pms/store/container/MediaLibraryFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,15 @@ public synchronized void doRefreshChildren() {
populatedFilesListFromDb = MediaTableFiles.getStrings(connection, firstSql);
}
case FILES_NOSORT_DEDUPED -> {
populatedFilesListFromDb = new ArrayList<>();
filesListFromDb = new ArrayList<>();
Set<String> populatedFiles = new LinkedHashSet<>();
List<File> 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);
Expand Down Expand Up @@ -374,28 +375,23 @@ public synchronized void doRefreshChildren() {
}
Set<File> newFiles = new LinkedHashSet<>();
Set<String> newVirtualFolders = new LinkedHashSet<>();
List<StoreResource> oldFiles = new ArrayList<>();
List<StoreResource> 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) {
Expand Down
87 changes: 47 additions & 40 deletions src/main/java/net/pms/util/UMSUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -76,48 +78,55 @@ public static void filterResourcesByName(List<StoreResource> 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<StoreResource> 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<StoreResource> 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) {
Expand Down Expand Up @@ -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<String> values, List<String> labels, JsonArray jsonArray) {
public static JsonArray getListsAsJsonArrayOfObjects(List<String> values, List<String> labels, JsonArray jsonArray) {
if (jsonArray == null) {
jsonArray = new JsonArray();
}
Expand All @@ -297,9 +306,7 @@ public static synchronized JsonArray getListsAsJsonArrayOfObjects(List<String> 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();
}
}
87 changes: 87 additions & 0 deletions src/test/java/net/pms/util/UMSUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<StoreResource> 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<StoreResource> 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<StoreResource> 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;
}
}
}
Loading