From ef9b6eb9f81d439f8418e50f7f626c45d87733bc Mon Sep 17 00:00:00 2001 From: johnniang Date: Fri, 19 Apr 2019 10:49:34 +0800 Subject: [PATCH] Change theme property list to set --- .../theme/config/support/ThemeProperty.java | 15 ++++++ .../run/halo/app/service/ThemeService.java | 5 +- .../app/service/impl/ThemeServiceImpl.java | 49 +++++++++---------- .../controller/admin/api/ThemeController.java | 5 +- 4 files changed, 43 insertions(+), 31 deletions(-) diff --git a/src/main/java/run/halo/app/handler/theme/config/support/ThemeProperty.java b/src/main/java/run/halo/app/handler/theme/config/support/ThemeProperty.java index 69b2d0557..6b6f4afa9 100644 --- a/src/main/java/run/halo/app/handler/theme/config/support/ThemeProperty.java +++ b/src/main/java/run/halo/app/handler/theme/config/support/ThemeProperty.java @@ -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); + } } diff --git a/src/main/java/run/halo/app/service/ThemeService.java b/src/main/java/run/halo/app/service/ThemeService.java index aae1486c8..7d24242dd 100644 --- a/src/main/java/run/halo/app/service/ThemeService.java +++ b/src/main/java/run/halo/app/service/ThemeService.java @@ -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 getThemes(); + Set getThemes(); /** * Lists theme folder by absolute path. diff --git a/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java b/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java index 192ea008f..42e9ebc3c 100644 --- a/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java @@ -93,7 +93,7 @@ public class ThemeServiceImpl implements ThemeService { public Optional getThemeBy(String themeId) { Assert.hasText(themeId, "Theme id must not be blank"); - List themes = getThemes(); + Set themes = getThemes(); log.debug("Themes type: [{}]", themes.getClass()); log.debug("Themes: [{}]", themes); @@ -102,38 +102,33 @@ public class ThemeServiceImpl implements ThemeService { } @Override - public List getThemes() { - // Fetch themes from cache - List result = cacheStore.get(THEMES_CACHE_KEY).map(themesCache -> { - try { + public Set getThemes() { + Optional 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 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 themePaths = Files.list(getBasePath()).filter(path -> Files.isDirectory(path)).collect(Collectors.toList()); - // Get theme properties - List 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 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 diff --git a/src/main/java/run/halo/app/web/controller/admin/api/ThemeController.java b/src/main/java/run/halo/app/web/controller/admin/api/ThemeController.java index b326c1773..a6354cb02 100644 --- a/src/main/java/run/halo/app/web/controller/admin/api/ThemeController.java +++ b/src/main/java/run/halo/app/web/controller/admin/api/ThemeController.java @@ -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 listAll() { + public Set 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); } }