diff --git a/src/main/java/cc/ryanc/halo/model/support/Theme.java b/src/main/java/cc/ryanc/halo/model/support/Theme.java index 782be3561..508430b30 100644 --- a/src/main/java/cc/ryanc/halo/model/support/Theme.java +++ b/src/main/java/cc/ryanc/halo/model/support/Theme.java @@ -5,9 +5,7 @@ import lombok.Data; import java.io.Serializable; /** - *
- *     主题信息
- * 
+ * Theme DTO * * @author : RYAN0UP * @date : 2018/1/3 @@ -18,17 +16,22 @@ public class Theme implements Serializable { private static final long serialVersionUID = 1L; /** - * 主题名称 + * theme name */ private String themeName; /** - * 是否支持设置 + * is support setting options */ private boolean hasOptions; /** - * 是否支持更新 + * is support update */ private boolean hasUpdate; + + /** + * is internal theme + */ + private boolean isInternal; } diff --git a/src/main/java/cc/ryanc/halo/utils/ThemeUtils.java b/src/main/java/cc/ryanc/halo/utils/ThemeUtils.java index da3ecb7e4..f5021298f 100644 --- a/src/main/java/cc/ryanc/halo/utils/ThemeUtils.java +++ b/src/main/java/cc/ryanc/halo/utils/ThemeUtils.java @@ -1,6 +1,9 @@ package cc.ryanc.halo.utils; +import cc.ryanc.halo.model.support.HaloConst; import cc.ryanc.halo.model.support.Theme; +import cc.ryanc.halo.web.controller.core.BaseContentController; +import cn.hutool.core.text.StrBuilder; import cn.hutool.core.util.StrUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.util.ResourceUtils; @@ -27,8 +30,8 @@ public class ThemeUtils { public static List getThemes() { final List themes = new ArrayList<>(); try { - themes.addAll(getThemesByPath(getInternalThemesPath())); - themes.addAll(getThemesByPath(getUsersThemesPath())); + themes.addAll(getThemesByPath(getInternalThemesPath(), true)); + themes.addAll(getThemesByPath(getUsersThemesPath(), false)); } catch (Exception e) { log.error("Themes scan failed", e); } @@ -41,7 +44,7 @@ public class ThemeUtils { * @param file file * @return List */ - private static List getThemesByPath(File themesPath) { + private static List getThemesByPath(File themesPath, boolean isInternal) { final List themes = new ArrayList<>(); try { final File[] files = themesPath.listFiles(); @@ -67,6 +70,7 @@ public class ThemeUtils { } else { theme.setHasUpdate(false); } + theme.setInternal(isInternal); themes.add(theme); } } @@ -78,7 +82,7 @@ public class ThemeUtils { } /** - * Get internal themes + * Get internal themes path * * @return File * @throws FileNotFoundException FileNotFoundException @@ -88,7 +92,7 @@ public class ThemeUtils { } /** - * Get user's themes + * Get user's themes path * * @return File */ @@ -96,6 +100,20 @@ public class ThemeUtils { 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 getThemesPath(String themeName) throws FileNotFoundException { + if (isInternal(themeName)) { + return getInternalThemesPath(); + } else { + return getUsersThemesPath(); + } + } + /** * Get theme templates * @@ -105,7 +123,7 @@ public class ThemeUtils { public static List getTplName(String theme) { final List templates = new ArrayList<>(); try { - final File themesPath = new File(getUsersThemesPath(), "templates/themes/" + theme); + final File themesPath = new File(getThemesPath(theme), theme); final File modulePath = new File(themesPath.getAbsolutePath(), "module"); final File[] baseFiles = themesPath.listFiles(); final File[] moduleFiles = modulePath.listFiles(); @@ -134,9 +152,9 @@ public class ThemeUtils { * * @return List */ - public static List getCustomTpl(String theme) { + public static List getCustomTpl(String theme) throws FileNotFoundException { final List templates = new ArrayList<>(); - final File themePath = new File(getUsersThemesPath(), "templates/themes/" + theme); + final File themePath = new File(getThemesPath(theme), theme); final File[] themeFiles = themePath.listFiles(); if (null != themeFiles && themeFiles.length > 0) { for (File file : themeFiles) { @@ -148,4 +166,41 @@ public class ThemeUtils { } return templates; } + + + /** + * Judging whether template exists under the specified theme + * + * @param template template + * @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(getThemesPath(BaseContentController.THEME), templatePath.toString()); + if (file.exists()) { + result = true; + } + return result; + } + + /** + * Judging whether the theme is a internal theme or not + * + * @param themeName themeName + * @return boolean + */ + public static boolean isInternal(String themeName) { + boolean result = false; + List themes = HaloConst.THEMES; + for (Theme theme : themes) { + if (theme.getThemeName().equals(themeName) && theme.isInternal()) { + result = true; + break; + } + } + return result; + } } diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/ThemeController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/ThemeController.java index d9d6de1d4..2e5500633 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/ThemeController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/ThemeController.java @@ -129,7 +129,7 @@ public class ThemeController extends BaseController { @GetMapping(value = "/remove") public String removeTheme(@RequestParam("themeName") String themeName) { try { - final File themePath = new File(ThemeUtils.getUsersThemesPath(), themeName); + final File themePath = new File(ThemeUtils.getThemesPath(themeName), themeName); FileUtil.del(themePath); } catch (Exception e) { log.error("Delete theme failed: {}", e.getMessage()); @@ -244,7 +244,7 @@ public class ThemeController extends BaseController { public String getTplContent(@RequestParam("tplName") String tplName) { String tplContent = ""; try { - final StrBuilder themePath = new StrBuilder(ThemeUtils.getUsersThemesPath().getAbsolutePath()); + final StrBuilder themePath = new StrBuilder(ThemeUtils.getThemesPath(BaseContentController.THEME).getAbsolutePath()); themePath.append(BaseContentController.THEME); themePath.append("/"); themePath.append(tplName); @@ -272,7 +272,7 @@ public class ThemeController extends BaseController { return new JsonResult(0, localeMessage("code.admin.theme.edit.no-content")); } try { - final StrBuilder themePath = new StrBuilder(ThemeUtils.getUsersThemesPath().getAbsolutePath()); + final StrBuilder themePath = new StrBuilder(ThemeUtils.getThemesPath(BaseContentController.THEME).getAbsolutePath()); themePath.append(BaseContentController.THEME); themePath.append("/"); themePath.append(tplName); diff --git a/src/main/java/cc/ryanc/halo/web/controller/core/CommonController.java b/src/main/java/cc/ryanc/halo/web/controller/core/CommonController.java index 5994053a9..55d415871 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/core/CommonController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/core/CommonController.java @@ -3,6 +3,7 @@ package cc.ryanc.halo.web.controller.core; import cc.ryanc.halo.logging.Logger; import cc.ryanc.halo.model.entity.User; import cc.ryanc.halo.model.support.HaloConst; +import cc.ryanc.halo.utils.ThemeUtils; import cn.hutool.core.text.StrBuilder; import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.stereotype.Controller; @@ -11,6 +12,7 @@ import org.springframework.web.bind.annotation.GetMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; +import java.io.FileNotFoundException; /** *
@@ -92,7 +94,10 @@ public class CommonController implements ErrorController {
      * @return String
      */
     @GetMapping(value = "/404")
-    public String contentNotFround() {
+    public String contentNotFround() throws FileNotFoundException {
+        if(ThemeUtils.isTemplateExist("404.ftl")){
+            return "common/error/404";
+        }
         StrBuilder path = new StrBuilder("themes/");
         path.append(BaseContentController.THEME);
         path.append("/404");
@@ -105,7 +110,10 @@ public class CommonController implements ErrorController {
      * @return template path:
      */
     @GetMapping(value = "/500")
-    public String contentInternalError() {
+    public String contentInternalError() throws FileNotFoundException {
+        if(ThemeUtils.isTemplateExist("500.ftl")){
+            return "common/error/404";
+        }
         StrBuilder path = new StrBuilder("themes/");
         path.append(BaseContentController.THEME);
         path.append("/500");
diff --git a/src/main/java/cc/ryanc/halo/web/controller/core/InstallController.java b/src/main/java/cc/ryanc/halo/web/controller/core/InstallController.java
index 64921db36..b0acb866e 100644
--- a/src/main/java/cc/ryanc/halo/web/controller/core/InstallController.java
+++ b/src/main/java/cc/ryanc/halo/web/controller/core/InstallController.java
@@ -2,6 +2,7 @@ package cc.ryanc.halo.web.controller.core;
 
 import cc.ryanc.halo.model.entity.*;
 import cc.ryanc.halo.model.enums.CommentStatus;
+import cc.ryanc.halo.model.enums.PostStatus;
 import cc.ryanc.halo.model.support.JsonResult;
 import cc.ryanc.halo.service.*;
 import cc.ryanc.halo.utils.MarkdownUtils;
@@ -133,9 +134,9 @@ public class InstallController {
                     "欢迎使用Halo进行创作,删除这篇文章后赶紧开始吧。");
             post.setFormatContent(MarkdownUtils.renderMarkdown(post.getOriginalContent()));
             post.setSummary("欢迎使用Halo进行创作,删除这篇文章后赶紧开始吧。");
-            post.setStatus(0);
+            post.setStatus(PostStatus.PUBLISHED);
             post.setUrl("hello-halo");
-            post.setDisallowComment(1);
+            post.setDisallowComment(true);
             post.setThumbnail("/static/halo-frontend/images/thumbnail/thumbnail-" + RandomUtil.randomInt(1, 11) + ".jpg");
             postService.create(post);
 
diff --git a/src/main/resources/templates/common/error/404.ftl b/src/main/resources/templates/common/error/404.ftl
index 607becad3..44d986c4b 100644
--- a/src/main/resources/templates/common/error/404.ftl
+++ b/src/main/resources/templates/common/error/404.ftl
@@ -1,52 +1 @@
-<#compress>
-
-
-
-    
-    
-    
-    
-    404 Not Found
-    
-    
-    
-
-
-    
-
-
-
-
-
-
-
-

404

- -
-
- -
-
-
- - - - - +404 Not Found diff --git a/src/main/resources/templates/common/error/500.ftl b/src/main/resources/templates/common/error/500.ftl index 3c3759f5d..d2bb962a3 100644 --- a/src/main/resources/templates/common/error/500.ftl +++ b/src/main/resources/templates/common/error/500.ftl @@ -1,52 +1 @@ -<#compress> - - - - - - - - 500 Error Page - - - - - -
-
-
-
-
-
-
-
-

500

- -
-
- -
-
-
- - - - - +500 Internal Error