Refactor controllers and HaloConst

pull/146/head
johnniang 2019-04-08 19:12:30 +08:00
parent d717d72303
commit d35b6e7da3
17 changed files with 178 additions and 149 deletions

View File

@ -1,10 +1,5 @@
package run.halo.app.config; package run.halo.app.config;
import run.halo.app.config.properties.HaloProperties;
import run.halo.app.factory.StringToEnumConverterFactory;
import run.halo.app.model.support.HaloConst;
import run.halo.app.security.resolver.AuthenticationArgumentResolver;
import run.halo.app.web.controller.support.PageJacksonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -25,8 +20,11 @@ import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
import run.halo.app.config.properties.HaloProperties;
import run.halo.app.factory.StringToEnumConverterFactory; import run.halo.app.factory.StringToEnumConverterFactory;
import run.halo.app.model.support.HaloConst;
import run.halo.app.security.resolver.AuthenticationArgumentResolver; import run.halo.app.security.resolver.AuthenticationArgumentResolver;
import run.halo.app.web.controller.support.PageJacksonSerializer;
import java.util.List; import java.util.List;
@ -43,6 +41,8 @@ import java.util.List;
@PropertySource(value = "classpath:application.yaml", ignoreResourceNotFound = true, encoding = "UTF-8") @PropertySource(value = "classpath:application.yaml", ignoreResourceNotFound = true, encoding = "UTF-8")
public class WebMvcAutoConfiguration implements WebMvcConfigurer { public class WebMvcAutoConfiguration implements WebMvcConfigurer {
private static final String FILE_PROTOCOL = "file:///";
@Autowired @Autowired
private HaloProperties haloProperties; private HaloProperties haloProperties;
@ -76,13 +76,13 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer {
.addResourceLocations("classpath:/static/"); .addResourceLocations("classpath:/static/");
registry.addResourceHandler("/**") registry.addResourceHandler("/**")
.addResourceLocations("classpath:/templates/themes/") .addResourceLocations("classpath:/templates/themes/")
.addResourceLocations("file:///" + System.getProperties().getProperty("user.home") + "/halo/templates/themes/"); .addResourceLocations(FILE_PROTOCOL + haloProperties.getWorkDir() + "templates/themes/");
registry.addResourceHandler("/upload/**") registry.addResourceHandler("/upload/**")
.addResourceLocations("file:///" + System.getProperties().getProperty("user.home") + "/halo/upload/"); .addResourceLocations(FILE_PROTOCOL + haloProperties.getWorkDir() + "upload/");
registry.addResourceHandler("/favicon.ico") registry.addResourceHandler("/favicon.ico")
.addResourceLocations("classpath:/static/halo-admin/images/favicon.ico"); .addResourceLocations("classpath:/static/halo-admin/images/favicon.ico");
registry.addResourceHandler("/backup/**") registry.addResourceHandler("/backup/**")
.addResourceLocations("file:///" + System.getProperties().getProperty("user.home") + "/halo/backup/"); .addResourceLocations(FILE_PROTOCOL + haloProperties.getWorkDir() + "backup/");
registry.addResourceHandler("/admin/**") registry.addResourceHandler("/admin/**")
.addResourceLocations("classpath:/static/admin/"); .addResourceLocations("classpath:/static/admin/");
@ -108,7 +108,7 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer {
@Bean @Bean
public FreeMarkerConfigurer freemarkerConfig() { public FreeMarkerConfigurer freemarkerConfig() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPaths("file:///" + System.getProperties().getProperty("user.home") + "/halo/templates/", "classpath:/templates/"); configurer.setTemplateLoaderPaths(FILE_PROTOCOL + haloProperties.getWorkDir() + "templates/", "classpath:/templates/");
configurer.setDefaultEncoding("UTF-8"); configurer.setDefaultEncoding("UTF-8");
return configurer; return configurer;
} }

View File

@ -1,16 +1,5 @@
package run.halo.app.listener; package run.halo.app.listener;
import run.halo.app.config.properties.HaloProperties;
import run.halo.app.model.entity.User;
import run.halo.app.model.params.UserParam;
import run.halo.app.model.properties.BlogProperties;
import run.halo.app.model.properties.PrimaryProperties;
import run.halo.app.model.support.HaloConst;
import run.halo.app.model.support.Theme;
import run.halo.app.service.OptionService;
import run.halo.app.service.ThemeService;
import run.halo.app.service.UserService;
import run.halo.app.utils.HaloUtils;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
@ -22,12 +11,16 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
import run.halo.app.config.properties.HaloProperties;
import run.halo.app.model.entity.User; import run.halo.app.model.entity.User;
import run.halo.app.model.params.UserParam; import run.halo.app.model.params.UserParam;
import run.halo.app.model.properties.BlogProperties; import run.halo.app.model.properties.BlogProperties;
import run.halo.app.model.properties.PrimaryProperties; import run.halo.app.model.properties.PrimaryProperties;
import run.halo.app.model.support.HaloConst;
import run.halo.app.model.support.Theme; import run.halo.app.model.support.Theme;
import run.halo.app.service.OptionService;
import run.halo.app.service.ThemeService; import run.halo.app.service.ThemeService;
import run.halo.app.service.UserService;
import run.halo.app.utils.HaloUtils; import run.halo.app.utils.HaloUtils;
import java.io.File; import java.io.File;
@ -36,9 +29,6 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static run.halo.app.model.support.HaloConst.ACTIVATED_THEME_NAME;
import static run.halo.app.model.support.HaloConst.DEFAULT_THEME_NAME;
/** /**
* The method executed after the application is started. * The method executed after the application is started.
* *
@ -75,7 +65,7 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
// save halo version to database // save halo version to database
this.cacheThemes(); this.cacheThemes();
this.cacheOwo(); this.cacheOwo();
this.getActiveTheme(); this.cacheActiveTheme();
this.printStartInfo(); this.printStartInfo();
this.initThemes(); this.initThemes();
@ -119,13 +109,11 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
/** /**
* Get active theme * Get active theme
*/ */
private void getActiveTheme() { private void cacheActiveTheme() {
ACTIVATED_THEME_NAME = optionService.getByProperty(PrimaryProperties.THEME).orElse(DEFAULT_THEME_NAME);
try { try {
configuration.setSharedVariable("themeName", ACTIVATED_THEME_NAME); configuration.setSharedVariable("themeName", optionService.getTheme());
} catch (TemplateModelException e) { } catch (TemplateModelException e) {
e.printStackTrace(); log.error("", e);
} }
} }

View File

@ -24,11 +24,6 @@ public class HaloConst {
*/ */
public final static String DEFAULT_THEME_NAME = "anatole"; public final static String DEFAULT_THEME_NAME = "anatole";
/**
* Activated theme name.
*/
public static String ACTIVATED_THEME_NAME = "anatole";
/** /**
* version constant * version constant
*/ */

View File

@ -1,13 +1,5 @@
package run.halo.app.service; package run.halo.app.service;
import run.halo.app.exception.MissingPropertyException;
import run.halo.app.model.dto.OptionOutputDTO;
import run.halo.app.model.entity.Option;
import run.halo.app.model.enums.OptionSource;
import run.halo.app.model.enums.ValueEnum;
import run.halo.app.model.params.OptionParam;
import run.halo.app.model.properties.PropertyEnum;
import run.halo.app.service.base.CrudService;
import com.qiniu.common.Zone; import com.qiniu.common.Zone;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
@ -16,6 +8,7 @@ import run.halo.app.exception.MissingPropertyException;
import run.halo.app.model.dto.OptionOutputDTO; import run.halo.app.model.dto.OptionOutputDTO;
import run.halo.app.model.entity.Option; import run.halo.app.model.entity.Option;
import run.halo.app.model.enums.OptionSource; import run.halo.app.model.enums.OptionSource;
import run.halo.app.model.enums.ValueEnum;
import run.halo.app.model.params.OptionParam; import run.halo.app.model.params.OptionParam;
import run.halo.app.model.properties.PropertyEnum; import run.halo.app.model.properties.PropertyEnum;
import run.halo.app.service.base.CrudService; import run.halo.app.service.base.CrudService;
@ -66,6 +59,16 @@ public interface OptionService extends CrudService<Option, Integer> {
@Transactional @Transactional
void save(List<OptionParam> optionParams, @NonNull OptionSource source); void save(List<OptionParam> optionParams, @NonNull OptionSource source);
/**
* Saves a property.
*
* @param property must not be null
* @param value could be null
* @param source must not be null
*/
@Transactional
void saveProperty(@NonNull PropertyEnum property, String value, @NonNull OptionSource source);
/** /**
* Saves blog properties. * Saves blog properties.
* *
@ -276,4 +279,13 @@ public interface OptionService extends CrudService<Option, Integer> {
*/ */
@NonNull @NonNull
Locale getLocale(); Locale getLocale();
/**
* Gets current active theme.
*
* @return current active theme
*/
@NonNull
String getTheme();
} }

View File

@ -124,6 +124,7 @@ public interface PostService extends CrudService<Post, Integer> {
* @param url post url. * @param url post url.
* @return Post * @return Post
*/ */
@NonNull
Post getByUrl(@NonNull String url); Post getByUrl(@NonNull String url);
/** /**

View File

@ -1,10 +1,9 @@
package run.halo.app.service; package run.halo.app.service;
import org.springframework.lang.NonNull;
import run.halo.app.model.dto.TagOutputDTO; import run.halo.app.model.dto.TagOutputDTO;
import run.halo.app.model.entity.Tag; import run.halo.app.model.entity.Tag;
import run.halo.app.service.base.CrudService; import run.halo.app.service.base.CrudService;
import org.springframework.lang.NonNull;
import run.halo.app.service.base.CrudService;
import java.util.List; import java.util.List;

View File

@ -119,4 +119,14 @@ public interface ThemeService {
*/ */
@Nullable @Nullable
Object fetchConfig(@NonNull String themeName); Object fetchConfig(@NonNull String themeName);
/**
* Render a theme page.
*
* @param pageName must not be blank
* @return full path of the theme page
*/
@NonNull
String render(@NonNull String pageName);
} }

View File

@ -24,6 +24,8 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static run.halo.app.model.support.HaloConst.DEFAULT_THEME_NAME;
/** /**
* OptionService implementation class * OptionService implementation class
* *
@ -102,6 +104,14 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
optionParams.forEach(optionParam -> save(optionParam.getOptionKey(), optionParam.getOptionValue(), source)); optionParams.forEach(optionParam -> save(optionParam.getOptionKey(), optionParam.getOptionValue(), source));
} }
@Override
public void saveProperty(PropertyEnum property, String value, OptionSource source) {
Assert.notNull(property, "Property must not be null");
Assert.notNull(source, "Option source must not be null");
save(property.getValue(), value, source);
}
@Override @Override
public void saveProperties(Map<? extends PropertyEnum, String> properties, OptionSource source) { public void saveProperties(Map<? extends PropertyEnum, String> properties, OptionSource source) {
if (CollectionUtils.isEmpty(properties)) { if (CollectionUtils.isEmpty(properties)) {
@ -280,4 +290,9 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
} }
}).orElseGet(Locale::getDefault); }).orElseGet(Locale::getDefault);
} }
@Override
public String getTheme() {
return getByProperty(PrimaryProperties.THEME).orElse(DEFAULT_THEME_NAME);
}
} }

View File

@ -9,6 +9,7 @@ import cn.hutool.setting.dialect.Props;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import run.halo.app.config.properties.HaloProperties; import run.halo.app.config.properties.HaloProperties;
@ -17,6 +18,7 @@ import run.halo.app.model.support.HaloConst;
import run.halo.app.model.support.Theme; import run.halo.app.model.support.Theme;
import run.halo.app.model.support.ThemeFile; import run.halo.app.model.support.ThemeFile;
import run.halo.app.model.support.ThemeProperties; import run.halo.app.model.support.ThemeProperties;
import run.halo.app.service.OptionService;
import run.halo.app.service.ThemeService; import run.halo.app.service.ThemeService;
import run.halo.app.utils.FilenameUtils; import run.halo.app.utils.FilenameUtils;
@ -47,15 +49,30 @@ public class ThemeServiceImpl implements ThemeService {
*/ */
private static String[] FILTER_FILES = {".git", ".DS_Store", "theme.properties"}; private static String[] FILTER_FILES = {".git", ".DS_Store", "theme.properties"};
/**
* Theme folder location.
*/
private final static String THEME_FOLDER = "templates/themes"; private final static String THEME_FOLDER = "templates/themes";
/**
* Configuration file name.
*/
private final static String[] OPTIONS_NAMES = {"options.yaml", "options.yml"}; private final static String[] OPTIONS_NAMES = {"options.yaml", "options.yml"};
/**
* Render template.
*/
private final static String RENDER_TEMPLATE = "themes/%s/%s";
private final Path workDir; private final Path workDir;
private final ObjectMapper yamlMapper; private final ObjectMapper yamlMapper;
public ThemeServiceImpl(HaloProperties haloProperties) { private final OptionService optionService;
public ThemeServiceImpl(HaloProperties haloProperties,
OptionService optionService) {
this.optionService = optionService;
yamlMapper = new ObjectMapper(new YAMLFactory()); yamlMapper = new ObjectMapper(new YAMLFactory());
workDir = Paths.get(haloProperties.getWorkDir(), THEME_FOLDER); workDir = Paths.get(haloProperties.getWorkDir(), THEME_FOLDER);
} }
@ -181,7 +198,7 @@ public class ThemeServiceImpl implements ThemeService {
*/ */
@Override @Override
public boolean isTemplateExist(String template) { public boolean isTemplateExist(String template) {
StrBuilder templatePath = new StrBuilder(HaloConst.ACTIVATED_THEME_NAME); StrBuilder templatePath = new StrBuilder(getThemeName());
templatePath.append("/"); templatePath.append("/");
templatePath.append(template); templatePath.append(template);
File file = new File(getThemeBasePath(), templatePath.toString()); File file = new File(getThemeBasePath(), templatePath.toString());
@ -304,4 +321,19 @@ public class ThemeServiceImpl implements ThemeService {
return null; return null;
} }
} }
@Override
public String render(String pageName) {
return String.format(RENDER_TEMPLATE, optionService.getTheme(), pageName);
}
/**
* Gets theme name.
*
* @return theme name.
*/
@NonNull
private String getThemeName() {
return optionService.getTheme();
}
} }

