mirror of https://github.com/halo-dev/halo
🎨 代码优化
parent
2c8f43c09b
commit
e61f82c8e3
|
@ -45,12 +45,6 @@ import java.util.*;
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class HaloUtils {
|
public class HaloUtils {
|
||||||
|
|
||||||
private final static Calendar NOW = Calendar.getInstance();
|
|
||||||
|
|
||||||
public final static String YEAR = NOW.get(Calendar.YEAR) + "";
|
|
||||||
|
|
||||||
public final static String MONTH = (NOW.get(Calendar.MONTH) + 1) + "";
|
|
||||||
|
|
||||||
private static ArrayList<String> FILE_LIST = new ArrayList<>();
|
private static ArrayList<String> FILE_LIST = new ArrayList<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -251,32 +245,6 @@ public class HaloUtils {
|
||||||
return tpls;
|
return tpls;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取文件内容
|
|
||||||
*
|
|
||||||
* @param filePath filePath
|
|
||||||
* @return String
|
|
||||||
*/
|
|
||||||
public static String getFileContent(String filePath) throws IOException {
|
|
||||||
File file = new File(filePath);
|
|
||||||
Long fileLength = file.length();
|
|
||||||
byte[] fileContent = new byte[fileLength.intValue()];
|
|
||||||
FileInputStream inputStream = null;
|
|
||||||
try {
|
|
||||||
inputStream = new FileInputStream(file);
|
|
||||||
inputStream.read(fileContent);
|
|
||||||
inputStream.close();
|
|
||||||
return new String(fileContent, "UTF-8");
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("读取模板文件错误:", e.getMessage());
|
|
||||||
} finally {
|
|
||||||
if (null != inputStream) {
|
|
||||||
inputStream.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前时间
|
* 获取当前时间
|
||||||
*
|
*
|
||||||
|
|
|
@ -134,7 +134,7 @@ public class AttachmentController {
|
||||||
//upload的路径
|
//upload的路径
|
||||||
StringBuffer sbMedia = new StringBuffer("upload/");
|
StringBuffer sbMedia = new StringBuffer("upload/");
|
||||||
//获取当前年月以创建目录,如果没有该目录则创建
|
//获取当前年月以创建目录,如果没有该目录则创建
|
||||||
sbMedia.append(HaloUtils.YEAR).append("/").append(HaloUtils.MONTH).append("/");
|
sbMedia.append(DateUtil.thisYear()).append("/").append(DateUtil.thisMonth()).append("/");
|
||||||
File mediaPath = new File(basePath.getAbsolutePath(), sbMedia.toString());
|
File mediaPath = new File(basePath.getAbsolutePath(), sbMedia.toString());
|
||||||
if (!mediaPath.exists()) {
|
if (!mediaPath.exists()) {
|
||||||
mediaPath.mkdirs();
|
mediaPath.mkdirs();
|
||||||
|
@ -151,8 +151,8 @@ public class AttachmentController {
|
||||||
//保存在数据库
|
//保存在数据库
|
||||||
Attachment attachment = new Attachment();
|
Attachment attachment = new Attachment();
|
||||||
attachment.setAttachName(fileName);
|
attachment.setAttachName(fileName);
|
||||||
attachment.setAttachPath(new StringBuffer("/upload/").append(HaloUtils.YEAR).append("/").append(HaloUtils.MONTH).append("/").append(fileName).toString());
|
attachment.setAttachPath(new StringBuffer("/upload/").append(DateUtil.thisYear()).append("/").append(DateUtil.thisMonth()).append("/").append(fileName).toString());
|
||||||
attachment.setAttachSmallPath(new StringBuffer("/upload/").append(HaloUtils.YEAR).append("/").append(HaloUtils.MONTH).append("/").append(nameWithOutSuffix).append("_small.").append(fileSuffix).toString());
|
attachment.setAttachSmallPath(new StringBuffer("/upload/").append(DateUtil.thisYear()).append("/").append(DateUtil.thisMonth()).append("/").append(nameWithOutSuffix).append("_small.").append(fileSuffix).toString());
|
||||||
attachment.setAttachType(file.getContentType());
|
attachment.setAttachType(file.getContentType());
|
||||||
attachment.setAttachSuffix(new StringBuffer(".").append(fileSuffix).toString());
|
attachment.setAttachSuffix(new StringBuffer(".").append(fileSuffix).toString());
|
||||||
attachment.setAttachCreated(DateUtil.date());
|
attachment.setAttachCreated(DateUtil.date());
|
||||||
|
|
|
@ -12,6 +12,8 @@ import cc.ryanc.halo.utils.HaloUtils;
|
||||||
import cc.ryanc.halo.web.controller.core.BaseController;
|
import cc.ryanc.halo.web.controller.core.BaseController;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.core.io.file.FileReader;
|
||||||
|
import cn.hutool.core.io.file.FileWriter;
|
||||||
import cn.hutool.core.util.ZipUtil;
|
import cn.hutool.core.util.ZipUtil;
|
||||||
import cn.hutool.extra.servlet.ServletUtil;
|
import cn.hutool.extra.servlet.ServletUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
@ -27,8 +29,6 @@ import org.springframework.web.multipart.MultipartFile;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.websocket.server.PathParam;
|
import javax.websocket.server.PathParam;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -184,7 +184,8 @@ public class ThemeController extends BaseController {
|
||||||
File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
|
File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
|
||||||
//获取主题路径
|
//获取主题路径
|
||||||
File themesPath = new File(basePath.getAbsolutePath(), new StringBuffer("templates/themes/").append(BaseController.THEME).append("/").append(tplName).toString());
|
File themesPath = new File(basePath.getAbsolutePath(), new StringBuffer("templates/themes/").append(BaseController.THEME).append("/").append(tplName).toString());
|
||||||
tplContent = HaloUtils.getFileContent(themesPath.getAbsolutePath());
|
FileReader fileReader = new FileReader(themesPath);
|
||||||
|
tplContent = fileReader.readString();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("获取模板文件错误:{}", e.getMessage());
|
log.error("获取模板文件错误:{}", e.getMessage());
|
||||||
}
|
}
|
||||||
|
@ -210,8 +211,8 @@ public class ThemeController extends BaseController {
|
||||||
File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
|
File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
|
||||||
//获取主题路径
|
//获取主题路径
|
||||||
File tplPath = new File(basePath.getAbsolutePath(), new StringBuffer("templates/themes/").append(BaseController.THEME).append("/").append(tplName).toString());
|
File tplPath = new File(basePath.getAbsolutePath(), new StringBuffer("templates/themes/").append(BaseController.THEME).append("/").append(tplName).toString());
|
||||||
byte[] tplContentByte = tplContent.getBytes("UTF-8");
|
FileWriter fileWriter = new FileWriter(tplPath);
|
||||||
Files.write(Paths.get(tplPath.getAbsolutePath()), tplContentByte);
|
fileWriter.write(tplContent);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("模板保存失败:{}", e.getMessage());
|
log.error("模板保存失败:{}", e.getMessage());
|
||||||
return new JsonResult(ResultCode.FAIL.getCode(), "模板保存失败!");
|
return new JsonResult(ResultCode.FAIL.getCode(), "模板保存失败!");
|
||||||
|
|
Loading…
Reference in New Issue