Create ThemeService

pull/137/head
ruibaby 2019-03-26 15:00:29 +08:00
parent 3b451491a8
commit ca73a30c78
3 changed files with 94 additions and 18 deletions

View File

@ -0,0 +1,68 @@
package cc.ryanc.halo.service;
import cc.ryanc.halo.model.support.Theme;
import cc.ryanc.halo.model.support.ThemeProperties;
import java.io.File;
import java.util.List;
/**
* @author : RYAN0UP
* @date : 2019/3/26
*/
public interface ThemeService {
/**
* Gets all themes
*
* @return list of themes
*/
List<Theme> getThemes();
/**
* Gets theme templates
*
* @param theme theme
* @return List<String>
*/
List<String> getTemplates(String theme);
/**
* Gets custom template, such as page_xxx.ftl, and xxx will be template name
*
* @param theme theme name
* @return List
*/
List<String> getCustomTpl(String theme);
/**
* Judging whether template exists under the specified theme
*
* @param template template
* @return boolean
*/
boolean isTemplateExist(String template);
/**
* Judging whether theme exists under template path
*
* @param theme theme name
* @return boolean
*/
boolean isThemeExist(String theme);
/**
* Gets theme base path.
*
* @return File
*/
File getThemeBasePath();
/**
* Get theme Properties.
*
* @param path path
* @return ThemeProperties
*/
ThemeProperties getProperties(File path);
}

View File

@ -1,34 +1,33 @@
package cc.ryanc.halo.utils; package cc.ryanc.halo.service.impl;
import cc.ryanc.halo.model.support.HaloConst; import cc.ryanc.halo.model.support.HaloConst;
import cc.ryanc.halo.model.support.Theme; import cc.ryanc.halo.model.support.Theme;
import cc.ryanc.halo.model.support.ThemeProperties; import cc.ryanc.halo.model.support.ThemeProperties;
import cc.ryanc.halo.service.ThemeService;
import cc.ryanc.halo.web.controller.content.base.BaseContentController; import cc.ryanc.halo.web.controller.content.base.BaseContentController;
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 lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
* Theme utils
*
* @author : RYAN0UP * @author : RYAN0UP
* @date : 2019/3/26 * @date : 2019/3/26
*/ */
@Slf4j @Service
public class ThemeUtils { public class ThemeServiceImpl implements ThemeService {
/** /**
* Gets all themes * Gets all themes
* *
* @return list of themes * @return list of themes
*/ */
public static List<Theme> getThemes() { @Override
public List<Theme> getThemes() {
final List<Theme> themes = new ArrayList<>(); final List<Theme> themes = new ArrayList<>();
final File[] files = getThemeBasePath().listFiles(); final File[] files = getThemeBasePath().listFiles();
try { try {
@ -62,7 +61,8 @@ public class ThemeUtils {
* @param theme theme * @param theme theme
* @return List<String> * @return List<String>
*/ */
public static List<String> getTemplates(String theme) { @Override
public List<String> getTemplates(String theme) {
final List<String> templates = new ArrayList<>(); final List<String> templates = new ArrayList<>();
try { try {
final File themesPath = new File(getThemeBasePath(), theme); final File themesPath = new File(getThemeBasePath(), theme);
@ -95,7 +95,8 @@ public class ThemeUtils {
* @param theme theme name * @param theme theme name
* @return List * @return List
*/ */
public static List<String> getCustomTpl(String theme) throws FileNotFoundException { @Override
public List<String> getCustomTpl(String theme) {
final List<String> templates = new ArrayList<>(); final List<String> templates = new ArrayList<>();
final File themePath = new File(getThemeBasePath(), theme); final File themePath = new File(getThemeBasePath(), theme);
final File[] themeFiles = themePath.listFiles(); final File[] themeFiles = themePath.listFiles();
@ -116,7 +117,8 @@ public class ThemeUtils {
* @param template template * @param template template
* @return boolean * @return boolean
*/ */
public static boolean isTemplateExist(String template) throws FileNotFoundException { @Override
public boolean isTemplateExist(String template) {
StrBuilder templatePath = new StrBuilder(BaseContentController.THEME); StrBuilder templatePath = new StrBuilder(BaseContentController.THEME);
templatePath.append("/"); templatePath.append("/");
templatePath.append(template); templatePath.append(template);
@ -129,9 +131,9 @@ public class ThemeUtils {
* *
* @param theme theme name * @param theme theme name
* @return boolean * @return boolean
* @throws FileNotFoundException FileNotFoundException
*/ */
public static boolean isThemeExist(String theme) throws FileNotFoundException { @Override
public boolean isThemeExist(String theme) {
File file = new File(getThemeBasePath(), theme); File file = new File(getThemeBasePath(), theme);
return file.exists(); return file.exists();
} }
@ -141,7 +143,8 @@ public class ThemeUtils {
* *
* @return File * @return File
*/ */
private static File getThemeBasePath() { @Override
public File getThemeBasePath() {
return new File(System.getProperties().getProperty("user.home"), ".halo/templates/themes"); return new File(System.getProperties().getProperty("user.home"), ".halo/templates/themes");
} }
@ -151,7 +154,8 @@ public class ThemeUtils {
* @param path path * @param path path
* @return ThemeProperties * @return ThemeProperties
*/ */
private static ThemeProperties getProperties(File path) { @Override
public ThemeProperties getProperties(File path) {
File propertiesFile = new File(path, "theme.properties"); File propertiesFile = new File(path, "theme.properties");
ThemeProperties properties = new ThemeProperties(); ThemeProperties properties = new ThemeProperties();
if (propertiesFile.exists()) { if (propertiesFile.exists()) {

View File

@ -3,7 +3,7 @@ package cc.ryanc.halo.web.controller.admin.api;
import cc.ryanc.halo.model.enums.BlogProperties; import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.support.Theme; import cc.ryanc.halo.model.support.Theme;
import cc.ryanc.halo.service.OptionService; import cc.ryanc.halo.service.OptionService;
import cc.ryanc.halo.utils.ThemeUtils; import cc.ryanc.halo.service.ThemeService;
import cc.ryanc.halo.web.controller.content.base.BaseContentController; import cc.ryanc.halo.web.controller.content.base.BaseContentController;
import freemarker.template.Configuration; import freemarker.template.Configuration;
import freemarker.template.TemplateModelException; import freemarker.template.TemplateModelException;
@ -29,10 +29,14 @@ public class ThemeController {
private final Configuration configuration; private final Configuration configuration;
private final ThemeService themeService;
public ThemeController(OptionService optionService, public ThemeController(OptionService optionService,
Configuration configuration) { Configuration configuration,
ThemeService themeService) {
this.optionService = optionService; this.optionService = optionService;
this.configuration = configuration; this.configuration = configuration;
this.themeService = themeService;
} }
/** /**
@ -43,7 +47,7 @@ public class ThemeController {
@GetMapping @GetMapping
@ApiOperation("List all themes") @ApiOperation("List all themes")
public List<Theme> listAll() { public List<Theme> listAll() {
return ThemeUtils.getThemes(); return themeService.getThemes();
} }
/** /**