View File

@ -1,7 +1,6 @@
package run.halo.app.utils; package run.halo.app.utils;
import cn.hutool.core.text.StrBuilder; import cn.hutool.core.text.StrBuilder;
import io.github.biezhi.ome.OhMyEmail;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
@ -19,7 +18,6 @@ import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.Date; import java.util.Date;
import java.util.Properties;
import java.util.UUID; import java.util.UUID;
/** /**
@ -33,6 +31,12 @@ import java.util.UUID;
@Slf4j @Slf4j
public class HaloUtils { public class HaloUtils {
/**
* Time format.
*
* @param totalSeconds seconds
* @return formatted time
*/
@NonNull @NonNull
public static String timeFormat(long totalSeconds) { public static String timeFormat(long totalSeconds) {
if (totalSeconds <= 0) { if (totalSeconds <= 0) {

View File

@ -6,17 +6,13 @@ import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import run.halo.app.model.enums.OptionSource; import run.halo.app.model.enums.OptionSource;
import run.halo.app.model.properties.PrimaryProperties; import run.halo.app.model.properties.PrimaryProperties;
import run.halo.app.model.properties.PropertyEnum;
import run.halo.app.model.support.BaseResponse; import run.halo.app.model.support.BaseResponse;
import run.halo.app.model.support.HaloConst;
import run.halo.app.model.support.Theme; import run.halo.app.model.support.Theme;
import run.halo.app.model.support.ThemeFile; import run.halo.app.model.support.ThemeFile;
import run.halo.app.service.OptionService; import run.halo.app.service.OptionService;
import run.halo.app.service.ThemeService; import run.halo.app.service.ThemeService;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* Theme controller. * Theme controller.
@ -60,7 +56,7 @@ public class ThemeController {
*/ */
@GetMapping("files") @GetMapping("files")
public List<ThemeFile> listFiles() { public List<ThemeFile> listFiles() {
return themeService.listThemeFolderBy(HaloConst.ACTIVATED_THEME_NAME); return themeService.listThemeFolderBy(optionService.getTheme());
} }
@GetMapping("files/content") @GetMapping("files/content")
@ -76,7 +72,7 @@ public class ThemeController {
@GetMapping("files/custom") @GetMapping("files/custom")
public List<String> customTemplate() { public List<String> customTemplate() {
return themeService.getCustomTpl(HaloConst.ACTIVATED_THEME_NAME); return themeService.getCustomTpl(optionService.getTheme());
} }
/** /**
@ -86,13 +82,10 @@ public class ThemeController {
* @throws TemplateModelException TemplateModelException * @throws TemplateModelException TemplateModelException
*/ */
@GetMapping(value = "active") @GetMapping(value = "active")
@ApiOperation("Active theme") @ApiOperation("Active a theme")
public void active(@RequestParam(name = "theme", defaultValue = "anatole") String theme) throws TemplateModelException { public void active(String theme) throws TemplateModelException {
Map<PropertyEnum, String> properties = new HashMap<>(1); // TODO Check existence of the theme
properties.put(PrimaryProperties.THEME, theme); optionService.saveProperty(PrimaryProperties.THEME, theme, OptionSource.SYSTEM);
// TODO Refactor: saveProperties => saveProperty
optionService.saveProperties(properties, OptionSource.SYSTEM);
HaloConst.ACTIVATED_THEME_NAME = theme;
configuration.setSharedVariable("themeName", theme); configuration.setSharedVariable("themeName", theme);
configuration.setSharedVariable("options", optionService.listOptions()); configuration.setSharedVariable("options", optionService.listOptions());
} }

View File

@ -1,9 +1,5 @@
package run.halo.app.web.controller.content; package run.halo.app.web.controller.content;
import run.halo.app.model.entity.Category;
import run.halo.app.service.CategoryService;
import run.halo.app.service.PostService;
import run.halo.app.web.controller.content.base.BaseContentController;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.web.SortDefault; import org.springframework.data.web.SortDefault;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
@ -14,7 +10,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import run.halo.app.model.entity.Category; import run.halo.app.model.entity.Category;
import run.halo.app.service.CategoryService; import run.halo.app.service.CategoryService;
import run.halo.app.service.PostService; import run.halo.app.service.PostService;
import run.halo.app.web.controller.content.base.BaseContentController; import run.halo.app.service.ThemeService;
import java.util.List; import java.util.List;
@ -26,16 +22,20 @@ import static org.springframework.data.domain.Sort.Direction.DESC;
*/ */
@Controller @Controller
@RequestMapping(value = "/categories") @RequestMapping(value = "/categories")
public class ContentCategoryController extends BaseContentController { public class ContentCategoryController {
private final CategoryService categoryService; private final CategoryService categoryService;
private final PostService postService; private final PostService postService;
private final ThemeService themeService;
public ContentCategoryController(CategoryService categoryService, public ContentCategoryController(CategoryService categoryService,
PostService postService) { PostService postService,
ThemeService themeService) {
this.categoryService = categoryService; this.categoryService = categoryService;
this.postService = postService; this.postService = postService;
this.themeService = themeService;
} }
/** /**
@ -48,7 +48,7 @@ public class ContentCategoryController extends BaseContentController {
public String categories(Model model) { public String categories(Model model) {
final List<Category> categories = categoryService.listAll(); final List<Category> categories = categoryService.listAll();
model.addAttribute("categories", categories); model.addAttribute("categories", categories);
return this.render("categories"); return themeService.render("categories");
} }
/** /**
@ -77,6 +77,7 @@ public class ContentCategoryController extends BaseContentController {
@PathVariable("slugName") String slugName, @PathVariable("slugName") String slugName,
@PathVariable("page") Integer page, @PathVariable("page") Integer page,
@SortDefault(sort = "postDate", direction = DESC) Sort sort) { @SortDefault(sort = "postDate", direction = DESC) Sort sort) {
// TODO Complete this api in the future
return ""; return "";
} }
} }

View File

@ -1,11 +1,5 @@
package run.halo.app.web.controller.content; package run.halo.app.web.controller.content;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.enums.PostType;
import run.halo.app.model.vo.PostListVO;
import run.halo.app.service.OptionService;
import run.halo.app.service.PostService;
import run.halo.app.web.controller.content.base.BaseContentController;
import cn.hutool.core.util.PageUtil; import cn.hutool.core.util.PageUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
@ -20,8 +14,9 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import run.halo.app.model.enums.PostStatus; import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.vo.PostListVO; import run.halo.app.model.vo.PostListVO;
import run.halo.app.service.OptionService;
import run.halo.app.service.PostService; import run.halo.app.service.PostService;
import run.halo.app.web.controller.content.base.BaseContentController; import run.halo.app.service.ThemeService;
import static org.springframework.data.domain.Sort.Direction.DESC; import static org.springframework.data.domain.Sort.Direction.DESC;
@ -34,16 +29,20 @@ import static org.springframework.data.domain.Sort.Direction.DESC;
@Slf4j @Slf4j
@Controller @Controller
@RequestMapping @RequestMapping
public class ContentIndexController extends BaseContentController { public class ContentIndexController {
private final PostService postService; private final PostService postService;
private final OptionService optionService; private final OptionService optionService;
private final ThemeService themeService;
public ContentIndexController(PostService postService, public ContentIndexController(PostService postService,
OptionService optionService) { OptionService optionService,
ThemeService themeService) {
this.postService = postService; this.postService = postService;
this.optionService = optionService; this.optionService = optionService;
this.themeService = themeService;
} }
@ -80,6 +79,6 @@ public class ContentIndexController extends BaseContentController {
model.addAttribute("is_index", true); model.addAttribute("is_index", true);
model.addAttribute("posts", posts); model.addAttribute("posts", posts);
model.addAttribute("rainbow", rainbow); model.addAttribute("rainbow", rainbow);
return this.render("index"); return themeService.render("index");
} }
} }

View File

@ -1,19 +1,11 @@
package run.halo.app.web.controller.content; package run.halo.app.web.controller.content;
import run.halo.app.model.entity.Comment;
import run.halo.app.model.entity.Gallery;
import run.halo.app.model.entity.Post;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.enums.PostType;
import run.halo.app.service.CommentService;
import run.halo.app.service.GalleryService;
import run.halo.app.service.PostService;
import run.halo.app.web.controller.content.base.BaseContentController;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import run.halo.app.exception.NotFoundException;
import run.halo.app.model.entity.Comment; import run.halo.app.model.entity.Comment;
import run.halo.app.model.entity.Gallery; import run.halo.app.model.entity.Gallery;
import run.halo.app.model.entity.Post; import run.halo.app.model.entity.Post;
@ -21,7 +13,7 @@ import run.halo.app.model.enums.PostStatus;
import run.halo.app.service.CommentService; import run.halo.app.service.CommentService;
import run.halo.app.service.GalleryService; import run.halo.app.service.GalleryService;
import run.halo.app.service.PostService; import run.halo.app.service.PostService;
import run.halo.app.web.controller.content.base.BaseContentController; import run.halo.app.service.ThemeService;
import java.util.List; import java.util.List;
@ -30,7 +22,7 @@ import java.util.List;
* @date : 2019-03-21 * @date : 2019-03-21
*/ */
@Controller @Controller
public class ContentPageController extends BaseContentController { public class ContentPageController {
private final GalleryService galleryService; private final GalleryService galleryService;
@ -38,10 +30,16 @@ public class ContentPageController extends BaseContentController {
private final CommentService commentService; private final CommentService commentService;
public ContentPageController(GalleryService galleryService, PostService postService, CommentService commentService) { private final ThemeService themeService;
public ContentPageController(GalleryService galleryService,
PostService postService,
CommentService commentService,
ThemeService themeService) {
this.galleryService = galleryService; this.galleryService = galleryService;
this.postService = postService; this.postService = postService;
this.commentService = commentService; this.commentService = commentService;
this.themeService = themeService;
} }
/** /**
@ -53,7 +51,7 @@ public class ContentPageController extends BaseContentController {
public String gallery(Model model) { public String gallery(Model model) {
final List<Gallery> galleries = galleryService.listAll(); final List<Gallery> galleries = galleryService.listAll();
model.addAttribute("galleries", galleries); model.addAttribute("galleries", galleries);
return this.render("gallery"); return themeService.render("gallery");
} }
/** /**
@ -63,7 +61,7 @@ public class ContentPageController extends BaseContentController {
*/ */
@GetMapping(value = "/links") @GetMapping(value = "/links")
public String links() { public String links() {
return this.render("links"); return themeService.render("links");
} }
/** /**
@ -78,10 +76,15 @@ public class ContentPageController extends BaseContentController {
@RequestParam(value = "cp", defaultValue = "1") Integer cp, @RequestParam(value = "cp", defaultValue = "1") Integer cp,
Model model) { Model model) {
final Post post = postService.getByUrl(url); final Post post = postService.getByUrl(url);
if (null == post || !post.getStatus().equals(PostStatus.PUBLISHED)) {
return this.renderNotFound(); if (!post.getStatus().equals(PostStatus.PUBLISHED)) {
throw new NotFoundException("The post isn't published").setErrorData(url);
} }
List<Comment> comments; List<Comment> comments;
// TODO Complete this api
// if (StrUtil.equals(OPTIONS.get(BlogProperties.NEW_COMMENT_NEED_CHECK.getValue()), "true") || OPTIONS.get(BlogProperties.NEW_COMMENT_NEED_CHECK.getValue()) == null) { // if (StrUtil.equals(OPTIONS.get(BlogProperties.NEW_COMMENT_NEED_CHECK.getValue()), "true") || OPTIONS.get(BlogProperties.NEW_COMMENT_NEED_CHECK.getValue()) == null) {
// comments = commentService.findCommentsByPostAndCommentStatus(post, CommentStatus.PUBLISHED.getValue()); // comments = commentService.findCommentsByPostAndCommentStatus(post, CommentStatus.PUBLISHED.getValue());
// } else { // } else {
@ -106,6 +109,6 @@ public class ContentPageController extends BaseContentController {
// if (StrUtil.isNotEmpty(post.getCustomTpl())) { // if (StrUtil.isNotEmpty(post.getCustomTpl())) {
// return this.render(post.getCustomTpl()); // return this.render(post.getCustomTpl());
// } // }
return this.render("page"); return themeService.render("page");
} }
} }

View File

@ -1,11 +1,5 @@
package run.halo.app.web.controller.content; package run.halo.app.web.controller.content;
import run.halo.app.model.entity.Tag;
import run.halo.app.model.vo.PostListVO;
import run.halo.app.service.OptionService;
import run.halo.app.service.PostService;
import run.halo.app.service.TagService;
import run.halo.app.web.controller.content.base.BaseContentController;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
@ -18,9 +12,10 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import run.halo.app.model.entity.Tag; import run.halo.app.model.entity.Tag;
import run.halo.app.model.vo.PostListVO; import run.halo.app.model.vo.PostListVO;
import run.halo.app.service.OptionService;
import run.halo.app.service.PostService; import run.halo.app.service.PostService;
import run.halo.app.service.TagService; import run.halo.app.service.TagService;
import run.halo.app.web.controller.content.base.BaseContentController; import run.halo.app.service.ThemeService;
import static org.springframework.data.domain.Sort.Direction.DESC; import static org.springframework.data.domain.Sort.Direction.DESC;
@ -32,7 +27,7 @@ import static org.springframework.data.domain.Sort.Direction.DESC;
*/ */
@Controller @Controller
@RequestMapping(value = "/tags") @RequestMapping(value = "/tags")
public class ContentTagController extends BaseContentController { public class ContentTagController {
private final TagService tagService; private final TagService tagService;
@ -40,12 +35,16 @@ public class ContentTagController extends BaseContentController {
private final OptionService optionService; private final OptionService optionService;
private final ThemeService themeService;
public ContentTagController(TagService tagService, public ContentTagController(TagService tagService,
PostService postService, PostService postService,
OptionService optionService) { OptionService optionService,
ThemeService themeService) {
this.tagService = tagService; this.tagService = tagService;
this.postService = postService; this.postService = postService;
this.optionService = optionService; this.optionService = optionService;
this.themeService = themeService;
} }
/** /**
@ -55,7 +54,7 @@ public class ContentTagController extends BaseContentController {
*/ */
@GetMapping @GetMapping
public String tags() { public String tags() {
return this.render("tags"); return themeService.render("tags");
} }
/** /**
@ -84,20 +83,18 @@ public class ContentTagController extends BaseContentController {
@PathVariable("slugName") String slugName, @PathVariable("slugName") String slugName,
@PathVariable("page") Integer page, @PathVariable("page") Integer page,
@SortDefault(sort = "postDate", direction = DESC) Sort sort) { @SortDefault(sort = "postDate", direction = DESC) Sort sort) {
final Tag tag = tagService.getBySlugNameOfNonNull(slugName); Tag tag = tagService.getBySlugNameOfNonNull(slugName);
if (null == tag) {
return this.renderNotFound();
}
int size = optionService.getPostPageSize();
final Pageable pageable = PageRequest.of(page - 1, size, sort);
int size = optionService.getPostPageSize();
Pageable pageable = PageRequest.of(page - 1, size, sort);
Page<PostListVO> posts;
// TODO get posts by tag // TODO get posts by tag
final Page<PostListVO> posts;
//final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3); //final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
// model.addAttribute("is_tags", true); // model.addAttribute("is_tags", true);
// model.addAttribute("posts", posts); // model.addAttribute("posts", posts);
// model.addAttribute("rainbow", rainbow); // model.addAttribute("rainbow", rainbow);
// model.addAttribute("tag", tag); // model.addAttribute("tag", tag);
return this.render("tag"); return themeService.render("tag");
} }
} }

View File

@ -1,9 +1,5 @@
package run.halo.app.web.controller.content.base; package run.halo.app.web.controller.content.base;
import cn.hutool.core.text.StrBuilder;
import static run.halo.app.model.support.HaloConst.ACTIVATED_THEME_NAME;
/** /**
* Content base Controller * Content base Controller
* *
@ -12,26 +8,4 @@ import static run.halo.app.model.support.HaloConst.ACTIVATED_THEME_NAME;
*/ */
public abstract class BaseContentController { public abstract class BaseContentController {
/**
* Render page by template name
*
* @param pageName pageName
* @return template path
*/
public String render(String pageName) {
final StrBuilder themeStr = new StrBuilder("themes/");
themeStr.append(ACTIVATED_THEME_NAME);
themeStr.append("/");
themeStr.append(pageName);
return themeStr.toString();
}
/**
* Redirect to 404
*
* @return redirect:/404
*/
public String renderNotFound() {
return "redirect:/404";
}
} }

View File

@ -1,16 +1,14 @@
package run.halo.app.web.controller.core; package run.halo.app.web.controller.core;
import run.halo.app.model.entity.User;
import run.halo.app.model.support.HaloConst;
import run.halo.app.service.ThemeService;
import cn.hutool.core.text.StrBuilder; import cn.hutool.core.text.StrBuilder;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import run.halo.app.model.entity.User; 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 run.halo.app.service.ThemeService;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -35,8 +33,16 @@ public class CommonController implements ErrorController {
private static final String ADMIN_URL = "/admin"; private static final String ADMIN_URL = "/admin";
@Autowired private final ThemeService themeService;
private ThemeService themeService;
private final OptionService optionService;
public CommonController(ThemeService themeService,
OptionService optionService) {
this.themeService = themeService;
this.optionService = optionService;
}
/** /**
* Handle error * Handle error
@ -108,7 +114,7 @@ public class CommonController implements ErrorController {
return "common/error/404"; return "common/error/404";
} }
StrBuilder path = new StrBuilder("themes/"); StrBuilder path = new StrBuilder("themes/");
path.append(HaloConst.ACTIVATED_THEME_NAME); path.append(optionService.getTheme());
path.append("/404"); path.append("/404");
return path.toString(); return path.toString();
} }
@ -119,12 +125,12 @@ public class CommonController implements ErrorController {
* @return template path: * @return template path:
*/ */
@GetMapping(value = "/500") @GetMapping(value = "/500")
public String contentInternalError() throws FileNotFoundException { public String contentInternalError() {
if (!themeService.isTemplateExist(INTERNAL_ERROR_TEMPLATE)) { if (!themeService.isTemplateExist(INTERNAL_ERROR_TEMPLATE)) {
return "common/error/500"; return "common/error/500";
} }
StrBuilder path = new StrBuilder("themes/"); StrBuilder path = new StrBuilder("themes/");
path.append(HaloConst.ACTIVATED_THEME_NAME); path.append(optionService.getTheme());
path.append("/500"); path.append("/500");
return path.toString(); return path.toString();
} }