PerfreeBlog
PerfreeBlog copied to clipboard
如果初始化后的资源目录被删除,则无法再一次加载Default主题
这种情况将无法使用
优化方式:
public static final String DEFAULT_THEMES_NAME= "default.zip";
@Slf4j
public class Themeutils {
/**
* 解压默认主题
*/
public static void unzipDefaultTheme() {
File file = ClassPathFileUtil.getClassPathFile(SystemConstants.DEV_THEMES_PATH + SystemConstants.FILE_SEPARATOR + SystemConstants.DEFAULT_THEMES_NAME);
if (null == file || !file.exists()) {
throw new ServiceException(ErrorCode.DEFAULT_THEME_NOT_EXIST);
}
File defaultDir = new File(SystemConstants.PROD_THEMES_PATH + SystemConstants.FILE_SEPARATOR + "default");
if (defaultDir.exists()) {
FileUtil.del(defaultDir);
}
log.info("准备解压缩默认主题,主题位置:{}", file.getAbsolutePath());
ZipUtil.unzip(file.getAbsoluteFile(), defaultDir.getAbsoluteFile());
}
}
public void initThemeResourceHandle(String themePath) {
if (StringUtils.isBlank(themePath)) {
themePath = optionCacheService.getDefaultValue(OptionEnum.WEB_THEME.getKey(), OptionConstant.OPTION_IDENTIFICATION_SYSTEM, "");
}
if (StringUtils.isBlank(themePath)) {
return;
}
ThemeInfo themeInfo = getThemeInfo(themePath);
if (null == themeInfo) {
Themeutils.unzipDefaultTheme();
themeInfo = getThemeInfo(themePath);
}
if (null == themeInfo) {
LOGGER.error("未找到主题:{}", themePath);
return;
}
LOGGER.info("theminfo :{}", JSONUtil.toJsonStr(themeInfo));
if (StringUtils.isBlank(themeInfo.getType())) {
return;
}
String upperCase = themeInfo.getType().toUpperCase();
if (SystemConstants.THEME_TYPE_NODE.contains(upperCase)) {
addStaticResourceHandler("file:./resources/static/themes/" + themePath + "/static/");
}
}
在项目初始化的方法中调用公共方法 :CommonServiceImpl#initTheme
private void initTheme() {
Themeutils.unzipDefaultTheme();
}