Create api of theme files.

pull/137/head
ruibaby 2019-04-02 16:57:08 +08:00
parent ceb8887454
commit 44163519c3
4 changed files with 74 additions and 22 deletions

View File

@ -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<ThemeFile> node;
}

View File

@ -1,6 +1,7 @@
package cc.ryanc.halo.service; package cc.ryanc.halo.service;
import cc.ryanc.halo.model.support.Theme; 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.model.support.ThemeProperties;
import java.io.File; import java.io.File;
@ -20,12 +21,20 @@ public interface ThemeService {
List<Theme> getThemes(); List<Theme> getThemes();
/** /**
* Gets theme templates * Lists theme folder by absolute path.
*
* @param absolutePath absolutePath
* @return List<ThemeFile>
*/
List<ThemeFile> listThemeFolder(String absolutePath);
/**
* Lists theme folder by theme name.
* *
* @param theme theme * @param theme theme
* @return List<String> * @return List<ThemeFile>
*/ */
List<String> getTemplates(String theme); List<ThemeFile> listThemeFolderBy(String theme);
/** /**
* Gets custom template, such as page_xxx.ftl, and xxx will be template name * Gets custom template, such as page_xxx.ftl, and xxx will be template name

View File

@ -2,6 +2,7 @@ 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.ThemeFile;
import cc.ryanc.halo.model.support.ThemeProperties; import cc.ryanc.halo.model.support.ThemeProperties;
import cc.ryanc.halo.service.ThemeService; import cc.ryanc.halo.service.ThemeService;
import cc.ryanc.halo.web.controller.content.base.BaseContentController; 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 * @param absolutePath absolutePath
* @return List<String> * @return List<ThemeFile>
*/ */
@Override @Override
public List<String> getTemplates(String theme) { public List<ThemeFile> listThemeFolder(String absolutePath) {
final List<String> templates = new ArrayList<>(); final List<ThemeFile> templates = new ArrayList<>();
try { try {
final File themesPath = new File(getThemeBasePath(), theme); File absolutePathFile = new File(absolutePath);
final File modulePath = new File(themesPath.getAbsolutePath(), "module"); File[] baseFiles = absolutePathFile.listFiles();
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());
}
}
}
if (null != baseFiles) { if (null != baseFiles) {
for (File file : baseFiles) { for (File base : baseFiles) {
if (file.isFile() && file.getName().endsWith(HaloConst.SUFFIX_FTL)) { ThemeFile file = new ThemeFile();
templates.add(file.getName()); 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) { } catch (Exception e) {
@ -89,6 +88,18 @@ public class ThemeServiceImpl implements ThemeService {
return templates; return templates;
} }
/**
* Lists theme folder by theme name.
*
* @param theme theme
* @return List<ThemeFile>
*/
@Override
public List<ThemeFile> 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 * Gets custom template, such as page_xxx.ftl, and xxx will be template name
* *

View File

@ -4,6 +4,7 @@ import cc.ryanc.halo.model.enums.OptionSource;
import cc.ryanc.halo.model.properties.PrimaryProperties; import cc.ryanc.halo.model.properties.PrimaryProperties;
import cc.ryanc.halo.model.properties.PropertyEnum; import cc.ryanc.halo.model.properties.PropertyEnum;
import cc.ryanc.halo.model.support.Theme; 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.OptionService;
import cc.ryanc.halo.service.ThemeService; import cc.ryanc.halo.service.ThemeService;
import cc.ryanc.halo.web.controller.content.base.BaseContentController; import cc.ryanc.halo.web.controller.content.base.BaseContentController;
@ -52,6 +53,16 @@ public class ThemeController {
return themeService.getThemes(); return themeService.getThemes();
} }
/**
* List all of theme files.
*
* @return List<ThemeFile>
*/
@GetMapping(value = "/files")
public List<ThemeFile> listFiles() {
return themeService.listThemeFolderBy(BaseContentController.THEME);
}
/** /**
* Active theme * Active theme
* *