Improve FolderThemeProvider

Closes #33015

Signed-off-by: stianst <stianst@gmail.com>
This commit is contained in:
stianst 2024-09-16 13:46:16 +02:00 committed by Alexander Schwartz
parent 14368018b7
commit c137482d77

View file

@ -29,7 +29,7 @@ import java.util.Set;
*/ */
public class FolderThemeProvider implements ThemeProvider { public class FolderThemeProvider implements ThemeProvider {
private File themesDir; private final File themesDir;
public FolderThemeProvider(File themesDir) { public FolderThemeProvider(File themesDir) {
this.themesDir = themesDir; this.themesDir = themesDir;
@ -42,30 +42,20 @@ public class FolderThemeProvider implements ThemeProvider {
@Override @Override
public Theme getTheme(String name, Theme.Type type) throws IOException { public Theme getTheme(String name, Theme.Type type) throws IOException {
if (themesDir == null) {
return null;
}
File themeDir = getThemeDir(name, type); File themeDir = getThemeDir(name, type);
return themeDir.isDirectory() ? new FolderTheme(themeDir, name, type) : null; return themeDir != null ? new FolderTheme(themeDir, name, type) : null;
} }
@Override @Override
public Set<String> nameSet(Theme.Type type) { public Set<String> nameSet(Theme.Type type) {
if (themesDir == null) { if (themesDir == null || !themesDir.isDirectory()) {
return Collections.emptySet(); return Collections.emptySet();
} }
final String typeName = type.name().toLowerCase(); File[] themes = themesDir.listFiles(f -> f.isDirectory() && new File(f, type.name().toLowerCase()).isDirectory());
File[] themeDirs = themesDir.listFiles(new FileFilter() { if (themes != null && themes.length > 0) {
@Override Set<String> names = new HashSet<>();
public boolean accept(File pathname) { for (File themeDir : themes) {
return pathname.isDirectory() && new File(pathname, typeName).isDirectory();
}
});
if (themeDirs != null) {
Set<String> names = new HashSet<String>();
for (File themeDir : themeDirs) {
names.add(themeDir.getName()); names.add(themeDir.getName());
} }
return names; return names;
@ -76,7 +66,7 @@ public class FolderThemeProvider implements ThemeProvider {
@Override @Override
public boolean hasTheme(String name, Theme.Type type) { public boolean hasTheme(String name, Theme.Type type) {
return themesDir != null ? getThemeDir(name, type).isDirectory() : false; return getThemeDir(name, type) != null;
} }
@Override @Override
@ -84,15 +74,18 @@ public class FolderThemeProvider implements ThemeProvider {
} }
private File getThemeDir(String name, Theme.Type type) { private File getThemeDir(String name, Theme.Type type) {
File f = new File(themesDir, name + File.separator + type.name().toLowerCase()); if (themesDir == null || !themesDir.isDirectory()) {
try {
if (!f.getCanonicalPath().startsWith(themesDir.getCanonicalPath() + File.separator)) {
return null; return null;
} }
} catch (IOException e) {
return null; File[] themes = themesDir.listFiles(f -> f.isDirectory() && f.getName().equals(name));
if (themes != null && themes.length == 1) {
File themeTypeDir = new File(themes[0], type.name().toLowerCase());
if (themeTypeDir.isDirectory()) {
return themeTypeDir;
} }
return f; }
return null;
} }
} }