mirror of https://github.com/halo-dev/halo
Refactor ThemeUtils
parent
88b6d55d23
commit
92e4804bf0
|
@ -2,6 +2,7 @@ package cc.ryanc.halo.config;
|
||||||
|
|
||||||
import cc.ryanc.halo.config.properties.HaloProperties;
|
import cc.ryanc.halo.config.properties.HaloProperties;
|
||||||
import cc.ryanc.halo.factory.StringToEnumConverterFactory;
|
import cc.ryanc.halo.factory.StringToEnumConverterFactory;
|
||||||
|
import cc.ryanc.halo.model.support.HaloConst;
|
||||||
import cc.ryanc.halo.security.resolver.AuthenticationArgumentResolver;
|
import cc.ryanc.halo.security.resolver.AuthenticationArgumentResolver;
|
||||||
import cc.ryanc.halo.web.controller.support.PageJacksonSerializer;
|
import cc.ryanc.halo.web.controller.support.PageJacksonSerializer;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
@ -115,7 +116,7 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer {
|
||||||
resolver.setExposeRequestAttributes(false);
|
resolver.setExposeRequestAttributes(false);
|
||||||
resolver.setExposeSessionAttributes(false);
|
resolver.setExposeSessionAttributes(false);
|
||||||
resolver.setExposeSpringMacroHelpers(true);
|
resolver.setExposeSpringMacroHelpers(true);
|
||||||
resolver.setSuffix(".ftl");
|
resolver.setSuffix(HaloConst.SUFFIX_FTL);
|
||||||
resolver.setContentType("text/html; charset=UTF-8");
|
resolver.setContentType("text/html; charset=UTF-8");
|
||||||
registry.viewResolver(resolver);
|
registry.viewResolver(resolver);
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,11 @@ public class HaloConst {
|
||||||
*/
|
*/
|
||||||
public static final String TOKEN_HEADER = "token";
|
public static final String TOKEN_HEADER = "token";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Suffix of freemarker template file
|
||||||
|
*/
|
||||||
|
public static final String SUFFIX_FTL = ".ftl";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Owo map. (Unmodified map)
|
* Owo map. (Unmodified map)
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -25,15 +25,13 @@ public class Theme implements Serializable {
|
||||||
*/
|
*/
|
||||||
private boolean hasOptions;
|
private boolean hasOptions;
|
||||||
|
|
||||||
/**
|
|
||||||
* Is support update
|
|
||||||
*/
|
|
||||||
private boolean hasUpdate;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is internal theme
|
* Is internal theme
|
||||||
*/
|
*/
|
||||||
private boolean isInternal;
|
private boolean isInternal;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Theme properties
|
||||||
|
*/
|
||||||
private ThemeProperties properties;
|
private ThemeProperties properties;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,6 @@ 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 lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.util.ResourceUtils;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
|
@ -19,63 +18,36 @@ import java.util.List;
|
||||||
* Theme utils
|
* Theme utils
|
||||||
*
|
*
|
||||||
* @author : RYAN0UP
|
* @author : RYAN0UP
|
||||||
* @date : 2019/3/16
|
* @date : 2019/3/26
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class ThemeUtils {
|
public class ThemeUtils {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scan internal themes and user's themes
|
* Gets all themes
|
||||||
*
|
*
|
||||||
* @return List
|
* @return list of themes
|
||||||
*/
|
*/
|
||||||
public static List<Theme> getThemes() {
|
public static List<Theme> getThemes() {
|
||||||
final List<Theme> themes = new ArrayList<>();
|
final List<Theme> themes = new ArrayList<>();
|
||||||
|
final File[] files = getThemeBasePath().listFiles();
|
||||||
try {
|
try {
|
||||||
themes.addAll(getThemesByPath(getInternalThemesBasePath(), true));
|
|
||||||
themes.addAll(getThemesByPath(getUsersThemesBasePath(), false));
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException("Themes scan failed", e);
|
|
||||||
}
|
|
||||||
return themes;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Scan themes by directory
|
|
||||||
*
|
|
||||||
* @param themesPath themes base Path
|
|
||||||
* @param isInternal isInternal
|
|
||||||
* @return themes
|
|
||||||
*/
|
|
||||||
private static List<Theme> getThemesByPath(File themesBasePath, boolean isInternal) {
|
|
||||||
final List<Theme> themes = new ArrayList<>();
|
|
||||||
try {
|
|
||||||
final File[] files = themesBasePath.listFiles();
|
|
||||||
if (null != files) {
|
if (null != files) {
|
||||||
Theme theme;
|
Theme theme;
|
||||||
for (File file : files) {
|
for (File file : files) {
|
||||||
if (file.isDirectory()) {
|
if (!file.isDirectory()) {
|
||||||
if (StrUtil.equals("__MACOSX", file.getName())) {
|
continue;
|
||||||
continue;
|
|
||||||
}
|
|
||||||
theme = new Theme();
|
|
||||||
theme.setThemeDir(file.getName());
|
|
||||||
File optionsPath = new File(themesBasePath.getAbsolutePath(),
|
|
||||||
file.getName() + "/module/options.ftl");
|
|
||||||
if (optionsPath.exists()) {
|
|
||||||
theme.setHasOptions(true);
|
|
||||||
} else {
|
|
||||||
theme.setHasOptions(false);
|
|
||||||
}
|
|
||||||
File gitPath = new File(themesBasePath.getAbsolutePath(), file.getName() + "/.git");
|
|
||||||
if (gitPath.exists()) {
|
|
||||||
theme.setHasUpdate(true);
|
|
||||||
} else {
|
|
||||||
theme.setHasUpdate(false);
|
|
||||||
}
|
|
||||||
theme.setProperties(getProperties(new File(themesBasePath,file.getName())));
|
|
||||||
themes.add(theme);
|
|
||||||
}
|
}
|
||||||
|
theme = new Theme();
|
||||||
|
theme.setThemeDir(file.getName());
|
||||||
|
File optionsPath = new File(getThemeBasePath().getAbsolutePath(), file.getName() + "/module/options.ftl");
|
||||||
|
if (optionsPath.exists()) {
|
||||||
|
theme.setHasOptions(true);
|
||||||
|
} else {
|
||||||
|
theme.setHasOptions(false);
|
||||||
|
}
|
||||||
|
theme.setProperties(getProperties(new File(getThemeBasePath(), file.getName())));
|
||||||
|
themes.add(theme);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -85,36 +57,7 @@ public class ThemeUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get internal themes base path
|
* Gets theme templates
|
||||||
*
|
|
||||||
* @return File
|
|
||||||
* @throws FileNotFoundException FileNotFoundException
|
|
||||||
*/
|
|
||||||
public static File getInternalThemesBasePath() throws FileNotFoundException {
|
|
||||||
return new File(ResourceUtils.getURL("classpath:").getPath(), "templates/themes");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get user's themes base path
|
|
||||||
*
|
|
||||||
* @return File
|
|
||||||
*/
|
|
||||||
public static File getUsersThemesBasePath() {
|
|
||||||
return new File(System.getProperties().getProperty("user.home"), "halo/templates/themes");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get themes path by theme name
|
|
||||||
*
|
|
||||||
* @param themeName themeName
|
|
||||||
* @return File
|
|
||||||
*/
|
|
||||||
public static File getThemesBasePath(String themeName) throws FileNotFoundException {
|
|
||||||
return isInternal(themeName) ? getInternalThemesBasePath() : getUsersThemesBasePath();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get theme templates
|
|
||||||
*
|
*
|
||||||
* @param theme theme
|
* @param theme theme
|
||||||
* @return List<String>
|
* @return List<String>
|
||||||
|
@ -122,20 +65,20 @@ public class ThemeUtils {
|
||||||
public static List<String> getTemplates(String theme) {
|
public static List<String> getTemplates(String theme) {
|
||||||
final List<String> templates = new ArrayList<>();
|
final List<String> templates = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
final File themesPath = new File(getThemesBasePath(theme), theme);
|
final File themesPath = new File(getThemeBasePath(), theme);
|
||||||
final File modulePath = new File(themesPath.getAbsolutePath(), "module");
|
final File modulePath = new File(themesPath.getAbsolutePath(), "module");
|
||||||
final File[] baseFiles = themesPath.listFiles();
|
final File[] baseFiles = themesPath.listFiles();
|
||||||
final File[] moduleFiles = modulePath.listFiles();
|
final File[] moduleFiles = modulePath.listFiles();
|
||||||
if (null != moduleFiles) {
|
if (null != moduleFiles) {
|
||||||
for (File file : moduleFiles) {
|
for (File file : moduleFiles) {
|
||||||
if (file.isFile() && file.getName().endsWith(".ftl")) {
|
if (file.isFile() && file.getName().endsWith(HaloConst.SUFFIX_FTL)) {
|
||||||
templates.add("module/" + file.getName());
|
templates.add("module/" + file.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (null != baseFiles) {
|
if (null != baseFiles) {
|
||||||
for (File file : baseFiles) {
|
for (File file : baseFiles) {
|
||||||
if (file.isFile() && file.getName().endsWith(".ftl")) {
|
if (file.isFile() && file.getName().endsWith(HaloConst.SUFFIX_FTL)) {
|
||||||
templates.add(file.getName());
|
templates.add(file.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -147,20 +90,20 @@ public class ThemeUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get 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
|
||||||
*
|
*
|
||||||
* @param theme theme name
|
* @param theme theme name
|
||||||
* @return List
|
* @return List
|
||||||
*/
|
*/
|
||||||
public static List<String> getCustomTpl(String theme) throws FileNotFoundException {
|
public static List<String> getCustomTpl(String theme) throws FileNotFoundException {
|
||||||
final List<String> templates = new ArrayList<>();
|
final List<String> templates = new ArrayList<>();
|
||||||
final File themePath = new File(getThemesBasePath(theme), theme);
|
final File themePath = new File(getThemeBasePath(), theme);
|
||||||
final File[] themeFiles = themePath.listFiles();
|
final File[] themeFiles = themePath.listFiles();
|
||||||
if (null != themeFiles && themeFiles.length > 0) {
|
if (null != themeFiles && themeFiles.length > 0) {
|
||||||
for (File file : themeFiles) {
|
for (File file : themeFiles) {
|
||||||
String[] split = StrUtil.removeSuffix(file.getName(), ".ftl").split("_");
|
String[] split = StrUtil.removeSuffix(file.getName(), HaloConst.SUFFIX_FTL).split("_");
|
||||||
if (split.length == 2 && "page".equals(split[0])) {
|
if (split.length == 2 && "page".equals(split[0])) {
|
||||||
templates.add(StrUtil.removeSuffix(file.getName(), ".ftl"));
|
templates.add(StrUtil.removeSuffix(file.getName(), HaloConst.SUFFIX_FTL));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -174,15 +117,11 @@ public class ThemeUtils {
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public static boolean isTemplateExist(String template) throws FileNotFoundException {
|
public static boolean isTemplateExist(String template) throws FileNotFoundException {
|
||||||
boolean result = false;
|
|
||||||
StrBuilder templatePath = new StrBuilder(BaseContentController.THEME);
|
StrBuilder templatePath = new StrBuilder(BaseContentController.THEME);
|
||||||
templatePath.append("/");
|
templatePath.append("/");
|
||||||
templatePath.append(template);
|
templatePath.append(template);
|
||||||
File file = new File(getThemesBasePath(BaseContentController.THEME), templatePath.toString());
|
File file = new File(getThemeBasePath(), templatePath.toString());
|
||||||
if (file.exists()) {
|
return file.exists();
|
||||||
result = true;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -193,30 +132,17 @@ public class ThemeUtils {
|
||||||
* @throws FileNotFoundException FileNotFoundException
|
* @throws FileNotFoundException FileNotFoundException
|
||||||
*/
|
*/
|
||||||
public static boolean isThemeExist(String theme) throws FileNotFoundException {
|
public static boolean isThemeExist(String theme) throws FileNotFoundException {
|
||||||
boolean result = false;
|
File file = new File(getThemeBasePath(), theme);
|
||||||
File file = new File(getThemesBasePath(theme), theme);
|
return file.exists();
|
||||||
if (file.exists()) {
|
|
||||||
result = true;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Judging whether the theme is a internal theme or not
|
* Gets theme base path.
|
||||||
*
|
*
|
||||||
* @param themeName themeName
|
* @return File
|
||||||
* @return boolean
|
|
||||||
*/
|
*/
|
||||||
public static boolean isInternal(String themeName) {
|
private static File getThemeBasePath() {
|
||||||
boolean result = false;
|
return new File(System.getProperties().getProperty("user.home"), ".halo/templates/themes");
|
||||||
List<Theme> themes = HaloConst.THEMES;
|
|
||||||
for (Theme theme : themes) {
|
|
||||||
if (theme.getThemeDir().equals(themeName) && theme.isInternal()) {
|
|
||||||
result = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -225,7 +151,7 @@ public class ThemeUtils {
|
||||||
* @param path path
|
* @param path path
|
||||||
* @return ThemeProperties
|
* @return ThemeProperties
|
||||||
*/
|
*/
|
||||||
public static ThemeProperties getProperties(File path) {
|
private static 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()) {
|
||||||
|
|
Loading…
Reference in New Issue