Refactor theme module

pull/137/head
ruibaby 2019-03-22 18:51:07 +08:00
parent 1018481bd9
commit 1ddb1043d4
8 changed files with 92 additions and 24 deletions

View File

@ -157,6 +157,11 @@
<artifactId>hutool-extra</artifactId> <artifactId>hutool-extra</artifactId>
<version>${hutool.version}</version> <version>${hutool.version}</version>
</dependency> </dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-setting</artifactId>
<version>${hutool.version}</version>
</dependency>
<!-- Upyun SDK --> <!-- Upyun SDK -->
<dependency> <dependency>

View File

@ -16,9 +16,9 @@ public class Theme implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** /**
* Theme name * Theme dir
*/ */
private String themeName; private String themeDir;
/** /**
* Is support setting options * Is support setting options
@ -34,4 +34,6 @@ public class Theme implements Serializable {
* Is internal theme * Is internal theme
*/ */
private boolean isInternal; private boolean isInternal;
private ThemeProperties properties;
} }

View File

@ -0,0 +1,27 @@
package cc.ryanc.halo.model.support;
import lombok.Data;
/**
* @author : RYAN0UP
* @date : 2019-03-22
*/
@Data
public class ThemeProperties {
private String id;
private String name;
private String website;
private String description;
private String logo;
private String version;
private String author;
private String authorWebsite;
}

View File

