pull/137/head
ruibaby 2019-03-16 15:59:44 +08:00
parent f11b3ca65a
commit e900016874
6 changed files with 164 additions and 8 deletions

View File

@ -54,7 +54,8 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/templates/themes/");
.addResourceLocations("classpath:/templates/themes/")
.addResourceLocations("file:///" + System.getProperties().getProperty("user.home") + "/halo/templates/themes/");
registry.addResourceHandler("/upload/**")
.addResourceLocations("file:///" + System.getProperties().getProperty("user.home") + "/halo/upload/");
registry.addResourceHandler("/favicon.ico")

View File

@ -3,6 +3,8 @@ package cc.ryanc.halo.repository;
import cc.ryanc.halo.model.entity.Option;
import cc.ryanc.halo.repository.base.BaseRepository;
import java.util.Optional;
/**
* Option repository.
*
@ -10,4 +12,18 @@ import cc.ryanc.halo.repository.base.BaseRepository;
*/
public interface OptionRepository extends BaseRepository<Option, Integer> {
/**
* Query option by key
*
* @param key key
* @return Option
*/
Optional<Option> findByOptionKey(String key);
/**
* Delete option by key
*
* @param key key
*/
void removeByOptionKey(String key);
}

View File

@ -3,6 +3,8 @@ package cc.ryanc.halo.service;
import cc.ryanc.halo.model.entity.Option;
import cc.ryanc.halo.service.base.CrudService;
import java.util.Map;
/**
* Option service.
*
@ -10,4 +12,33 @@ import cc.ryanc.halo.service.base.CrudService;
*/
public interface OptionService extends CrudService<Option, Integer> {
/**
* Save one option
*
* @param key key
* @param value value
*/
void saveOption(String key, String value);
/**
* Save multiple options
*
* @param options options
*/
void saveOptions(Map<String, String> options);
/**
* Get all options
*
* @return Map
*/
Map<String, String> listOptions();
/**
* Get option by key
*
* @param key key
* @return String
*/
String getByKey(String key);
}

View File

@ -4,7 +4,12 @@ import cc.ryanc.halo.model.entity.Option;
import cc.ryanc.halo.repository.OptionRepository;
import cc.ryanc.halo.service.OptionService;
import cc.ryanc.halo.service.base.AbstractCrudService;
import cc.ryanc.halo.utils.ServiceUtils;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.Map;
/**
* OptionService implementation class
@ -21,4 +26,65 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
super(optionRepository);
this.optionRepository = optionRepository;
}
/**
* Save one option
*
* @param key key
* @param value value
*/
@Override
public void saveOption(String key, String value) {
if (StrUtil.equals(value, "")) {
optionRepository.removeByOptionKey(key);
} else if (StrUtil.isNotEmpty(key)) {
Option options = optionRepository.findByOptionKey(key).map(option -> {
// Exist
option.setOptionValue(value);
return option;
}).orElseGet(() -> {
// Not exist
Option option = new Option();
option.setOptionKey(key);
option.setOptionValue(value);
return option;
});
// Save or update the options
optionRepository.save(options);
}
}
/**
* Save multiple options
*
* @param options options
*/
@Override
public void saveOptions(Map<String, String> options) {
if (!CollectionUtils.isEmpty(options)) {
options.forEach(this::saveOption);
}
}
/**
* Get all options
*
* @return Map
*/
@Override
public Map<String, String> listOptions() {
return ServiceUtils.convertToMap(listAll(), Option::getOptionKey, Option::getOptionValue);
}
/**
* Get option by key
*
* @param key key
* @return String
*/
@Override
public String getByKey(String key) {
return optionRepository.findByOptionKey(key).map(Option::getOptionValue).orElse(null);
}
}

View File

@ -1,8 +1,6 @@
package cc.ryanc.halo.utils;
import cc.ryanc.halo.model.support.BackupDto;
import cc.ryanc.halo.model.support.Theme;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.StrUtil;
import com.qiniu.common.Zone;
@ -208,17 +206,35 @@ public class HaloUtils {
}
/**
*
* Scan internal themes and user's themes
*
* @return List
*/
public static List<Theme> getThemes() {
final List<Theme> themes = new ArrayList<>();
try {
// 获取项目根路径
final File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
// 获取主题路径
final File themesPath = new File(basePath.getAbsolutePath(), "templates/themes");
final File classPath = new File(ResourceUtils.getURL("classpath:").getPath());
final File internalThemesPath = new File(classPath.getAbsolutePath(), "templates/themes");
themes.addAll(getThemesByPath(internalThemesPath));
final File userPath = new File(System.getProperties().getProperty("user.home"));
final File userThemesPath = new File(userPath.getAbsolutePath(),"halo/templates/themes");
themes.addAll(getThemesByPath(userThemesPath));
} catch (Exception e) {
log.error("Themes scan failed", e);
}
return themes;
}
/**
* Scan themes by directory
*
* @param file file
* @return List<Theme>
*/
private static List<Theme> getThemesByPath(File themesPath) {
final List<Theme> themes = new ArrayList<>();
try {
final File[] files = themesPath.listFiles();
if (null != files) {
Theme theme;

View File

@ -1,8 +1,16 @@
package cc.ryanc.halo.web.controller.admin;
import cc.ryanc.halo.model.support.Theme;
import cc.ryanc.halo.service.OptionService;
import cc.ryanc.halo.utils.HaloUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
import java.util.Map;
/**
* Themes controller
*
@ -12,4 +20,22 @@ import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(value = "/admin/themes")
public class ThemeController {
private OptionService optionService;
public ThemeController(OptionService optionService) {
this.optionService = optionService;
}
@GetMapping
public String themes(Model model) {
Map<String, String> options = optionService.listOptions();
model.addAttribute("options",options);
List<Theme> themes = HaloUtils.getThemes();
model.addAttribute("themes", themes);
return "admin/admin_theme";
}
}