mirror of https://github.com/halo-dev/halo
Complete theme configuration fetch api
parent
f362a56bc7
commit
23cd1d17fc
31
pom.xml
31
pom.xml
|
@ -229,13 +229,20 @@
|
|||
<version>${httpclient.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Jackson yaml -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-yaml</artifactId>
|
||||
<version>2.9.2</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<!--<testResources>-->
|
||||
<!--<testResource>-->
|
||||
<!--<directory>${basedir}/src/main/resources</directory>-->
|
||||
<!--</testResource>-->
|
||||
<!--<testResource>-->
|
||||
<!--<directory>${basedir}/src/main/resources</directory>-->
|
||||
<!--</testResource>-->
|
||||
<!--</testResources>-->
|
||||
<plugins>
|
||||
<!-- Spring Boot plugin -->
|
||||
|
@ -244,15 +251,15 @@
|
|||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<!--<plugin>-->
|
||||
<!--<groupId>org.apache.maven.plugins</groupId>-->
|
||||
<!--<artifactId>maven-jar-plugin</artifactId>-->
|
||||
<!--<version>3.0.2</version>-->
|
||||
<!--<configuration>-->
|
||||
<!--<excludes>-->
|
||||
<!--<exclude>/**/application-dev.yaml</exclude>-->
|
||||
<!--<exclude>/**/application-test.yaml</exclude>-->
|
||||
<!--</excludes>-->
|
||||
<!--</configuration>-->
|
||||
<!--<groupId>org.apache.maven.plugins</groupId>-->
|
||||
<!--<artifactId>maven-jar-plugin</artifactId>-->
|
||||
<!--<version>3.0.2</version>-->
|
||||
<!--<configuration>-->
|
||||
<!--<excludes>-->
|
||||
<!--<exclude>/**/application-dev.yaml</exclude>-->
|
||||
<!--<exclude>/**/application-test.yaml</exclude>-->
|
||||
<!--</excludes>-->
|
||||
<!--</configuration>-->
|
||||
<!--</plugin>-->
|
||||
</plugins>
|
||||
</build>
|
||||
|
|
|
@ -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<String, Object> fetchConfig(@NonNull String themeName);
|
||||
@Nullable
|
||||
Object fetchConfig(@NonNull String themeName);
|
||||
}
|
||||
|
|
|
@ -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<String, Object> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Object> fetchConfig(@RequestParam("name") String name) {
|
||||
return BaseResponse.ok(themeService.fetchConfig(name));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue