Refactor ThemeUtils

pull/137/head
ruibaby 2019-03-26 14:34:55 +08:00
parent 88b6d55d23
commit 92e4804bf0
4 changed files with 43 additions and 113 deletions

View File

@ -2,6 +2,7 @@ package cc.ryanc.halo.config;
import cc.ryanc.halo.config.properties.HaloProperties;
import cc.ryanc.halo.factory.StringToEnumConverterFactory;
import cc.ryanc.halo.model.support.HaloConst;
import cc.ryanc.halo.security.resolver.AuthenticationArgumentResolver;
import cc.ryanc.halo.web.controller.support.PageJacksonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
@ -115,7 +116,7 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer {
resolver.setExposeRequestAttributes(false);
resolver.setExposeSessionAttributes(false);
resolver.setExposeSpringMacroHelpers(true);
resolver.setSuffix(".ftl");
resolver.setSuffix(HaloConst.SUFFIX_FTL);
resolver.setContentType("text/html; charset=UTF-8");
registry.viewResolver(resolver);
}

View File

@ -30,6 +30,11 @@ public class HaloConst {
*/
public static final String TOKEN_HEADER = "token";
/**
* Suffix of freemarker template file
*/
public static final String SUFFIX_FTL = ".ftl";
/**
* Owo map. (Unmodified map)
*/

View File

@ -25,15 +25,13 @@ public class Theme implements Serializable {
*/
private boolean hasOptions;
/**
* Is support update
*/
private boolean hasUpdate;
/**
* Is internal theme
*/
private boolean isInternal;
/**
* Theme properties
*/
private ThemeProperties properties;
}

View File

@ -8,7 +8,6 @@ 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.util.ResourceUtils;
import java.io.File;
import java.io.FileNotFoundException;
@ -19,63 +18,36 @@ import java.util.List;
* Theme utils
*
* @author : RYAN0UP
* @date : 2019/3/16
* @date : 2019/3/26
*/
@Slf4j
public class ThemeUtils {
/**
* Scan internal themes and user's themes
* Gets all themes
*
* @return List
* @return list of themes
*/
public static List<Theme> getThemes() {
final List<Theme> themes = new ArrayList<>();
final File[] files = getThemeBasePath().listFiles();
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) {
Theme theme;
for (File file : files) {
if (file.isDirectory()) {
if (StrUtil.equals("__MACOSX", file.getName())) {
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);
if (!file.isDirectory()) {
continue;
}
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) {
@ -85,36 +57,7 @@ public class ThemeUtils {
}
/**
* Get internal themes base path
*
* @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
* Gets theme templates
*
* @param theme theme
* @return List<String>
@ -122,20 +65,20 @@ public class ThemeUtils {
public static List<String> getTemplates(String theme) {
final List<String> templates = new ArrayList<>();
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[] baseFiles = themesPath.listFiles();
final File[] moduleFiles = modulePath.listFiles();
if (null != 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());
}
}
}
if (null != 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());
}
}
@ -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
* @return List
*/
public static List<String> getCustomTpl(String theme) throws FileNotFoundException {
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();
if (null != themeFiles && themeFiles.length > 0) {
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])) {
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
*/
public static boolean isTemplateExist(String template) throws FileNotFoundException {
boolean result = false;
StrBuilder templatePath = new StrBuilder(BaseContentController.THEME);
templatePath.append("/");
templatePath.append(template);
File file = new File(getThemesBasePath(BaseContentController.THEME), templatePath.toString());
if (file.exists()) {
result = true;
}
return result;
File file = new File(getThemeBasePath(), templatePath.toString());
return file.exists();
}
/**
@ -193,30 +132,17 @@ public class ThemeUtils {
* @throws FileNotFoundException FileNotFoundException
*/
public static boolean isThemeExist(String theme) throws FileNotFoundException {
boolean result = false;
File file = new File(getThemesBasePath(theme), theme);
if (file.exists()) {
result = true;
}
return result;
File file = new File(getThemeBasePath(), theme);
return file.exists();
}
/**
* Judging whether the theme is a internal theme or not
* Gets theme base path.
*
* @param themeName themeName
* @return boolean
* @return File
*/
public static boolean isInternal(String themeName) {
boolean result = false;
List<Theme> themes = HaloConst.THEMES;
for (Theme theme : themes) {
if (theme.getThemeDir().equals(themeName) && theme.isInternal()) {
result = true;
break;
}
}
return result;
private static File getThemeBasePath() {
return new File(System.getProperties().getProperty("user.home"), ".halo/templates/themes");
}
/**
@ -225,7 +151,7 @@ public class ThemeUtils {
* @param path path
* @return ThemeProperties
*/
public static ThemeProperties getProperties(File path) {
private static ThemeProperties getProperties(File path) {
File propertiesFile = new File(path, "theme.properties");
ThemeProperties properties = new ThemeProperties();
if (propertiesFile.exists()) {