diff --git a/src/main/java/cc/ryanc/halo/service/ThemeService.java b/src/main/java/cc/ryanc/halo/service/ThemeService.java new file mode 100644 index 000000000..aa3c84061 --- /dev/null +++ b/src/main/java/cc/ryanc/halo/service/ThemeService.java @@ -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 getThemes(); + + /** + * Gets theme templates + * + * @param theme theme + * @return List + */ + List getTemplates(String theme); + + /** + * Gets custom template, such as page_xxx.ftl, and xxx will be template name + * + * @param theme theme name + * @return List + */ + List 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); +} diff --git a/src/main/java/cc/ryanc/halo/utils/ThemeUtils.java b/src/main/java/cc/ryanc/halo/service/impl/ThemeServiceImpl.java similarity index 88% rename from src/main/java/cc/ryanc/halo/utils/ThemeUtils.java rename to src/main/java/cc/ryanc/halo/service/impl/ThemeServiceImpl.java index 745bdb5d6..9b4d79bc0 100644 --- a/src/main/java/cc/ryanc/halo/utils/ThemeUtils.java +++ b/src/main/java/cc/ryanc/halo/service/impl/ThemeServiceImpl.java @@ -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.Theme; import cc.ryanc.halo.model.support.ThemeProperties; +import cc.ryanc.halo.service.ThemeService; import cc.ryanc.halo.web.controller.content.base.BaseContentController; import cn.hutool.core.text.StrBuilder; import cn.hutool.core.util.StrUtil; import cn.hutool.setting.dialect.Props; -import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; import java.io.File; -import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; /** - * Theme utils - * * @author : RYAN0UP * @date : 2019/3/26 */ -@Slf4j -public class ThemeUtils { +@Service +public class ThemeServiceImpl implements ThemeService { /** * Gets all themes * * @return list of themes */ - public static List getThemes() { + @Override + public List getThemes() { final List themes = new ArrayList<>(); final File[] files = getThemeBasePath().listFiles(); try { @@ -62,7 +61,8 @@ public class ThemeUtils { * @param theme theme * @return List */ - public static List getTemplates(String theme) { + @Override + public List getTemplates(String theme) { final List templates = new ArrayList<>(); try { final File themesPath = new File(getThemeBasePath(), theme); @@ -95,7 +95,8 @@ public class ThemeUtils { * @param theme theme name * @return List */ - public static List getCustomTpl(String theme) throws FileNotFoundException { + @Override + public List getCustomTpl(String theme) { final List templates = new ArrayList<>(); final File themePath = new File(getThemeBasePath(), theme); final File[] themeFiles = themePath.listFiles(); @@ -116,7 +117,8 @@ public class ThemeUtils { * @param template template * @return boolean */ - public static boolean isTemplateExist(String template) throws FileNotFoundException { + @Override + public boolean isTemplateExist(String template) { StrBuilder templatePath = new StrBuilder(BaseContentController.THEME); templatePath.append("/"); templatePath.append(template); @@ -129,9 +131,9 @@ public class ThemeUtils { * * @param theme theme name * @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); return file.exists(); } @@ -141,7 +143,8 @@ public class ThemeUtils { * * @return File */ - private static File getThemeBasePath() { + @Override + public File getThemeBasePath() { return new File(System.getProperties().getProperty("user.home"), ".halo/templates/themes"); } @@ -151,7 +154,8 @@ public class ThemeUtils { * @param path path * @return ThemeProperties */ - private static ThemeProperties getProperties(File path) { + @Override + public ThemeProperties getProperties(File path) { File propertiesFile = new File(path, "theme.properties"); ThemeProperties properties = new ThemeProperties(); if (propertiesFile.exists()) { diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/api/ThemeController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/api/ThemeController.java index d7d727fc6..40c4874cb 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/api/ThemeController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/api/ThemeController.java @@ -3,7 +3,7 @@ package cc.ryanc.halo.web.controller.admin.api; import cc.ryanc.halo.model.enums.BlogProperties; import cc.ryanc.halo.model.support.Theme; 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 freemarker.template.Configuration; import freemarker.template.TemplateModelException; @@ -29,10 +29,14 @@ public class ThemeController { private final Configuration configuration; + private final ThemeService themeService; + public ThemeController(OptionService optionService, - Configuration configuration) { + Configuration configuration, + ThemeService themeService) { this.optionService = optionService; this.configuration = configuration; + this.themeService = themeService; } /** @@ -43,7 +47,7 @@ public class ThemeController { @GetMapping @ApiOperation("List all themes") public List listAll() { - return ThemeUtils.getThemes(); + return themeService.getThemes(); } /**