Change theme property list to set

pull/146/head
johnniang 2019-04-19 10:49:34 +08:00
parent 01b57826f0
commit ef9b6eb9f8
4 changed files with 43 additions and 31 deletions

View File

@ -2,6 +2,8 @@ package run.halo.app.handler.theme.config.support;
import lombok.Data;
import java.util.Objects;
/**
* Theme property.
*
@ -84,4 +86,17 @@ public class ThemeProperty {
*/
private String avatar;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ThemeProperty that = (ThemeProperty) o;
return id.equals(that.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}

View File

@ -11,6 +11,7 @@ import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.Set;
/**
* @author : RYAN0UP
@ -86,10 +87,10 @@ public interface ThemeService {
/**
* Gets all themes
*
* @return list of themes
* @return set of themes
*/
@NonNull
List<ThemeProperty> getThemes();
Set<ThemeProperty> getThemes();
/**
* Lists theme folder by absolute path.

View File

@ -93,7 +93,7 @@ public class ThemeServiceImpl implements ThemeService {
public Optional<ThemeProperty> getThemeBy(String themeId) {
Assert.hasText(themeId, "Theme id must not be blank");
List<ThemeProperty> themes = getThemes();
Set<ThemeProperty> themes = getThemes();
log.debug("Themes type: [{}]", themes.getClass());
log.debug("Themes: [{}]", themes);
@ -102,38 +102,33 @@ public class ThemeServiceImpl implements ThemeService {
}
@Override
public List<ThemeProperty> getThemes() {
// Fetch themes from cache
List<ThemeProperty> result = cacheStore.get(THEMES_CACHE_KEY).map(themesCache -> {
try {
public Set<ThemeProperty> getThemes() {
Optional<String> themeCacheString = cacheStore.get(THEMES_CACHE_KEY);
try {
if (themeCacheString.isPresent()) {
// Convert to theme properties
ThemeProperty[] themeProperties = JsonUtils.jsonToObject(themesCache, ThemeProperty[].class);
return Arrays.asList(themeProperties);
} catch (IOException e) {
throw new ServiceException("Failed to parse json", e);
ThemeProperty[] themeProperties = JsonUtils.jsonToObject(themeCacheString.get(), ThemeProperty[].class);
return new HashSet<>(Arrays.asList(themeProperties));
}
}).orElseGet(() -> {
try {
// List and filter sub folders
List<Path> themePaths = Files.list(getBasePath()).filter(path -> Files.isDirectory(path)).collect(Collectors.toList());
if (CollectionUtils.isEmpty(themePaths)) {
return Collections.emptyList();
}
// List and filter sub folders
List<Path> themePaths = Files.list(getBasePath()).filter(path -> Files.isDirectory(path)).collect(Collectors.toList());
// Get theme properties
List<ThemeProperty> themes = themePaths.stream().map(this::getProperty).collect(Collectors.toList());
// Cache the themes
cacheStore.put(THEMES_CACHE_KEY, JsonUtils.objectToJson(themes));
return themes;
} catch (Exception e) {
throw new ServiceException("Themes scan failed", e);
if (CollectionUtils.isEmpty(themePaths)) {
return Collections.emptySet();
}
});
return CollectionUtils.isEmpty(result) ? Collections.emptyList() : result;
// Get theme properties
Set<ThemeProperty> themes = themePaths.stream().map(this::getProperty).collect(Collectors.toSet());
// Cache the themes
cacheStore.put(THEMES_CACHE_KEY, JsonUtils.objectToJson(themes));
return themes;
} catch (IOException e) {
throw new ServiceException("Failed to get themes", e);
}
}
@Override

View File

@ -12,6 +12,7 @@ import run.halo.app.service.ThemeSettingService;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Theme controller.
@ -41,7 +42,7 @@ public class ThemeController {
@GetMapping
@ApiOperation("List all themes")
public List<ThemeProperty> listAll() {
public Set<ThemeProperty> listAll() {
return themeService.getThemes();
}
@ -118,7 +119,7 @@ public class ThemeController {
@PostMapping("upload")
@ApiOperation("Upload theme")
public ThemeProperty uploadTheme(@RequestPart("file") MultipartFile file){
public ThemeProperty uploadTheme(@RequestPart("file") MultipartFile file) {
return themeService.upload(file);
}
}