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 lombok.Data;
import java.util.Objects;
/** /**
* Theme property. * Theme property.
* *
@ -84,4 +86,17 @@ public class ThemeProperty {
*/ */
private String avatar; 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.nio.file.Path;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
/** /**
* @author : RYAN0UP * @author : RYAN0UP
@ -86,10 +87,10 @@ public interface ThemeService {
/** /**
* Gets all themes * Gets all themes
* *
* @return list of themes * @return set of themes
*/ */
@NonNull @NonNull
List<ThemeProperty> getThemes(); Set<ThemeProperty> getThemes();
/** /**
* Lists theme folder by absolute path. * Lists theme folder by absolute path.

View File

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

View File

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