Refactor template existence check

pull/146/head
johnniang 2019-04-30 15:26:25 +08:00
parent 57172cdf55
commit e4e36cbfe5
4 changed files with 16 additions and 14 deletions

View File

@ -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());
}
}

View File

@ -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/");

View File

@ -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

View File

@ -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