@ -2,9 +2,11 @@ package cc.ryanc.halo.utils;
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.ThemeProperties;
import cc.ryanc.halo.web.controller.content.base.BaseContentController; import cc.ryanc.halo.web.controller.content.base.BaseContentController;
import cn.hutool.core.text.StrBuilder; 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 lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
@ -30,8 +32,8 @@ public class ThemeUtils {
public static List<Theme> getThemes() { public static List<Theme> getThemes() {
final List<Theme> themes = new ArrayList<>(); final List<Theme> themes = new ArrayList<>();
try { try {
themes.addAll(getThemesByPath(getInternalThemesPath(), true)); themes.addAll(getThemesByPath(getInternalThemesBasePath(), true));
themes.addAll(getThemesByPath(getUsersThemesPath(), false)); themes.addAll(getThemesByPath(getUsersThemesBasePath(), false));
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Themes scan failed", e); throw new RuntimeException("Themes scan failed", e);
} }
@ -41,14 +43,14 @@ public class ThemeUtils {
/** /**
* Scan themes by directory * Scan themes by directory
* *
* @param themesPath themes Path * @param themesPath themes base Path
* @param isInternal isInternal * @param isInternal isInternal
* @return themes * @return themes
*/ */
private static List<Theme> getThemesByPath(File themesPath, boolean isInternal) { private static List<Theme> getThemesByPath(File themesBasePath, boolean isInternal) {
final List<Theme> themes = new ArrayList<>(); final List<Theme> themes = new ArrayList<>();
try { try {
final File[] files = themesPath.listFiles(); final File[] files = themesBasePath.listFiles();
if (null != files) { if (null != files) {
Theme theme; Theme theme;
for (File file : files) { for (File file : files) {
@ -57,20 +59,21 @@ public class ThemeUtils {
continue; continue;
} }
theme = new Theme(); theme = new Theme();
theme.setThemeName(file.getName()); theme.setThemeDir(file.getName());
File optionsPath = new File(themesPath.getAbsolutePath(), File optionsPath = new File(themesBasePath.getAbsolutePath(),
file.getName() + "/module/options.ftl"); file.getName() + "/module/options.ftl");
if (optionsPath.exists()) { if (optionsPath.exists()) {
theme.setHasOptions(true); theme.setHasOptions(true);
} else { } else {
theme.setHasOptions(false); theme.setHasOptions(false);
} }
File gitPath = new File(themesPath.getAbsolutePath(), file.getName() + "/.git"); File gitPath = new File(themesBasePath.getAbsolutePath(), file.getName() + "/.git");
if (gitPath.exists()) { if (gitPath.exists()) {
theme.setHasUpdate(true); theme.setHasUpdate(true);
} else { } else {
theme.setHasUpdate(false); theme.setHasUpdate(false);
} }
theme.setProperties(getProperties(new File(themesBasePath,file.getName())));
theme.setInternal(isInternal); theme.setInternal(isInternal);
themes.add(theme); themes.add(theme);
} }
@ -83,21 +86,21 @@ public class ThemeUtils {
} }
/** /**
* Get internal themes path * Get internal themes base path
* *
* @return File * @return File
* @throws FileNotFoundException FileNotFoundException * @throws FileNotFoundException FileNotFoundException
*/ */
public static File getInternalThemesPath() throws FileNotFoundException { public static File getInternalThemesBasePath() throws FileNotFoundException {
return new File(ResourceUtils.getURL("classpath:").getPath(), "templates/themes"); return new File(ResourceUtils.getURL("classpath:").getPath(), "templates/themes");
} }
/** /**
* Get user's themes path * Get user's themes base path
* *
* @return File * @return File
*/ */
public static File getUsersThemesPath() { public static File getUsersThemesBasePath() {
return new File(System.getProperties().getProperty("user.home"), "halo/templates/themes"); return new File(System.getProperties().getProperty("user.home"), "halo/templates/themes");
} }
@ -107,8 +110,8 @@ public class ThemeUtils {
* @param themeName themeName * @param themeName themeName
* @return File * @return File
*/ */
public static File getThemesPath(String themeName) throws FileNotFoundException { public static File getThemesBasePath(String themeName) throws FileNotFoundException {
return isInternal(themeName) ? getInternalThemesPath() : getUsersThemesPath(); return isInternal(themeName) ? getInternalThemesBasePath() : getUsersThemesBasePath();
} }
/** /**
@ -120,7 +123,7 @@ 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(getThemesPath(theme), theme); final File themesPath = new File(getThemesBasePath(theme), 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();
@ -152,7 +155,7 @@ public class ThemeUtils {
*/ */
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(getThemesPath(theme), theme); final File themePath = new File(getThemesBasePath(theme), 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) {
@ -165,7 +168,6 @@ public class ThemeUtils {
return templates; return templates;
} }
/** /**
* Judging whether template exists under the specified theme * Judging whether template exists under the specified theme
* *
@ -177,7 +179,7 @@ public class ThemeUtils {
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(getThemesPath(BaseContentController.THEME), templatePath.toString()); File file = new File(getThemesBasePath(BaseContentController.THEME), templatePath.toString());
if (file.exists()) { if (file.exists()) {
result = true; result = true;
} }
@ -193,7 +195,7 @@ public class ThemeUtils {
*/ */
public static boolean isThemeExist(String theme) throws FileNotFoundException { public static boolean isThemeExist(String theme) throws FileNotFoundException {
boolean result = false; boolean result = false;
File file = new File(getThemesPath(theme), theme); File file = new File(getThemesBasePath(theme), theme);
if (file.exists()) { if (file.exists()) {
result = true; result = true;
} }
@ -210,11 +212,34 @@ public class ThemeUtils {
boolean result = false; boolean result = false;
List<Theme> themes = HaloConst.THEMES; List<Theme> themes = HaloConst.THEMES;
for (Theme theme : themes) { for (Theme theme : themes) {
if (theme.getThemeName().equals(themeName) && theme.isInternal()) { if (theme.getThemeDir().equals(themeName) && theme.isInternal()) {
result = true; result = true;
break; break;
} }
} }
return result; return result;
} }
/**
* Get theme Properties.
*
* @param path path
* @return ThemeProperties
*/
public static ThemeProperties getProperties(File path) {
File propertiesFile = new File(path, "theme.properties");
ThemeProperties properties = new ThemeProperties();
if (propertiesFile.exists()) {
Props props = new Props(propertiesFile);
properties.setId(props.getStr("theme.id"));
properties.setName(props.getStr("theme.name"));
properties.setWebsite(props.getStr("theme.website"));
properties.setDescription(props.getStr("theme.description"));
properties.setLogo(props.getStr("theme.logo"));
properties.setVersion(props.getStr("theme.version"));
properties.setAuthor(props.getStr("theme.author"));
properties.setAuthorWebsite(props.getStr("theme.author.website"));
}
return properties;
}
} }

View File

@ -75,7 +75,7 @@ public class ContentFeedController {
@GetMapping(value = {"atom", "atom.xml"}, produces = "application/xml;charset=UTF-8") @GetMapping(value = {"atom", "atom.xml"}, produces = "application/xml;charset=UTF-8")
@ResponseBody @ResponseBody
public String atom(Model model) throws IOException, TemplateException { public String atom(Model model) throws IOException, TemplateException {
int pageSize = HaloUtils.getDefaultPageSize(); int pageSize = HaloUtils.getDefaultPageSize(10);
final Sort sort = new Sort(Sort.Direction.DESC, "createTime"); final Sort sort = new Sort(Sort.Direction.DESC, "createTime");
final Pageable pageable = PageRequest.of(0, pageSize, sort); final Pageable pageable = PageRequest.of(0, pageSize, sort);
model.addAttribute("posts", buildPosts(pageable)); model.addAttribute("posts", buildPosts(pageable));

View File

@ -65,7 +65,7 @@ public class ContentIndexController extends BaseContentController {
@SortDefault(sort = "createTime", direction = DESC) @SortDefault(sort = "createTime", direction = DESC)
}) Sort sort) { }) Sort sort) {
log.debug("Requested index page, sort info: [{}]", sort); log.debug("Requested index page, sort info: [{}]", sort);
int pageSize = HaloUtils.getDefaultPageSize(); int pageSize = HaloUtils.getDefaultPageSize(10);
Pageable pageable = PageRequest.of(page - 1, pageSize, sort); Pageable pageable = PageRequest.of(page - 1, pageSize, sort);
Page<PostListVO> posts = postService.pageListVoBy(PostStatus.PUBLISHED, PostType.POST, pageable); Page<PostListVO> posts = postService.pageListVoBy(PostStatus.PUBLISHED, PostType.POST, pageable);
int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3); int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);

View File

@ -0,0 +1,9 @@
theme.id=anatole
theme.name=Anatole
theme.website=https://github.com/hi-caicai/farbox-theme-Anatole
theme.description=A other farbox theme
theme.logo=https://ryanc.cc/anatole/source/images/logo@2x.png
theme.version=1.0
theme.author=Caicai
theme.author.website=https://www.caicai.me/