mirror of https://github.com/halo-dev/halo
Change theme property list to set
parent
01b57826f0
commit
ef9b6eb9f8
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.
|
||||||
|
|
|
@ -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<Path> themePaths = Files.list(getBasePath()).filter(path -> Files.isDirectory(path)).collect(Collectors.toList());
|
|
||||||
|
|
||||||
if (CollectionUtils.isEmpty(themePaths)) {
|
// List and filter sub folders
|
||||||
return Collections.emptyList();
|
List<Path> themePaths = Files.list(getBasePath()).filter(path -> Files.isDirectory(path)).collect(Collectors.toList());
|
||||||
}
|
|
||||||
|
|
||||||
// Get theme properties
|
if (CollectionUtils.isEmpty(themePaths)) {
|
||||||
List<ThemeProperty> themes = themePaths.stream().map(this::getProperty).collect(Collectors.toList());
|
return Collections.emptySet();
|
||||||
|
|
||||||
// Cache the themes
|
|
||||||
cacheStore.put(THEMES_CACHE_KEY, JsonUtils.objectToJson(themes));
|
|
||||||
|
|
||||||
return themes;
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new ServiceException("Themes scan failed", e);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
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
|
@Override
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue