Complete theme configuration fetch api

pull/146/head
johnniang 2019-04-08 16:08:19 +08:00
parent f362a56bc7
commit 23cd1d17fc
5 changed files with 83 additions and 24 deletions

31
pom.xml
View File

@ -229,13 +229,20 @@
<version>${httpclient.version}</version> <version>${httpclient.version}</version>
</dependency> </dependency>
<!-- Jackson yaml -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<!--<testResources>--> <!--<testResources>-->
<!--<testResource>--> <!--<testResource>-->
<!--<directory>${basedir}/src/main/resources</directory>--> <!--<directory>${basedir}/src/main/resources</directory>-->
<!--</testResource>--> <!--</testResource>-->
<!--</testResources>--> <!--</testResources>-->
<plugins> <plugins>
<!-- Spring Boot plugin --> <!-- Spring Boot plugin -->
@ -244,15 +251,15 @@
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
</plugin> </plugin>
<!--<plugin>--> <!--<plugin>-->
<!--<groupId>org.apache.maven.plugins</groupId>--> <!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-jar-plugin</artifactId>--> <!--<artifactId>maven-jar-plugin</artifactId>-->
<!--<version>3.0.2</version>--> <!--<version>3.0.2</version>-->
<!--<configuration>--> <!--<configuration>-->
<!--<excludes>--> <!--<excludes>-->
<!--<exclude>/**/application-dev.yaml</exclude>--> <!--<exclude>/**/application-dev.yaml</exclude>-->
<!--<exclude>/**/application-test.yaml</exclude>--> <!--<exclude>/**/application-test.yaml</exclude>-->
<!--</excludes>--> <!--</excludes>-->
<!--</configuration>--> <!--</configuration>-->
<!--</plugin>--> <!--</plugin>-->
</plugins> </plugins>
</build> </build>

View File

@ -1,13 +1,14 @@
package run.halo.app.service; package run.halo.app.service;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import run.halo.app.model.support.Theme; import run.halo.app.model.support.Theme;
import run.halo.app.model.support.ThemeFile; import run.halo.app.model.support.ThemeFile;
import run.halo.app.model.support.ThemeProperties; import run.halo.app.model.support.ThemeProperties;
import java.io.File; import java.io.File;
import java.nio.file.Path;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @author : RYAN0UP * @author : RYAN0UP
@ -70,6 +71,13 @@ public interface ThemeService {
*/ */
File getThemeBasePath(); File getThemeBasePath();
/**
* Gets theme base path.
*
* @return theme base path
*/
Path getBasePath();
/** /**
* Get theme Properties. * Get theme Properties.
* *
@ -93,6 +101,7 @@ public interface ThemeService {
* @param absolutePath absolute path * @param absolutePath absolute path
* @param content new content * @param content new content
*/ */
@Deprecated
void saveTemplateContent(@NonNull String absolutePath, @NonNull String content); void saveTemplateContent(@NonNull String absolutePath, @NonNull String content);
/** /**
@ -106,8 +115,8 @@ public interface ThemeService {
* Fetchs theme configuration. * Fetchs theme configuration.
* *
* @param themeName theme name must not be blank * @param themeName theme name must not be blank
* @return theme configuration * @return theme configuration or null if not found
*/ */
@NonNull @Nullable
Map<String, Object> fetchConfig(@NonNull String themeName); Object fetchConfig(@NonNull String themeName);
} }

View File

@ -6,6 +6,9 @@ import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.text.StrBuilder; import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.setting.dialect.Props; 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.stereotype.Service;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import run.halo.app.config.properties.HaloProperties; import run.halo.app.config.properties.HaloProperties;
@ -18,15 +21,19 @@ import run.halo.app.service.ThemeService;
import run.halo.app.utils.FilenameUtils; import run.halo.app.utils.FilenameUtils;
import java.io.File; 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.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @author : RYAN0UP * @author : RYAN0UP
* @date : 2019/3/26 * @date : 2019/3/26
*/ */
@Slf4j
@Service @Service
public class ThemeServiceImpl implements ThemeService { 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 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) { 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 @Override
public File getThemeBasePath() { 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 @Override
public Map<String, Object> fetchConfig(String themeName) { public Object fetchConfig(String themeName) {
Assert.hasText(themeName, "Theme name must not be blank"); 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;
}
} }
} }

View File

@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.*;
import run.halo.app.model.enums.OptionSource; import run.halo.app.model.enums.OptionSource;
import run.halo.app.model.properties.PrimaryProperties; import run.halo.app.model.properties.PrimaryProperties;
import run.halo.app.model.properties.PropertyEnum; 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.HaloConst;
import run.halo.app.model.support.Theme; import run.halo.app.model.support.Theme;
import run.halo.app.model.support.ThemeFile; import run.halo.app.model.support.ThemeFile;
@ -106,4 +107,10 @@ public class ThemeController {
public void deleteBy(@PathVariable("key") String key) { public void deleteBy(@PathVariable("key") String key) {
themeService.deleteTheme(key); themeService.deleteTheme(key);
} }
@GetMapping("configurations")
@ApiOperation("Fetches theme configuration")
public BaseResponse<Object> fetchConfig(@RequestParam("name") String name) {
return BaseResponse.ok(themeService.fetchConfig(name));
}
} }

View File

@ -1,5 +1,4 @@
sns: sns:
name: sns
description: 社交资料设置 description: 社交资料设置
items: items:
rss: rss:
@ -39,7 +38,6 @@ sns:
description: Telegram description: Telegram
type: text type: text
style: style:
name: style
description: 样式设置 description: 样式设置
items: items:
icon: icon: