diff --git a/src/main/java/cc/ryanc/halo/model/support/ThemeFile.java b/src/main/java/cc/ryanc/halo/model/support/ThemeFile.java new file mode 100644 index 000000000..c986fdc21 --- /dev/null +++ b/src/main/java/cc/ryanc/halo/model/support/ThemeFile.java @@ -0,0 +1,21 @@ +package cc.ryanc.halo.model.support; + +import lombok.Data; +import lombok.ToString; + +import java.util.List; + +/** + * @author RYAN0UP + * @date 2019/04/02 + */ +@Data +@ToString +public class ThemeFile { + + private String name; + + private Boolean isFile; + + private List node; +} diff --git a/src/main/java/cc/ryanc/halo/service/ThemeService.java b/src/main/java/cc/ryanc/halo/service/ThemeService.java index aa3c84061..9a035c6ad 100644 --- a/src/main/java/cc/ryanc/halo/service/ThemeService.java +++ b/src/main/java/cc/ryanc/halo/service/ThemeService.java @@ -1,6 +1,7 @@ package cc.ryanc.halo.service; import cc.ryanc.halo.model.support.Theme; +import cc.ryanc.halo.model.support.ThemeFile; import cc.ryanc.halo.model.support.ThemeProperties; import java.io.File; @@ -20,12 +21,20 @@ public interface ThemeService { List getThemes(); /** - * Gets theme templates + * Lists theme folder by absolute path. + * + * @param absolutePath absolutePath + * @return List + */ + List listThemeFolder(String absolutePath); + + /** + * Lists theme folder by theme name. * * @param theme theme - * @return List + * @return List */ - List getTemplates(String theme); + List listThemeFolderBy(String theme); /** * Gets custom template, such as page_xxx.ftl, and xxx will be template name diff --git a/src/main/java/cc/ryanc/halo/service/impl/ThemeServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/ThemeServiceImpl.java index 8870e97b8..b453ee9f0 100644 --- a/src/main/java/cc/ryanc/halo/service/impl/ThemeServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/ThemeServiceImpl.java @@ -2,6 +2,7 @@ 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.ThemeFile; import cc.ryanc.halo.model.support.ThemeProperties; import cc.ryanc.halo.service.ThemeService; import cc.ryanc.halo.web.controller.content.base.BaseContentController; @@ -56,31 +57,29 @@ public class ThemeServiceImpl implements ThemeService { } /** - * Gets theme templates + * Lists theme folder by absolute path. * - * @param theme theme - * @return List + * @param absolutePath absolutePath + * @return List */ @Override - public List getTemplates(String theme) { - final List templates = new ArrayList<>(); + public List listThemeFolder(String absolutePath) { + final List templates = new ArrayList<>(); try { - final File themesPath = new File(getThemeBasePath(), theme); - final File modulePath = new File(themesPath.getAbsolutePath(), "module"); - final File[] baseFiles = themesPath.listFiles(); - final File[] moduleFiles = modulePath.listFiles(); - if (null != moduleFiles) { - for (File file : moduleFiles) { - if (file.isFile() && file.getName().endsWith(HaloConst.SUFFIX_FTL)) { - templates.add("module/" + file.getName()); - } - } - } + File absolutePathFile = new File(absolutePath); + File[] baseFiles = absolutePathFile.listFiles(); if (null != baseFiles) { - for (File file : baseFiles) { - if (file.isFile() && file.getName().endsWith(HaloConst.SUFFIX_FTL)) { - templates.add(file.getName()); + for (File base : baseFiles) { + ThemeFile file = new ThemeFile(); + if (base.isDirectory()) { + file.setName(base.getAbsolutePath()); + file.setIsFile(false); + file.setNode(listThemeFolder(base.getAbsolutePath())); + } else { + file.setName(base.getAbsolutePath()); + file.setIsFile(true); } + templates.add(file); } } } catch (Exception e) { @@ -89,6 +88,18 @@ public class ThemeServiceImpl implements ThemeService { return templates; } + /** + * Lists theme folder by theme name. + * + * @param theme theme + * @return List + */ + @Override + public List listThemeFolderBy(String theme) { + File themePath = new File(getThemeBasePath(), theme); + return listThemeFolder(themePath.getAbsolutePath()); + } + /** * Gets custom template, such as page_xxx.ftl, and xxx will be template name * 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 2d3e246bd..187c001f2 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 @@ -4,6 +4,7 @@ import cc.ryanc.halo.model.enums.OptionSource; import cc.ryanc.halo.model.properties.PrimaryProperties; import cc.ryanc.halo.model.properties.PropertyEnum; import cc.ryanc.halo.model.support.Theme; +import cc.ryanc.halo.model.support.ThemeFile; import cc.ryanc.halo.service.OptionService; import cc.ryanc.halo.service.ThemeService; import cc.ryanc.halo.web.controller.content.base.BaseContentController; @@ -52,6 +53,16 @@ public class ThemeController { return themeService.getThemes(); } + /** + * List all of theme files. + * + * @return List + */ + @GetMapping(value = "/files") + public List listFiles() { + return themeService.listThemeFolderBy(BaseContentController.THEME); + } + /** * Active theme *