mirror of https://github.com/halo-dev/halo
🎨 代码优化
parent
2c8f43c09b
commit
e61f82c8e3
|
@ -45,12 +45,6 @@ import java.util.*;
|
|||
@Slf4j
|
||||
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<>();
|
||||
|
||||
/**
|
||||
|
@ -251,32 +245,6 @@ public class HaloUtils {
|
|||
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的路径
|
||||
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());
|
||||
if (!mediaPath.exists()) {
|
||||
mediaPath.mkdirs();
|
||||
|
@ -151,8 +151,8 @@ public class AttachmentController {
|
|||
//保存在数据库
|
||||
Attachment attachment = new Attachment();
|
||||
attachment.setAttachName(fileName);
|
||||
attachment.setAttachPath(new StringBuffer("/upload/").append(HaloUtils.YEAR).append("/").append(HaloUtils.MONTH).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.setAttachPath(new StringBuffer("/upload/").append(DateUtil.thisYear()).append("/").append(DateUtil.thisMonth()).append("/").append(fileName).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.setAttachSuffix(new StringBuffer(".").append(fileSuffix).toString());
|
||||
attachment.setAttachCreated(DateUtil.date());
|
||||
|
|
|
@ -12,6 +12,8 @@ import cc.ryanc.halo.utils.HaloUtils;
|
|||
import cc.ryanc.halo.web.controller.core.BaseController;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
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.extra.servlet.ServletUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -27,8 +29,6 @@ import org.springframework.web.multipart.MultipartFile;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.websocket.server.PathParam;
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -184,7 +184,8 @@ public class ThemeController extends BaseController {
|
|||
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());
|
||||
tplContent = HaloUtils.getFileContent(themesPath.getAbsolutePath());
|
||||
FileReader fileReader = new FileReader(themesPath);
|
||||
tplContent = fileReader.readString();
|
||||
} catch (Exception e) {
|
||||
log.error("获取模板文件错误:{}", e.getMessage());
|
||||
}
|
||||
|
@ -210,8 +211,8 @@ public class ThemeController extends BaseController {
|
|||
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());
|
||||
byte[] tplContentByte = tplContent.getBytes("UTF-8");
|
||||
Files.write(Paths.get(tplPath.getAbsolutePath()), tplContentByte);
|
||||
FileWriter fileWriter = new FileWriter(tplPath);
|
||||
fileWriter.write(tplContent);
|
||||
} catch (Exception e) {
|
||||
log.error("模板保存失败:{}", e.getMessage());
|
||||
return new JsonResult(ResultCode.FAIL.getCode(), "模板保存失败!");
|
||||
|
|
Loading…
Reference in New Issue