diff --git a/pom.xml b/pom.xml index cbd643638..762a89517 100755 --- a/pom.xml +++ b/pom.xml @@ -229,13 +229,20 @@ ${httpclient.version} + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + 2.9.2 + + - - - + + + @@ -244,15 +251,15 @@ spring-boot-maven-plugin - - - - - - - - - + + + + + + + + + diff --git a/src/main/java/run/halo/app/service/ThemeService.java b/src/main/java/run/halo/app/service/ThemeService.java index 5a4b15dfa..2570883bb 100644 --- a/src/main/java/run/halo/app/service/ThemeService.java +++ b/src/main/java/run/halo/app/service/ThemeService.java @@ -1,13 +1,14 @@ package run.halo.app.service; import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; import run.halo.app.model.support.Theme; import run.halo.app.model.support.ThemeFile; import run.halo.app.model.support.ThemeProperties; import java.io.File; +import java.nio.file.Path; import java.util.List; -import java.util.Map; /** * @author : RYAN0UP @@ -70,6 +71,13 @@ public interface ThemeService { */ File getThemeBasePath(); + /** + * Gets theme base path. + * + * @return theme base path + */ + Path getBasePath(); + /** * Get theme Properties. * @@ -93,6 +101,7 @@ public interface ThemeService { * @param absolutePath absolute path * @param content new content */ + @Deprecated void saveTemplateContent(@NonNull String absolutePath, @NonNull String content); /** @@ -106,8 +115,8 @@ public interface ThemeService { * Fetchs theme configuration. * * @param themeName theme name must not be blank - * @return theme configuration + * @return theme configuration or null if not found */ - @NonNull - Map fetchConfig(@NonNull String themeName); + @Nullable + Object fetchConfig(@NonNull String themeName); } 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 9802a8ff5..528a9ef79 100644 --- a/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java @@ -6,6 +6,9 @@ import cn.hutool.core.io.file.FileWriter; import cn.hutool.core.text.StrBuilder; import cn.hutool.core.util.StrUtil; import cn.hutool.setting.dialect.Props; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.util.Assert; import run.halo.app.config.properties.HaloProperties; @@ -18,15 +21,19 @@ import run.halo.app.service.ThemeService; import run.halo.app.utils.FilenameUtils; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Comparator; import java.util.List; -import java.util.Map; /** * @author : RYAN0UP * @date : 2019/3/26 */ +@Slf4j @Service public class ThemeServiceImpl implements ThemeService { @@ -40,10 +47,17 @@ public class ThemeServiceImpl implements ThemeService { */ private static String[] FILTER_FILES = {".git", ".DS_Store", "theme.properties"}; - private final HaloProperties haloProperties; + private final static String THEME_FOLDER = "templates/themes"; + + private final static String[] OPTIONS_NAMES = {"options.yaml", "options.yml"}; + + private final Path workDir; + + private final ObjectMapper yamlMapper; public ThemeServiceImpl(HaloProperties haloProperties) { - this.haloProperties = haloProperties; + yamlMapper = new ObjectMapper(new YAMLFactory()); + workDir = Paths.get(haloProperties.getWorkDir(), THEME_FOLDER); } /** @@ -193,7 +207,12 @@ public class ThemeServiceImpl implements ThemeService { */ @Override public File getThemeBasePath() { - return new File(haloProperties.getWorkDir(), "templates/themes"); + return getBasePath().toFile(); + } + + @Override + public Path getBasePath() { + return workDir; } /** @@ -260,10 +279,29 @@ public class ThemeServiceImpl implements ThemeService { } @Override - public Map fetchConfig(String themeName) { + public Object fetchConfig(String themeName) { Assert.hasText(themeName, "Theme name must not be blank"); + try { + for (String optionsName : OPTIONS_NAMES) { + // Resolve the options path + Path optionsPath = workDir.resolve(themeName).resolve(optionsName); - return null; + log.debug("Finding options in: [{}]", optionsPath.toString()); + + // Check existence + if (!Files.exists(optionsPath)) { + continue; + } + + // Read the yaml file and return the object value + return yamlMapper.readValue(optionsPath.toFile(), Object.class); + } + + return null; + } catch (IOException e) { + log.error("Failed to read options.yaml", e); + return null; + } } } 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 c34bcf4e6..d70af1e80 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 @@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.*; import run.halo.app.model.enums.OptionSource; import run.halo.app.model.properties.PrimaryProperties; import run.halo.app.model.properties.PropertyEnum; +import run.halo.app.model.support.BaseResponse; import run.halo.app.model.support.HaloConst; import run.halo.app.model.support.Theme; import run.halo.app.model.support.ThemeFile; @@ -106,4 +107,10 @@ public class ThemeController { public void deleteBy(@PathVariable("key") String key) { themeService.deleteTheme(key); } + + @GetMapping("configurations") + @ApiOperation("Fetches theme configuration") + public BaseResponse fetchConfig(@RequestParam("name") String name) { + return BaseResponse.ok(themeService.fetchConfig(name)); + } } diff --git a/src/main/resources/templates/themes/anatole/options.yaml b/src/main/resources/templates/themes/anatole/options.yaml index 6f9d79280..0f03bec5b 100644 --- a/src/main/resources/templates/themes/anatole/options.yaml +++ b/src/main/resources/templates/themes/anatole/options.yaml @@ -1,5 +1,4 @@ sns: - name: sns description: 社交资料设置 items: rss: @@ -39,7 +38,6 @@ sns: description: Telegram type: text style: - name: style description: 样式设置 items: icon: