From e4e36cbfe5e94d89071b81afa4f64edc99e5e463 Mon Sep 17 00:00:00 2001 From: johnniang Date: Tue, 30 Apr 2019 15:26:25 +0800 Subject: [PATCH] Refactor template existence check --- .../content/ContentSheetController.java | 2 +- .../app/controller/core/CommonController.java | 6 ++---- .../run/halo/app/service/ThemeService.java | 4 ++-- .../app/service/impl/ThemeServiceImpl.java | 18 +++++++++++------- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/main/java/run/halo/app/controller/content/ContentSheetController.java b/src/main/java/run/halo/app/controller/content/ContentSheetController.java index c5130dfec..dfbc37aa1 100644 --- a/src/main/java/run/halo/app/controller/content/ContentSheetController.java +++ b/src/main/java/run/halo/app/controller/content/ContentSheetController.java @@ -66,7 +66,7 @@ public class ContentSheetController { model.addAttribute("sheet", sheetService.convertToDetail(sheet)); if (StrUtil.isNotEmpty(sheet.getTemplate())) { - if (themeService.isTemplateExist(sheet.getTemplate() + HaloConst.SUFFIX_FTL)) { + if (themeService.templateExists(sheet.getTemplate() + HaloConst.SUFFIX_FTL)) { return themeService.render(sheet.getTemplate()); } } diff --git a/src/main/java/run/halo/app/controller/core/CommonController.java b/src/main/java/run/halo/app/controller/core/CommonController.java index 4ce631d9f..6ab7c18e9 100644 --- a/src/main/java/run/halo/app/controller/core/CommonController.java +++ b/src/main/java/run/halo/app/controller/core/CommonController.java @@ -8,12 +8,10 @@ import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.GetMapping; import run.halo.app.model.entity.User; import run.halo.app.model.support.HaloConst; -import run.halo.app.service.OptionService; import run.halo.app.service.ThemeService; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; -import java.io.FileNotFoundException; /** * Error page Controller @@ -105,7 +103,7 @@ public class CommonController implements ErrorController { */ @GetMapping(value = "/404") public String contentNotFround() { - if (!themeService.isTemplateExist(NOT_FROUND_TEMPLATE)) { + if (!themeService.templateExists(NOT_FROUND_TEMPLATE)) { return "common/error/404"; } StrBuilder path = new StrBuilder("themes/"); @@ -121,7 +119,7 @@ public class CommonController implements ErrorController { */ @GetMapping(value = "/500") public String contentInternalError() { - if (!themeService.isTemplateExist(INTERNAL_ERROR_TEMPLATE)) { + if (!themeService.templateExists(INTERNAL_ERROR_TEMPLATE)) { return "common/error/500"; } StrBuilder path = new StrBuilder("themes/"); diff --git a/src/main/java/run/halo/app/service/ThemeService.java b/src/main/java/run/halo/app/service/ThemeService.java index 50fa5628f..6d2db16ff 100644 --- a/src/main/java/run/halo/app/service/ThemeService.java +++ b/src/main/java/run/halo/app/service/ThemeService.java @@ -119,10 +119,10 @@ public interface ThemeService { /** * Judging whether template exists under the specified theme * - * @param template template + * @param template template must not be blank * @return boolean */ - boolean isTemplateExist(@NonNull String template); + boolean templateExists(@NonNull String template); /** * Checks whether theme exists under template path diff --git a/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java b/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java index d75e22b9c..d140ba312 100644 --- a/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java @@ -3,7 +3,6 @@ package run.halo.app.service.impl; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.file.FileReader; import cn.hutool.core.io.file.FileWriter; -import cn.hutool.core.text.StrBuilder; import cn.hutool.core.util.StrUtil; import freemarker.template.Configuration; import lombok.extern.slf4j.Slf4j; @@ -182,12 +181,17 @@ public class ThemeServiceImpl implements ThemeService { } @Override - public boolean isTemplateExist(String template) { - StrBuilder templatePath = new StrBuilder(getActivatedThemeId()); - templatePath.append("/"); - templatePath.append(template); - File file = new File(getThemeBasePath(), templatePath.toString()); - return file.exists(); + public boolean templateExists(String template) { + Assert.hasText(template, "Template must not be blank"); + + // Resolve template path + Path templatePath = getBasePath().resolve(getActivatedTheme().getFolderName()).resolve(template); + + // Check the directory + checkDirectory(templatePath.toString()); + + // Check existence + return Files.exists(templatePath); } @Override