mirror of https://github.com/halo-dev/halo
🎨 代码优化
parent
2998da98e4
commit
0242fcea37
|
@ -4,6 +4,7 @@ import cc.ryanc.halo.model.domain.Logs;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@ -20,17 +21,11 @@ public interface LogsService {
|
||||||
/**
|
/**
|
||||||
* 保存日志
|
* 保存日志
|
||||||
*
|
*
|
||||||
* @param logs logs
|
* @param logTitle logTitle
|
||||||
* @return Logs
|
* @param logContent logContent
|
||||||
|
* @param request request
|
||||||
*/
|
*/
|
||||||
Logs saveByLogs(Logs logs);
|
void save(String logTitle, String logContent, HttpServletRequest request);
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据编号移除
|
|
||||||
*
|
|
||||||
* @param logsId logsId
|
|
||||||
*/
|
|
||||||
void removeByLogsId(Long logsId);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 移除所有日志
|
* 移除所有日志
|
||||||
|
|
|
@ -223,7 +223,6 @@ public class AttachmentServiceImpl implements AttachmentService {
|
||||||
fileSmallPath.append("_small.");
|
fileSmallPath.append("_small.");
|
||||||
fileSmallPath.append(fileSuffix);
|
fileSmallPath.append(fileSuffix);
|
||||||
|
|
||||||
|
|
||||||
String size = HaloUtils.parseSize(new File(fullPath.toString()).length());
|
String size = HaloUtils.parseSize(new File(fullPath.toString()).length());
|
||||||
String wh = HaloUtils.getImageWh(new File(fullPath.toString()));
|
String wh = HaloUtils.getImageWh(new File(fullPath.toString()));
|
||||||
|
|
||||||
|
@ -258,7 +257,7 @@ public class AttachmentServiceImpl implements AttachmentService {
|
||||||
String domain = HaloConst.OPTIONS.get("qiniu_domain");
|
String domain = HaloConst.OPTIONS.get("qiniu_domain");
|
||||||
String bucket = HaloConst.OPTIONS.get("qiniu_bucket");
|
String bucket = HaloConst.OPTIONS.get("qiniu_bucket");
|
||||||
String smallUrl = HaloConst.OPTIONS.get("qiniu_small_url");
|
String smallUrl = HaloConst.OPTIONS.get("qiniu_small_url");
|
||||||
if (accessKey == null || secretKey == null || domain == null || bucket == null) {
|
if (StrUtil.isEmpty(accessKey) || StrUtil.isEmpty(secretKey) || StrUtil.isEmpty(domain) || StrUtil.isEmpty(bucket)) {
|
||||||
return resultMap;
|
return resultMap;
|
||||||
}
|
}
|
||||||
Auth auth = Auth.create(accessKey, secretKey);
|
Auth auth = Auth.create(accessKey, secretKey);
|
||||||
|
@ -318,7 +317,7 @@ public class AttachmentServiceImpl implements AttachmentService {
|
||||||
String domain = HaloConst.OPTIONS.get("upyun_oss_domain");
|
String domain = HaloConst.OPTIONS.get("upyun_oss_domain");
|
||||||
String operator = HaloConst.OPTIONS.get("upyun_oss_operator");
|
String operator = HaloConst.OPTIONS.get("upyun_oss_operator");
|
||||||
String smallUrl = HaloConst.OPTIONS.get("upyun_oss_small");
|
String smallUrl = HaloConst.OPTIONS.get("upyun_oss_small");
|
||||||
if (ossSrc == null || ossPwd == null || domain == null || bucket == null || operator == null) {
|
if (StrUtil.isEmpty(ossSrc) || StrUtil.isEmpty(ossPwd) || StrUtil.isEmpty(domain) || StrUtil.isEmpty(bucket) || StrUtil.isEmpty(operator)) {
|
||||||
return resultMap;
|
return resultMap;
|
||||||
}
|
}
|
||||||
String fileName = file.getOriginalFilename();
|
String fileName = file.getOriginalFilename();
|
||||||
|
@ -363,7 +362,7 @@ public class AttachmentServiceImpl implements AttachmentService {
|
||||||
String accessKey = HaloConst.OPTIONS.get("qiniu_access_key");
|
String accessKey = HaloConst.OPTIONS.get("qiniu_access_key");
|
||||||
String secretKey = HaloConst.OPTIONS.get("qiniu_secret_key");
|
String secretKey = HaloConst.OPTIONS.get("qiniu_secret_key");
|
||||||
String bucket = HaloConst.OPTIONS.get("qiniu_bucket");
|
String bucket = HaloConst.OPTIONS.get("qiniu_bucket");
|
||||||
if (accessKey == null || secretKey == null || bucket == null) {
|
if (StrUtil.isEmpty(accessKey) || StrUtil.isEmpty(secretKey) || StrUtil.isEmpty(bucket)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Auth auth = Auth.create(accessKey, secretKey);
|
Auth auth = Auth.create(accessKey, secretKey);
|
||||||
|
@ -391,7 +390,7 @@ public class AttachmentServiceImpl implements AttachmentService {
|
||||||
String ossPwd = HaloConst.OPTIONS.get("upyun_oss_pwd");
|
String ossPwd = HaloConst.OPTIONS.get("upyun_oss_pwd");
|
||||||
String bucket = HaloConst.OPTIONS.get("upyun_oss_bucket");
|
String bucket = HaloConst.OPTIONS.get("upyun_oss_bucket");
|
||||||
String operator = HaloConst.OPTIONS.get("upyun_oss_operator");
|
String operator = HaloConst.OPTIONS.get("upyun_oss_operator");
|
||||||
if (ossSrc == null || ossPwd == null || bucket == null || operator == null) {
|
if (StrUtil.isEmpty(ossSrc) || StrUtil.isEmpty(ossPwd) || StrUtil.isEmpty(bucket) || StrUtil.isEmpty(operator)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
UpYun upYun = new UpYun(bucket, operator, ossPwd);
|
UpYun upYun = new UpYun(bucket, operator, ossPwd);
|
||||||
|
|
|
@ -3,11 +3,14 @@ package cc.ryanc.halo.service.impl;
|
||||||
import cc.ryanc.halo.model.domain.Logs;
|
import cc.ryanc.halo.model.domain.Logs;
|
||||||
import cc.ryanc.halo.repository.LogsRepository;
|
import cc.ryanc.halo.repository.LogsRepository;
|
||||||
import cc.ryanc.halo.service.LogsService;
|
import cc.ryanc.halo.service.LogsService;
|
||||||
|
import cn.hutool.extra.servlet.ServletUtil;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@ -28,23 +31,18 @@ public class LogsServiceImpl implements LogsService {
|
||||||
/**
|
/**
|
||||||
* 保存日志
|
* 保存日志
|
||||||
*
|
*
|
||||||
* @param logs logs
|
* @param logTitle logTitle
|
||||||
* @return Logs
|
* @param logContent logContent
|
||||||
|
* @param request request
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Logs saveByLogs(Logs logs) {
|
public void save(String logTitle, String logContent, HttpServletRequest request) {
|
||||||
return logsRepository.save(logs);
|
Logs logs = new Logs();
|
||||||
}
|
logs.setLogTitle(logTitle);
|
||||||
|
logs.setLogContent(logContent);
|
||||||
/**
|
logs.setLogCreated(new Date());
|
||||||
* 根据编号移除
|
logs.setLogIp(ServletUtil.getClientIP(request));
|
||||||
*
|
logsRepository.save(logs);
|
||||||
* @param logsId logsId
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void removeByLogsId(Long logsId) {
|
|
||||||
Optional<Logs> logs = this.findLogsByLogsId(logsId);
|
|
||||||
logsRepository.delete(logs.get());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -20,7 +20,6 @@ import cn.hutool.core.lang.Validator;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.crypto.SecureUtil;
|
import cn.hutool.crypto.SecureUtil;
|
||||||
import cn.hutool.extra.servlet.ServletUtil;
|
|
||||||
import cn.hutool.http.HtmlUtil;
|
import cn.hutool.http.HtmlUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -160,7 +159,7 @@ public class AdminController extends BaseController {
|
||||||
session.setAttribute(HaloConst.USER_SESSION_KEY, aUser);
|
session.setAttribute(HaloConst.USER_SESSION_KEY, aUser);
|
||||||
//重置用户的登录状态为正常
|
//重置用户的登录状态为正常
|
||||||
userService.updateUserNormal();
|
userService.updateUserNormal();
|
||||||
logsService.saveByLogs(new Logs(LogsRecord.LOGIN, LogsRecord.LOGIN_SUCCESS, ServletUtil.getClientIP(request), DateUtil.date()));
|
logsService.save(LogsRecord.LOGIN, LogsRecord.LOGIN_SUCCESS, request);
|
||||||
log.info("User {} login succeeded.", aUser.getUserDisplayName());
|
log.info("User {} login succeeded.", aUser.getUserDisplayName());
|
||||||
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.login.success"));
|
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.login.success"));
|
||||||
} else {
|
} else {
|
||||||
|
@ -170,14 +169,7 @@ public class AdminController extends BaseController {
|
||||||
if (errorCount >= CommonParamsEnum.FIVE.getValue()) {
|
if (errorCount >= CommonParamsEnum.FIVE.getValue()) {
|
||||||
userService.updateUserLoginEnable(TrueFalseEnum.FALSE.getDesc());
|
userService.updateUserLoginEnable(TrueFalseEnum.FALSE.getDesc());
|
||||||
}
|
}
|
||||||
logsService.saveByLogs(
|
logsService.save(LogsRecord.LOGIN, LogsRecord.LOGIN_ERROR + "[" + HtmlUtil.escape(loginName) + "," + HtmlUtil.escape(loginPwd) + "]", request);
|
||||||
new Logs(
|
|
||||||
LogsRecord.LOGIN,
|
|
||||||
LogsRecord.LOGIN_ERROR + "[" + HtmlUtil.escape(loginName) + "," + HtmlUtil.escape(loginPwd) + "]",
|
|
||||||
ServletUtil.getClientIP(request),
|
|
||||||
DateUtil.date()
|
|
||||||
)
|
|
||||||
);
|
|
||||||
Object[] args = {(5 - errorCount)};
|
Object[] args = {(5 - errorCount)};
|
||||||
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.login.failed", args));
|
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.login.failed", args));
|
||||||
}
|
}
|
||||||
|
@ -193,7 +185,7 @@ public class AdminController extends BaseController {
|
||||||
public String logOut(HttpSession session) {
|
public String logOut(HttpSession session) {
|
||||||
User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY);
|
User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY);
|
||||||
session.removeAttribute(HaloConst.USER_SESSION_KEY);
|
session.removeAttribute(HaloConst.USER_SESSION_KEY);
|
||||||
logsService.saveByLogs(new Logs(LogsRecord.LOGOUT, user.getUserName(), ServletUtil.getClientIP(request), DateUtil.date()));
|
logsService.save(LogsRecord.LOGOUT, user.getUserName(), request);
|
||||||
log.info("User {} has logged out", user.getUserName());
|
log.info("User {} has logged out", user.getUserName());
|
||||||
return "redirect:/admin/login";
|
return "redirect:/admin/login";
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package cc.ryanc.halo.web.controller.admin;
|
package cc.ryanc.halo.web.controller.admin;
|
||||||
|
|
||||||
import cc.ryanc.halo.model.domain.Attachment;
|
import cc.ryanc.halo.model.domain.Attachment;
|
||||||
import cc.ryanc.halo.model.domain.Logs;
|
|
||||||
import cc.ryanc.halo.model.dto.JsonResult;
|
import cc.ryanc.halo.model.dto.JsonResult;
|
||||||
import cc.ryanc.halo.model.dto.LogsRecord;
|
import cc.ryanc.halo.model.dto.LogsRecord;
|
||||||
import cc.ryanc.halo.model.enums.PostTypeEnum;
|
import cc.ryanc.halo.model.enums.PostTypeEnum;
|
||||||
|
@ -11,7 +10,6 @@ import cc.ryanc.halo.service.LogsService;
|
||||||
import cc.ryanc.halo.utils.LocaleMessageUtil;
|
import cc.ryanc.halo.utils.LocaleMessageUtil;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.extra.servlet.ServletUtil;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
|
@ -116,13 +114,12 @@ public class AttachmentController {
|
||||||
public Map<String, Object> upload(@RequestParam("file") MultipartFile file,
|
public Map<String, Object> upload(@RequestParam("file") MultipartFile file,
|
||||||
HttpServletRequest request) {
|
HttpServletRequest request) {
|
||||||
Map<String, Object> result = new HashMap<>(3);
|
Map<String, Object> result = new HashMap<>(3);
|
||||||
System.out.println("源地址" + file.getOriginalFilename() + "类型" + file.getContentType() + "文件名" + file.getName() + "文件大小" + file.getSize());
|
|
||||||
if (!file.isEmpty()) {
|
if (!file.isEmpty()) {
|
||||||
try {
|
try {
|
||||||
Map<String, String> resultMap = attachmentService.upload(file, request);
|
Map<String, String> resultMap = attachmentService.upload(file, request);
|
||||||
if (resultMap == null || resultMap.isEmpty()) {
|
if (resultMap == null || resultMap.isEmpty()) {
|
||||||
log.error("文件上传失败");
|
log.error("File upload failed");
|
||||||
result.put("success", 0);
|
result.put("success", ResultCodeEnum.FAIL.getCode());
|
||||||
result.put("message", localeMessageUtil.getMessage("code.admin.attachment.upload-failed"));
|
result.put("message", localeMessageUtil.getMessage("code.admin.attachment.upload-failed"));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -139,16 +136,14 @@ public class AttachmentController {
|
||||||
attachment.setAttachLocation(resultMap.get("location"));
|
attachment.setAttachLocation(resultMap.get("location"));
|
||||||
attachmentService.saveByAttachment(attachment);
|
attachmentService.saveByAttachment(attachment);
|
||||||
log.info("Upload file {} to {} successfully", resultMap.get("fileName"), resultMap.get("filePath"));
|
log.info("Upload file {} to {} successfully", resultMap.get("fileName"), resultMap.get("filePath"));
|
||||||
logsService.saveByLogs(
|
result.put("success", ResultCodeEnum.SUCCESS.getCode());
|
||||||
new Logs(LogsRecord.UPLOAD_FILE, resultMap.get("fileName"), ServletUtil.getClientIP(request), DateUtil.date())
|
|
||||||
);
|
|
||||||
result.put("success", 1);
|
|
||||||
result.put("message", localeMessageUtil.getMessage("code.admin.attachment.upload-success"));
|
result.put("message", localeMessageUtil.getMessage("code.admin.attachment.upload-success"));
|
||||||
result.put("url", attachment.getAttachPath());
|
result.put("url", attachment.getAttachPath());
|
||||||
result.put("filename", resultMap.get("filePath"));
|
result.put("filename", resultMap.get("filePath"));
|
||||||
|
logsService.save(LogsRecord.UPLOAD_FILE, resultMap.get("fileName"), request);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Upload file failed:{}", e.getMessage());
|
log.error("Upload file failed:{}", e.getMessage());
|
||||||
result.put("success", 0);
|
result.put("success", ResultCodeEnum.FAIL.getCode());
|
||||||
result.put("message", localeMessageUtil.getMessage("code.admin.attachment.upload-failed"));
|
result.put("message", localeMessageUtil.getMessage("code.admin.attachment.upload-failed"));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -216,9 +211,7 @@ public class AttachmentController {
|
||||||
}
|
}
|
||||||
if (flag) {
|
if (flag) {
|
||||||
log.info("Delete file {} successfully!", delFileName);
|
log.info("Delete file {} successfully!", delFileName);
|
||||||
logsService.saveByLogs(
|
logsService.save(LogsRecord.REMOVE_FILE, delFileName, request);
|
||||||
new Logs(LogsRecord.REMOVE_FILE, delFileName, ServletUtil.getClientIP(request), DateUtil.date())
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
log.error("Deleting attachment {} failed!", delFileName);
|
log.error("Deleting attachment {} failed!", delFileName);
|
||||||
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.delete-failed"));
|
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.delete-failed"));
|
||||||
|
|
|
@ -1,283 +1,283 @@
|
||||||
package cc.ryanc.halo.web.controller.admin;
|
package cc.ryanc.halo.web.controller.admin;
|
||||||
|
|
||||||
import cc.ryanc.halo.model.domain.Comment;
|
import cc.ryanc.halo.model.domain.Comment;
|
||||||
import cc.ryanc.halo.model.domain.Post;
|
import cc.ryanc.halo.model.domain.Post;
|
||||||
import cc.ryanc.halo.model.domain.User;
|
import cc.ryanc.halo.model.domain.User;
|
||||||
import cc.ryanc.halo.model.dto.HaloConst;
|
import cc.ryanc.halo.model.dto.HaloConst;
|
||||||
import cc.ryanc.halo.model.dto.JsonResult;
|
import cc.ryanc.halo.model.dto.JsonResult;
|
||||||
import cc.ryanc.halo.model.enums.*;
|
import cc.ryanc.halo.model.enums.*;
|
||||||
import cc.ryanc.halo.service.CommentService;
|
import cc.ryanc.halo.service.CommentService;
|
||||||
import cc.ryanc.halo.service.MailService;
|
import cc.ryanc.halo.service.MailService;
|
||||||
import cc.ryanc.halo.service.PostService;
|
import cc.ryanc.halo.service.PostService;
|
||||||
import cc.ryanc.halo.utils.OwoUtil;
|
import cc.ryanc.halo.utils.OwoUtil;
|
||||||
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.lang.Validator;
|
import cn.hutool.core.lang.Validator;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.crypto.SecureUtil;
|
import cn.hutool.crypto.SecureUtil;
|
||||||
import cn.hutool.extra.servlet.ServletUtil;
|
import cn.hutool.extra.servlet.ServletUtil;
|
||||||
import cn.hutool.http.HtmlUtil;
|
import cn.hutool.http.HtmlUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
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;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
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.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpSession;
|
import javax.servlet.http.HttpSession;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <pre>
|
* <pre>
|
||||||
* 后台评论管理控制器
|
* 后台评论管理控制器
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @author : RYAN0UP
|
* @author : RYAN0UP
|
||||||
* @date : 2017/12/10
|
* @date : 2017/12/10
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping(value = "/admin/comments")
|
@RequestMapping(value = "/admin/comments")
|
||||||
public class CommentController extends BaseController {
|
public class CommentController extends BaseController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private CommentService commentService;
|
private CommentService commentService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MailService mailService;
|
private MailService mailService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private PostService postService;
|
private PostService postService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 渲染评论管理页面
|
* 渲染评论管理页面
|
||||||
*
|
*
|
||||||
* @param model model
|
* @param model model
|
||||||
* @param status status 评论状态
|
* @param status status 评论状态
|
||||||
* @param page page 当前页码
|
* @param page page 当前页码
|
||||||
* @param size size 每页显示条数
|
* @param size size 每页显示条数
|
||||||
* @return 模板路径admin/admin_comment
|
* @return 模板路径admin/admin_comment
|
||||||
*/
|
*/
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public String comments(Model model,
|
public String comments(Model model,
|
||||||
@RequestParam(value = "status", defaultValue = "0") Integer status,
|
@RequestParam(value = "status", defaultValue = "0") Integer status,
|
||||||
@RequestParam(value = "page", defaultValue = "0") Integer page,
|
@RequestParam(value = "page", defaultValue = "0") Integer page,
|
||||||
@RequestParam(value = "size", defaultValue = "10") Integer size) {
|
@RequestParam(value = "size", defaultValue = "10") Integer size) {
|
||||||
Sort sort = new Sort(Sort.Direction.DESC, "commentDate");
|
Sort sort = new Sort(Sort.Direction.DESC, "commentDate");
|
||||||
Pageable pageable = PageRequest.of(page, size, sort);
|
Pageable pageable = PageRequest.of(page, size, sort);
|
||||||
Page<Comment> comments = commentService.findAllComments(status, pageable);
|
Page<Comment> comments = commentService.findAllComments(status, pageable);
|
||||||
model.addAttribute("comments", comments);
|
model.addAttribute("comments", comments);
|
||||||
model.addAttribute("publicCount", commentService.getCountByStatus(CommentStatusEnum.PUBLISHED.getCode()));
|
model.addAttribute("publicCount", commentService.getCountByStatus(CommentStatusEnum.PUBLISHED.getCode()));
|
||||||
model.addAttribute("checkCount", commentService.getCountByStatus(CommentStatusEnum.CHECKING.getCode()));
|
model.addAttribute("checkCount", commentService.getCountByStatus(CommentStatusEnum.CHECKING.getCode()));
|
||||||
model.addAttribute("trashCount", commentService.getCountByStatus(CommentStatusEnum.RECYCLE.getCode()));
|
model.addAttribute("trashCount", commentService.getCountByStatus(CommentStatusEnum.RECYCLE.getCode()));
|
||||||
model.addAttribute("status", status);
|
model.addAttribute("status", status);
|
||||||
return "admin/admin_comment";
|
return "admin/admin_comment";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将评论移到回收站
|
* 将评论移到回收站
|
||||||
*
|
*
|
||||||
* @param commentId 评论编号
|
* @param commentId 评论编号
|
||||||
* @param status 评论状态
|
* @param status 评论状态
|
||||||
* @return 重定向到/admin/comments
|
* @return 重定向到/admin/comments
|
||||||
*/
|
*/
|
||||||
@GetMapping(value = "/throw")
|
@GetMapping(value = "/throw")
|
||||||
public String moveToTrash(@RequestParam("commentId") Long commentId,
|
public String moveToTrash(@RequestParam("commentId") Long commentId,
|
||||||
@RequestParam("status") String status,
|
@RequestParam("status") String status,
|
||||||
@RequestParam(value = "page", defaultValue = "0") Integer page) {
|
@RequestParam(value = "page", defaultValue = "0") Integer page) {
|
||||||
try {
|
try {
|
||||||
commentService.updateCommentStatus(commentId, CommentStatusEnum.RECYCLE.getCode());
|
commentService.updateCommentStatus(commentId, CommentStatusEnum.RECYCLE.getCode());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Delete comment failed: {}", e.getMessage());
|
log.error("Delete comment failed: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
return "redirect:/admin/comments?status=" + status + "&page=" + page;
|
return "redirect:/admin/comments?status=" + status + "&page=" + page;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将评论改变为发布状态
|
* 将评论改变为发布状态
|
||||||
*
|
*
|
||||||
* @param commentId 评论编号
|
* @param commentId 评论编号
|
||||||
* @param status 评论状态
|
* @param status 评论状态
|
||||||
* @param session session
|
* @param session session
|
||||||
* @return 重定向到/admin/comments
|
* @return 重定向到/admin/comments
|
||||||
*/
|
*/
|
||||||
@GetMapping(value = "/revert")
|
@GetMapping(value = "/revert")
|
||||||
public String moveToPublish(@RequestParam("commentId") Long commentId,
|
public String moveToPublish(@RequestParam("commentId") Long commentId,
|
||||||
@RequestParam("status") Integer status,
|
@RequestParam("status") Integer status,
|
||||||
HttpSession session) {
|
HttpSession session) {
|
||||||
Comment comment = commentService.updateCommentStatus(commentId, CommentStatusEnum.PUBLISHED.getCode());
|
Comment comment = commentService.updateCommentStatus(commentId, CommentStatusEnum.PUBLISHED.getCode());
|
||||||
Post post = comment.getPost();
|
Post post = comment.getPost();
|
||||||
User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY);
|
User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY);
|
||||||
|
|
||||||
//判断是否启用邮件服务
|
//判断是否启用邮件服务
|
||||||
new NoticeToAuthor(comment, post, user, status).start();
|
new NoticeToAuthor(comment, post, user, status).start();
|
||||||
return "redirect:/admin/comments?status=" + status;
|
return "redirect:/admin/comments?status=" + status;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除评论
|
* 删除评论
|
||||||
*
|
*
|
||||||
* @param commentId commentId 评论编号
|
* @param commentId commentId 评论编号
|
||||||
* @param status status 评论状态
|
* @param status status 评论状态
|
||||||
* @param page 当前页码
|
* @param page 当前页码
|
||||||
* @return string 重定向到/admin/comments
|
* @return string 重定向到/admin/comments
|
||||||
*/
|
*/
|
||||||
@GetMapping(value = "/remove")
|
@GetMapping(value = "/remove")
|
||||||
public String moveToAway(@RequestParam("commentId") Long commentId,
|
public String moveToAway(@RequestParam("commentId") Long commentId,
|
||||||
@RequestParam("status") Integer status,
|
@RequestParam("status") Integer status,
|
||||||
@RequestParam(value = "page", defaultValue = "0") Integer page) {
|
@RequestParam(value = "page", defaultValue = "0") Integer page) {
|
||||||
try {
|
try {
|
||||||
commentService.removeByCommentId(commentId);
|
commentService.removeByCommentId(commentId);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Delete comment failed: {}", e.getMessage());
|
log.error("Delete comment failed: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
return "redirect:/admin/comments?status=" + status + "&page=" + page;
|
return "redirect:/admin/comments?status=" + status + "&page=" + page;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 管理员回复评论
|
* 管理员回复评论
|
||||||
*
|
*
|
||||||
* @param commentId 被回复的评论
|
* @param commentId 被回复的评论
|
||||||
* @param commentContent 回复的内容
|
* @param commentContent 回复的内容
|
||||||
* @return JsonResult
|
* @return JsonResult
|
||||||
*/
|
*/
|
||||||
@PostMapping(value = "/reply")
|
@PostMapping(value = "/reply")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public JsonResult replyComment(@RequestParam("commentId") Long commentId,
|
public JsonResult replyComment(@RequestParam("commentId") Long commentId,
|
||||||
@RequestParam("postId") Long postId,
|
@RequestParam("postId") Long postId,
|
||||||
@RequestParam("commentContent") String commentContent,
|
@RequestParam("commentContent") String commentContent,
|
||||||
@RequestParam("userAgent") String userAgent,
|
@RequestParam("userAgent") String userAgent,
|
||||||
HttpServletRequest request,
|
HttpServletRequest request,
|
||||||
HttpSession session) {
|
HttpSession session) {
|
||||||
try {
|
try {
|
||||||
Post post = postService.findByPostId(postId).orElse(new Post());
|
Post post = postService.findByPostId(postId).orElse(new Post());
|
||||||
|
|
||||||
//博主信息
|
//博主信息
|
||||||
User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY);
|
User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY);
|
||||||
|
|
||||||
//被回复的评论
|
//被回复的评论
|
||||||
Comment lastComment = commentService.findCommentById(commentId).orElse(new Comment());
|
Comment lastComment = commentService.findCommentById(commentId).orElse(new Comment());
|
||||||
|
|
||||||
//修改被回复的评论的状态
|
//修改被回复的评论的状态
|
||||||
lastComment.setCommentStatus(CommentStatusEnum.PUBLISHED.getCode());
|
lastComment.setCommentStatus(CommentStatusEnum.PUBLISHED.getCode());
|
||||||
commentService.saveByComment(lastComment);
|
commentService.saveByComment(lastComment);
|
||||||
|
|
||||||
//保存评论
|
//保存评论
|
||||||
Comment comment = new Comment();
|
Comment comment = new Comment();
|
||||||
comment.setPost(post);
|
comment.setPost(post);
|
||||||
comment.setCommentAuthor(user.getUserDisplayName());
|
comment.setCommentAuthor(user.getUserDisplayName());
|
||||||
comment.setCommentAuthorEmail(user.getUserEmail());
|
comment.setCommentAuthorEmail(user.getUserEmail());
|
||||||
comment.setCommentAuthorUrl(HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
|
comment.setCommentAuthorUrl(HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
|
||||||
comment.setCommentAuthorIp(ServletUtil.getClientIP(request));
|
comment.setCommentAuthorIp(ServletUtil.getClientIP(request));
|
||||||
comment.setCommentAuthorAvatarMd5(SecureUtil.md5(user.getUserEmail()));
|
comment.setCommentAuthorAvatarMd5(SecureUtil.md5(user.getUserEmail()));
|
||||||
comment.setCommentDate(DateUtil.date());
|
comment.setCommentDate(DateUtil.date());
|
||||||
String lastContent = "<a href='#comment-id-" + lastComment.getCommentId() + "'>@" + lastComment.getCommentAuthor() + "</a> ";
|
String lastContent = "<a href='#comment-id-" + lastComment.getCommentId() + "'>@" + lastComment.getCommentAuthor() + "</a> ";
|
||||||
comment.setCommentContent(lastContent + OwoUtil.markToImg(HtmlUtil.escape(commentContent).replace("<br/>", "<br/>")));
|
comment.setCommentContent(lastContent + OwoUtil.markToImg(HtmlUtil.escape(commentContent).replace("<br/>", "<br/>")));
|
||||||
comment.setCommentAgent(userAgent);
|
comment.setCommentAgent(userAgent);
|
||||||
comment.setCommentParent(commentId);
|
comment.setCommentParent(commentId);
|
||||||
comment.setCommentStatus(CommentStatusEnum.PUBLISHED.getCode());
|
comment.setCommentStatus(CommentStatusEnum.PUBLISHED.getCode());
|
||||||
comment.setIsAdmin(1);
|
comment.setIsAdmin(1);
|
||||||
commentService.saveByComment(comment);
|
commentService.saveByComment(comment);
|
||||||
|
|
||||||
//邮件通知
|
//邮件通知
|
||||||
new EmailToAuthor(comment, lastComment, post, user, commentContent).start();
|
new EmailToAuthor(comment, lastComment, post, user, commentContent).start();
|
||||||
return new JsonResult(ResultCodeEnum.SUCCESS.getCode());
|
return new JsonResult(ResultCodeEnum.SUCCESS.getCode());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Reply to comment failed: {}", e.getMessage());
|
log.error("Reply to comment failed: {}", e.getMessage());
|
||||||
return new JsonResult(ResultCodeEnum.FAIL.getCode());
|
return new JsonResult(ResultCodeEnum.FAIL.getCode());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 异步发送邮件回复给评论者
|
* 异步发送邮件回复给评论者
|
||||||
*/
|
*/
|
||||||
class EmailToAuthor extends Thread {
|
class EmailToAuthor extends Thread {
|
||||||
|
|
||||||
private Comment comment;
|
private Comment comment;
|
||||||
private Comment lastComment;
|
private Comment lastComment;
|
||||||
private Post post;
|
private Post post;
|
||||||
private User user;
|
private User user;
|
||||||
private String commentContent;
|
private String commentContent;
|
||||||
|
|
||||||
private EmailToAuthor(Comment comment, Comment lastComment, Post post, User user, String commentContent) {
|
private EmailToAuthor(Comment comment, Comment lastComment, Post post, User user, String commentContent) {
|
||||||
this.comment = comment;
|
this.comment = comment;
|
||||||
this.lastComment = lastComment;
|
this.lastComment = lastComment;
|
||||||
this.post = post;
|
this.post = post;
|
||||||
this.user = user;
|
this.user = user;
|
||||||
this.commentContent = commentContent;
|
this.commentContent = commentContent;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.SMTP_EMAIL_ENABLE.getProp()), TrueFalseEnum.TRUE.getDesc()) && StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.COMMENT_REPLY_NOTICE.getProp()), TrueFalseEnum.TRUE.getDesc())) {
|
if (StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.SMTP_EMAIL_ENABLE.getProp()), TrueFalseEnum.TRUE.getDesc()) && StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.COMMENT_REPLY_NOTICE.getProp()), TrueFalseEnum.TRUE.getDesc())) {
|
||||||
if (Validator.isEmail(lastComment.getCommentAuthorEmail())) {
|
if (Validator.isEmail(lastComment.getCommentAuthorEmail())) {
|
||||||
Map<String, Object> map = new HashMap<>(8);
|
Map<String, Object> map = new HashMap<>(8);
|
||||||
map.put("blogTitle", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_TITLE.getProp()));
|
map.put("blogTitle", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_TITLE.getProp()));
|
||||||
map.put("commentAuthor", lastComment.getCommentAuthor());
|
map.put("commentAuthor", lastComment.getCommentAuthor());
|
||||||
map.put("pageName", lastComment.getPost().getPostTitle());
|
map.put("pageName", lastComment.getPost().getPostTitle());
|
||||||
if (StrUtil.equals(post.getPostType(), PostTypeEnum.POST_TYPE_POST.getDesc())) {
|
if (StrUtil.equals(post.getPostType(), PostTypeEnum.POST_TYPE_POST.getDesc())) {
|
||||||
map.put("pageUrl", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "/archives/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId());
|
map.put("pageUrl", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "/archives/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId());
|
||||||
} else {
|
} else {
|
||||||
map.put("pageUrl", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "/p/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId());
|
map.put("pageUrl", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "/p/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId());
|
||||||
}
|
}
|
||||||
map.put("commentContent", lastComment.getCommentContent());
|
map.put("commentContent", lastComment.getCommentContent());
|
||||||
map.put("replyAuthor", user.getUserDisplayName());
|
map.put("replyAuthor", user.getUserDisplayName());
|
||||||
map.put("replyContent", commentContent);
|
map.put("replyContent", commentContent);
|
||||||
map.put("blogUrl", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
|
map.put("blogUrl", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
|
||||||
mailService.sendTemplateMail(
|
mailService.sendTemplateMail(
|
||||||
lastComment.getCommentAuthorEmail(), "您在" + HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "的评论有了新回复", map, "common/mail_template/mail_reply.ftl");
|
lastComment.getCommentAuthorEmail(), "您在" + HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "的评论有了新回复", map, "common/mail_template/mail_reply.ftl");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 异步通知评论者审核通过
|
* 异步通知评论者审核通过
|
||||||
*/
|
*/
|
||||||
class NoticeToAuthor extends Thread {
|
class NoticeToAuthor extends Thread {
|
||||||
|
|
||||||
private Comment comment;
|
private Comment comment;
|
||||||
private Post post;
|
private Post post;
|
||||||
private User user;
|
private User user;
|
||||||
private Integer status;
|
private Integer status;
|
||||||
|
|
||||||
private NoticeToAuthor(Comment comment, Post post, User user, Integer status) {
|
private NoticeToAuthor(Comment comment, Post post, User user, Integer status) {
|
||||||
this.comment = comment;
|
this.comment = comment;
|
||||||
this.post = post;
|
this.post = post;
|
||||||
this.user = user;
|
this.user = user;
|
||||||
this.status = status;
|
this.status = status;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.SMTP_EMAIL_ENABLE.getProp()), TrueFalseEnum.TRUE.getDesc()) && StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.COMMENT_REPLY_NOTICE.getProp()), TrueFalseEnum.TRUE.getDesc())) {
|
if (StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.SMTP_EMAIL_ENABLE.getProp()), TrueFalseEnum.TRUE.getDesc()) && StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.COMMENT_REPLY_NOTICE.getProp()), TrueFalseEnum.TRUE.getDesc())) {
|
||||||
try {
|
try {
|
||||||
if (status == 1 && Validator.isEmail(comment.getCommentAuthorEmail())) {
|
if (status == 1 && Validator.isEmail(comment.getCommentAuthorEmail())) {
|
||||||
Map<String, Object> map = new HashMap<>(6);
|
Map<String, Object> map = new HashMap<>(6);
|
||||||
if (StrUtil.equals(post.getPostType(), PostTypeEnum.POST_TYPE_POST.getDesc())) {
|
if (StrUtil.equals(post.getPostType(), PostTypeEnum.POST_TYPE_POST.getDesc())) {
|
||||||
map.put("pageUrl", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "/archives/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId());
|
map.put("pageUrl", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "/archives/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId());
|
||||||
} else {
|
} else {
|
||||||
map.put("pageUrl", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "/p/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId());
|
map.put("pageUrl", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "/p/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId());
|
||||||
}
|
}
|
||||||
map.put("pageName", post.getPostTitle());
|
map.put("pageName", post.getPostTitle());
|
||||||
map.put("commentContent", comment.getCommentContent());
|
map.put("commentContent", comment.getCommentContent());
|
||||||
map.put("blogUrl", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
|
map.put("blogUrl", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
|
||||||
map.put("blogTitle", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_TITLE.getProp()));
|
map.put("blogTitle", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_TITLE.getProp()));
|
||||||
map.put("author", user.getUserDisplayName());
|
map.put("author", user.getUserDisplayName());
|
||||||
mailService.sendTemplateMail(
|
mailService.sendTemplateMail(
|
||||||
comment.getCommentAuthorEmail(),
|
comment.getCommentAuthorEmail(),
|
||||||
"您在" + HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "的评论已审核通过!", map, "common/mail_template/mail_passed.ftl");
|
"您在" + HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "的评论已审核通过!", map, "common/mail_template/mail_passed.ftl");
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Mail server not configured: {}", e.getMessage());
|
log.error("Mail server not configured: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package cc.ryanc.halo.web.controller.admin;
|
package cc.ryanc.halo.web.controller.admin;
|
||||||
|
|
||||||
import cc.ryanc.halo.model.domain.*;
|
import cc.ryanc.halo.model.domain.Gallery;
|
||||||
|
import cc.ryanc.halo.model.domain.Link;
|
||||||
|
import cc.ryanc.halo.model.domain.Post;
|
||||||
|
import cc.ryanc.halo.model.domain.User;
|
||||||
import cc.ryanc.halo.model.dto.HaloConst;
|
import cc.ryanc.halo.model.dto.HaloConst;
|
||||||
import cc.ryanc.halo.model.dto.JsonResult;
|
import cc.ryanc.halo.model.dto.JsonResult;
|
||||||
import cc.ryanc.halo.model.dto.LogsRecord;
|
import cc.ryanc.halo.model.dto.LogsRecord;
|
||||||
|
@ -17,7 +20,6 @@ import cc.ryanc.halo.utils.MarkdownUtils;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.util.RandomUtil;
|
import cn.hutool.core.util.RandomUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.extra.servlet.ServletUtil;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.propertyeditors.CustomDateEditor;
|
import org.springframework.beans.propertyeditors.CustomDateEditor;
|
||||||
|
@ -211,7 +213,7 @@ public class PageController {
|
||||||
@GetMapping(value = "/new")
|
@GetMapping(value = "/new")
|
||||||
public String newPage(Model model) {
|
public String newPage(Model model) {
|
||||||
List<String> customTpls = HaloUtils.getCustomTpl(HaloConst.OPTIONS.get(BlogPropertiesEnum.THEME.getProp()));
|
List<String> customTpls = HaloUtils.getCustomTpl(HaloConst.OPTIONS.get(BlogPropertiesEnum.THEME.getProp()));
|
||||||
model.addAttribute("customTpls",customTpls);
|
model.addAttribute("customTpls", customTpls);
|
||||||
return "admin/admin_page_md_editor";
|
return "admin/admin_page_md_editor";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -247,7 +249,7 @@ public class PageController {
|
||||||
post.setPostThumbnail("/static/images/thumbnail/thumbnail-" + RandomUtil.randomInt(1, 10) + ".jpg");
|
post.setPostThumbnail("/static/images/thumbnail/thumbnail-" + RandomUtil.randomInt(1, 10) + ".jpg");
|
||||||
}
|
}
|
||||||
postService.saveByPost(post);
|
postService.saveByPost(post);
|
||||||
logsService.saveByLogs(new Logs(LogsRecord.PUSH_PAGE, post.getPostTitle(), ServletUtil.getClientIP(request), DateUtil.date()));
|
logsService.save(LogsRecord.PUSH_PAGE, post.getPostTitle(), request);
|
||||||
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), msg);
|
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), msg);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Save page failed: {}", e.getMessage());
|
log.error("Save page failed: {}", e.getMessage());
|
||||||
|
@ -267,7 +269,7 @@ public class PageController {
|
||||||
Optional<Post> post = postService.findByPostId(pageId);
|
Optional<Post> post = postService.findByPostId(pageId);
|
||||||
List<String> customTpls = HaloUtils.getCustomTpl(HaloConst.OPTIONS.get(BlogPropertiesEnum.THEME.getProp()));
|
List<String> customTpls = HaloUtils.getCustomTpl(HaloConst.OPTIONS.get(BlogPropertiesEnum.THEME.getProp()));
|
||||||
model.addAttribute("post", post.orElse(new Post()));
|
model.addAttribute("post", post.orElse(new Post()));
|
||||||
model.addAttribute("customTpls",customTpls);
|
model.addAttribute("customTpls", customTpls);
|
||||||
return "admin/admin_page_md_editor";
|
return "admin/admin_page_md_editor";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package cc.ryanc.halo.web.controller.admin;
|
package cc.ryanc.halo.web.controller.admin;
|
||||||
|
|
||||||
import cc.ryanc.halo.model.domain.*;
|
import cc.ryanc.halo.model.domain.Category;
|
||||||
|
import cc.ryanc.halo.model.domain.Post;
|
||||||
|
import cc.ryanc.halo.model.domain.Tag;
|
||||||
|
import cc.ryanc.halo.model.domain.User;
|
||||||
import cc.ryanc.halo.model.dto.HaloConst;
|
import cc.ryanc.halo.model.dto.HaloConst;
|
||||||
import cc.ryanc.halo.model.dto.JsonResult;
|
import cc.ryanc.halo.model.dto.JsonResult;
|
||||||
import cc.ryanc.halo.model.dto.LogsRecord;
|
import cc.ryanc.halo.model.dto.LogsRecord;
|
||||||
|
@ -19,7 +22,6 @@ import cc.ryanc.halo.web.controller.core.BaseController;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.util.RandomUtil;
|
import cn.hutool.core.util.RandomUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.extra.servlet.ServletUtil;
|
|
||||||
import cn.hutool.http.HtmlUtil;
|
import cn.hutool.http.HtmlUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -213,7 +215,7 @@ public class PostController extends BaseController {
|
||||||
post.setPostThumbnail("/static/images/thumbnail/thumbnail-" + RandomUtil.randomInt(1, 10) + ".jpg");
|
post.setPostThumbnail("/static/images/thumbnail/thumbnail-" + RandomUtil.randomInt(1, 10) + ".jpg");
|
||||||
}
|
}
|
||||||
postService.saveByPost(post);
|
postService.saveByPost(post);
|
||||||
logsService.saveByLogs(new Logs(LogsRecord.PUSH_POST, post.getPostTitle(), ServletUtil.getClientIP(request), DateUtil.date()));
|
logsService.save(LogsRecord.PUSH_POST, post.getPostTitle(), request);
|
||||||
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), msg);
|
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), msg);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Save article failed: {}", e.getMessage());
|
log.error("Save article failed: {}", e.getMessage());
|
||||||
|
@ -268,7 +270,7 @@ public class PostController extends BaseController {
|
||||||
try {
|
try {
|
||||||
Optional<Post> post = postService.findByPostId(postId);
|
Optional<Post> post = postService.findByPostId(postId);
|
||||||
postService.removeByPostId(postId);
|
postService.removeByPostId(postId);
|
||||||
logsService.saveByLogs(new Logs(LogsRecord.REMOVE_POST, post.get().getPostTitle(), ServletUtil.getClientIP(request), DateUtil.date()));
|
logsService.save(LogsRecord.REMOVE_POST, post.get().getPostTitle(), request);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Delete article failed: {}", e.getMessage());
|
log.error("Delete article failed: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,313 +1,306 @@
|
||||||
package cc.ryanc.halo.web.controller.admin;
|
package cc.ryanc.halo.web.controller.admin;
|
||||||
|
|
||||||
import cc.ryanc.halo.model.domain.Logs;
|
import cc.ryanc.halo.model.dto.HaloConst;
|
||||||
import cc.ryanc.halo.model.dto.HaloConst;
|
import cc.ryanc.halo.model.dto.JsonResult;
|
||||||
import cc.ryanc.halo.model.dto.JsonResult;
|
import cc.ryanc.halo.model.dto.LogsRecord;
|
||||||
import cc.ryanc.halo.model.dto.LogsRecord;
|
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
|
||||||
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
|
import cc.ryanc.halo.model.enums.ResultCodeEnum;
|
||||||
import cc.ryanc.halo.model.enums.ResultCodeEnum;
|
import cc.ryanc.halo.model.enums.TrueFalseEnum;
|
||||||
import cc.ryanc.halo.model.enums.TrueFalseEnum;
|
import cc.ryanc.halo.service.LogsService;
|
||||||
import cc.ryanc.halo.service.LogsService;
|
import cc.ryanc.halo.service.OptionsService;
|
||||||
import cc.ryanc.halo.service.OptionsService;
|
import cc.ryanc.halo.utils.HaloUtils;
|
||||||
import cc.ryanc.halo.utils.HaloUtils;
|
import cc.ryanc.halo.utils.LocaleMessageUtil;
|
||||||
import cc.ryanc.halo.utils.LocaleMessageUtil;
|
import cc.ryanc.halo.web.controller.core.BaseController;
|
||||||
import cc.ryanc.halo.web.controller.core.BaseController;
|
import cn.hutool.core.io.FileUtil;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.io.file.FileReader;
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.file.FileWriter;
|
||||||
import cn.hutool.core.io.file.FileReader;
|
import cn.hutool.core.util.RuntimeUtil;
|
||||||
import cn.hutool.core.io.file.FileWriter;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.core.util.RuntimeUtil;
|
import cn.hutool.core.util.ZipUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import freemarker.template.Configuration;
|
||||||
import cn.hutool.core.util.ZipUtil;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import cn.hutool.extra.servlet.ServletUtil;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import freemarker.template.Configuration;
|
import org.springframework.cache.annotation.CacheEvict;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.cache.annotation.CacheEvict;
|
import org.springframework.util.ResourceUtils;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import org.springframework.util.ResourceUtils;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import java.util.List;
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileNotFoundException;
|
/**
|
||||||
import java.util.List;
|
* <pre>
|
||||||
|
* 后台主题管理控制器
|
||||||
/**
|
* </pre>
|
||||||
* <pre>
|
*
|
||||||
* 后台主题管理控制器
|
* @author : RYAN0UP
|
||||||
* </pre>
|
* @date : 2017/12/16
|
||||||
*
|
*/
|
||||||
* @author : RYAN0UP
|
@Slf4j
|
||||||
* @date : 2017/12/16
|
@Controller
|
||||||
*/
|
@RequestMapping(value = "/admin/themes")
|
||||||
@Slf4j
|
public class ThemeController extends BaseController {
|
||||||
@Controller
|
|
||||||
@RequestMapping(value = "/admin/themes")
|
private static final String NOT_FOUND_GIT = "-bash: git: command not found";
|
||||||
public class ThemeController extends BaseController {
|
|
||||||
|
@Autowired
|
||||||
private static final String NOT_FOUND_GIT = "-bash: git: command not found";
|
private OptionsService optionsService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private OptionsService optionsService;
|
private LogsService logsService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private LogsService logsService;
|
private Configuration configuration;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private Configuration configuration;
|
private LocaleMessageUtil localeMessageUtil;
|
||||||
|
|
||||||
@Autowired
|
/**
|
||||||
private LocaleMessageUtil localeMessageUtil;
|
* 渲染主题设置页面
|
||||||
|
*
|
||||||
/**
|
* @param model model
|
||||||
* 渲染主题设置页面
|
* @return 模板路径admin/admin_theme
|
||||||
*
|
*/
|
||||||
* @param model model
|
@GetMapping
|
||||||
* @return 模板路径admin/admin_theme
|
public String themes(Model model) {
|
||||||
*/
|
model.addAttribute("activeTheme", BaseController.THEME);
|
||||||
@GetMapping
|
if (null != HaloConst.THEMES) {
|
||||||
public String themes(Model model) {
|
model.addAttribute("themes", HaloConst.THEMES);
|
||||||
model.addAttribute("activeTheme", BaseController.THEME);
|
}
|
||||||
if (null != HaloConst.THEMES) {
|
return "admin/admin_theme";
|
||||||
model.addAttribute("themes", HaloConst.THEMES);
|
}
|
||||||
}
|
|
||||||
return "admin/admin_theme";
|
/**
|
||||||
}
|
* 激活主题
|
||||||
|
*
|
||||||
/**
|
* @param siteTheme 主题名称
|
||||||
* 激活主题
|
* @param request request
|
||||||
*
|
* @return JsonResult
|
||||||
* @param siteTheme 主题名称
|
*/
|
||||||
* @param request request
|
@GetMapping(value = "/set")
|
||||||
* @return JsonResult
|
@ResponseBody
|
||||||
*/
|
@CacheEvict(value = "posts", allEntries = true, beforeInvocation = true)
|
||||||
@GetMapping(value = "/set")
|
public JsonResult activeTheme(@RequestParam("siteTheme") String siteTheme,
|
||||||
@ResponseBody
|
HttpServletRequest request) {
|
||||||
@CacheEvict(value = "posts", allEntries = true, beforeInvocation = true)
|
try {
|
||||||
public JsonResult activeTheme(@RequestParam("siteTheme") String siteTheme,
|
//保存主题设置项
|
||||||
HttpServletRequest request) {
|
optionsService.saveOption(BlogPropertiesEnum.THEME.getProp(), siteTheme);
|
||||||
try {
|
//设置主题
|
||||||
//保存主题设置项
|
BaseController.THEME = siteTheme;
|
||||||
optionsService.saveOption(BlogPropertiesEnum.THEME.getProp(), siteTheme);
|
HaloConst.OPTIONS.clear();
|
||||||
//设置主题
|
HaloConst.OPTIONS = optionsService.findAllOptions();
|
||||||
BaseController.THEME = siteTheme;
|
configuration.setSharedVariable("themeName", siteTheme);
|
||||||
HaloConst.OPTIONS.clear();
|
configuration.setSharedVariable("options", HaloConst.OPTIONS);
|
||||||
HaloConst.OPTIONS = optionsService.findAllOptions();
|
log.info("Changed theme to {}", siteTheme);
|
||||||
configuration.setSharedVariable("themeName", siteTheme);
|
logsService.save(LogsRecord.CHANGE_THEME, "更换为" + siteTheme, request);
|
||||||
configuration.setSharedVariable("options", HaloConst.OPTIONS);
|
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.theme.change-success", new Object[]{siteTheme}));
|
||||||
log.info("Changed theme to {}", siteTheme);
|
} catch (Exception e) {
|
||||||
logsService.saveByLogs(
|
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.theme.change-failed"));
|
||||||
new Logs(LogsRecord.CHANGE_THEME, "更换为" + siteTheme, ServletUtil.getClientIP(request), DateUtil.date())
|
}
|
||||||
);
|
}
|
||||||
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.theme.change-success", new Object[]{siteTheme}));
|
|
||||||
} catch (Exception e) {
|
|
||||||
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.theme.change-failed"));
|
/**
|
||||||
}
|
* 上传主题
|
||||||
}
|
*
|
||||||
|
* @param file 文件
|
||||||
|
* @return JsonResult
|
||||||
/**
|
*/
|
||||||
* 上传主题
|
@RequestMapping(value = "/upload", method = RequestMethod.POST)
|
||||||
*
|
@ResponseBody
|
||||||
* @param file 文件
|
public JsonResult uploadTheme(@RequestParam("file") MultipartFile file,
|
||||||
* @return JsonResult
|
HttpServletRequest request) {
|
||||||
*/
|
try {
|
||||||
@RequestMapping(value = "/upload", method = RequestMethod.POST)
|
if (!file.isEmpty()) {
|
||||||
@ResponseBody
|
//获取项目根路径
|
||||||
public JsonResult uploadTheme(@RequestParam("file") MultipartFile file,
|
File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
|
||||||
HttpServletRequest request) {
|
File themePath = new File(basePath.getAbsolutePath(), new StringBuffer("templates/themes/").append(file.getOriginalFilename()).toString());
|
||||||
try {
|
file.transferTo(themePath);
|
||||||
if (!file.isEmpty()) {
|
log.info("Upload topic success, path is " + themePath.getAbsolutePath());
|
||||||
//获取项目根路径
|
logsService.save(LogsRecord.UPLOAD_THEME, file.getOriginalFilename(), request);
|
||||||
File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
|
ZipUtil.unzip(themePath, new File(basePath.getAbsolutePath(), "templates/themes/"));
|
||||||
File themePath = new File(basePath.getAbsolutePath(), new StringBuffer("templates/themes/").append(file.getOriginalFilename()).toString());
|
FileUtil.del(themePath);
|
||||||
file.transferTo(themePath);
|
HaloConst.THEMES.clear();
|
||||||
log.info("Upload topic success, path is " + themePath.getAbsolutePath());
|
HaloConst.THEMES = HaloUtils.getThemes();
|
||||||
logsService.saveByLogs(
|
} else {
|
||||||
new Logs(LogsRecord.UPLOAD_THEME, file.getOriginalFilename(), ServletUtil.getClientIP(request), DateUtil.date())
|
log.error("Upload theme failed, no file selected");
|
||||||
);
|
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.theme.upload-no-file"));
|
||||||
ZipUtil.unzip(themePath, new File(basePath.getAbsolutePath(), "templates/themes/"));
|
}
|
||||||
FileUtil.del(themePath);
|
} catch (Exception e) {
|
||||||
HaloConst.THEMES.clear();
|
log.error("Upload theme failed: {}", e.getMessage());
|
||||||
HaloConst.THEMES = HaloUtils.getThemes();
|
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.theme.upload-failed"));
|
||||||
} else {
|
}
|
||||||
log.error("Upload theme failed, no file selected");
|
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.theme.upload-success"));
|
||||||
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.theme.upload-no-file"));
|
}
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
/**
|
||||||
log.error("Upload theme failed: {}", e.getMessage());
|
* 删除主题
|
||||||
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.theme.upload-failed"));
|
*
|
||||||
}
|
* @param themeName 主题文件夹名
|
||||||
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.theme.upload-success"));
|
* @return string 重定向到/admin/themes
|
||||||
}
|
*/
|
||||||
|
@GetMapping(value = "/remove")
|
||||||
/**
|
public String removeTheme(@RequestParam("themeName") String themeName) {
|
||||||
* 删除主题
|
try {
|
||||||
*
|
File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
|
||||||
* @param themeName 主题文件夹名
|
File themePath = new File(basePath.getAbsolutePath(), "templates/themes/" + themeName);
|
||||||
* @return string 重定向到/admin/themes
|
FileUtil.del(themePath);
|
||||||
*/
|
HaloConst.THEMES.clear();
|
||||||
@GetMapping(value = "/remove")
|
HaloConst.THEMES = HaloUtils.getThemes();
|
||||||
public String removeTheme(@RequestParam("themeName") String themeName) {
|
} catch (Exception e) {
|
||||||
try {
|
log.error("Delete theme failed: {}", e.getMessage());
|
||||||
File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
|
}
|
||||||
File themePath = new File(basePath.getAbsolutePath(), "templates/themes/" + themeName);
|
return "redirect:/admin/themes";
|
||||||
FileUtil.del(themePath);
|
}
|
||||||
HaloConst.THEMES.clear();
|
|
||||||
HaloConst.THEMES = HaloUtils.getThemes();
|
/**
|
||||||
} catch (Exception e) {
|
* 安装主题弹出层
|
||||||
log.error("Delete theme failed: {}", e.getMessage());
|
*
|
||||||
}
|
* @return 目录路径admin/widget/_theme-install
|
||||||
return "redirect:/admin/themes";
|
*/
|
||||||
}
|
@GetMapping(value = "/install")
|
||||||
|
public String install() {
|
||||||
/**
|
return "admin/widget/_theme-install";
|
||||||
* 安装主题弹出层
|
}
|
||||||
*
|
|
||||||
* @return 目录路径admin/widget/_theme-install
|
/**
|
||||||
*/
|
* 在线拉取主题
|
||||||
@GetMapping(value = "/install")
|
*
|
||||||
public String install() {
|
* @param remoteAddr 远程地址
|
||||||
return "admin/widget/_theme-install";
|
* @param themeName 主题名称
|
||||||
}
|
* @return JsonResult
|
||||||
|
*/
|
||||||
/**
|
@PostMapping(value = "/clone")
|
||||||
* 在线拉取主题
|
@ResponseBody
|
||||||
*
|
public JsonResult cloneFromRemote(@RequestParam(value = "remoteAddr") String remoteAddr,
|
||||||
* @param remoteAddr 远程地址
|
@RequestParam(value = "themeName") String themeName) {
|
||||||
* @param themeName 主题名称
|
if (StrUtil.isBlank(remoteAddr) || StrUtil.isBlank(themeName)) {
|
||||||
* @return JsonResult
|
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.info-no-complete"));
|
||||||
*/
|
}
|
||||||
@PostMapping(value = "/clone")
|
try {
|
||||||
@ResponseBody
|
File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
|
||||||
public JsonResult cloneFromRemote(@RequestParam(value = "remoteAddr") String remoteAddr,
|
File themePath = new File(basePath.getAbsolutePath(), "templates/themes");
|
||||||
@RequestParam(value = "themeName") String themeName) {
|
String cmdResult = RuntimeUtil.execForStr("git clone " + remoteAddr + " " + themePath.getAbsolutePath() + "/" + themeName);
|
||||||
if (StrUtil.isBlank(remoteAddr) || StrUtil.isBlank(themeName)) {
|
if (NOT_FOUND_GIT.equals(cmdResult)) {
|
||||||
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.info-no-complete"));
|
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.theme.no-git"));
|
||||||
}
|
}
|
||||||
try {
|
HaloConst.THEMES.clear();
|
||||||
File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
|
HaloConst.THEMES = HaloUtils.getThemes();
|
||||||
File themePath = new File(basePath.getAbsolutePath(), "templates/themes");
|
} catch (FileNotFoundException e) {
|
||||||
String cmdResult = RuntimeUtil.execForStr("git clone " + remoteAddr + " " + themePath.getAbsolutePath() + "/" + themeName);
|
log.error("Cloning theme failed: {}", e.getMessage());
|
||||||
if (NOT_FOUND_GIT.equals(cmdResult)) {
|
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.theme.clone-theme-failed") + e.getMessage());
|
||||||
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.theme.no-git"));
|
}
|
||||||
}
|
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.install-success"));
|
||||||
HaloConst.THEMES.clear();
|
}
|
||||||
HaloConst.THEMES = HaloUtils.getThemes();
|
|
||||||
} catch (FileNotFoundException e) {
|
/**
|
||||||
log.error("Cloning theme failed: {}", e.getMessage());
|
* 更新主题
|
||||||
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.theme.clone-theme-failed") + e.getMessage());
|
*
|
||||||
}
|
* @param themeName 主题名
|
||||||
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.install-success"));
|
* @return JsonResult
|
||||||
}
|
*/
|
||||||
|
@GetMapping(value = "/pull")
|
||||||
/**
|
@ResponseBody
|
||||||
* 更新主题
|
public JsonResult pullFromRemote(@RequestParam(value = "themeName") String themeName) {
|
||||||
*
|
try {
|
||||||
* @param themeName 主题名
|
File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
|
||||||
* @return JsonResult
|
File themePath = new File(basePath.getAbsolutePath(), "templates/themes");
|
||||||
*/
|
String cmdResult = RuntimeUtil.execForStr("cd " + themePath.getAbsolutePath() + "/" + themeName + " && git pull");
|
||||||
@GetMapping(value = "/pull")
|
if (NOT_FOUND_GIT.equals(cmdResult)) {
|
||||||
@ResponseBody
|
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.theme.no-git"));
|
||||||
public JsonResult pullFromRemote(@RequestParam(value = "themeName") String themeName) {
|
}
|
||||||
try {
|
HaloConst.THEMES.clear();
|
||||||
File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
|
HaloConst.THEMES = HaloUtils.getThemes();
|
||||||
File themePath = new File(basePath.getAbsolutePath(), "templates/themes");
|
} catch (Exception e) {
|
||||||
String cmdResult = RuntimeUtil.execForStr("cd " + themePath.getAbsolutePath() + "/" + themeName + " && git pull");
|
log.error("Update theme failed: {}", e.getMessage());
|
||||||
if (NOT_FOUND_GIT.equals(cmdResult)) {
|
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.theme.update-theme-failed") + e.getMessage());
|
||||||
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.theme.no-git"));
|
}
|
||||||
}
|
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.update-success"));
|
||||||
HaloConst.THEMES.clear();
|
}
|
||||||
HaloConst.THEMES = HaloUtils.getThemes();
|
|
||||||
} catch (Exception e) {
|
/**
|
||||||
log.error("Update theme failed: {}", e.getMessage());
|
* 跳转到主题设置
|
||||||
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.theme.update-theme-failed") + e.getMessage());
|
*
|
||||||
}
|
* @param theme theme名称
|
||||||
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.update-success"));
|
*/
|
||||||
}
|
@GetMapping(value = "/options")
|
||||||
|
public String setting(Model model,
|
||||||
/**
|
@RequestParam("theme") String theme,
|
||||||
* 跳转到主题设置
|
@RequestParam("hasUpdate") String hasUpdate) {
|
||||||
*
|
model.addAttribute("themeDir", theme);
|
||||||
* @param theme theme名称
|
if (StrUtil.equals(hasUpdate, TrueFalseEnum.TRUE.getDesc())) {
|
||||||
*/
|
model.addAttribute("hasUpdate", true);
|
||||||
@GetMapping(value = "/options")
|
} else {
|
||||||
public String setting(Model model,
|
model.addAttribute("hasUpdate", false);
|
||||||
@RequestParam("theme") String theme,
|
}
|
||||||
@RequestParam("hasUpdate") String hasUpdate) {
|
return "themes/" + theme + "/module/options";
|
||||||
model.addAttribute("themeDir", theme);
|
}
|
||||||
if (StrUtil.equals(hasUpdate, TrueFalseEnum.TRUE.getDesc())) {
|
|
||||||
model.addAttribute("hasUpdate", true);
|
/**
|
||||||
} else {
|
* 编辑主题
|
||||||
model.addAttribute("hasUpdate", false);
|
*
|
||||||
}
|
* @param model model
|
||||||
return "themes/" + theme + "/module/options";
|
* @return 模板路径admin/admin_theme-editor
|
||||||
}
|
*/
|
||||||
|
@GetMapping(value = "/editor")
|
||||||
/**
|
public String editor(Model model) {
|
||||||
* 编辑主题
|
List<String> tpls = HaloUtils.getTplName(BaseController.THEME);
|
||||||
*
|
model.addAttribute("tpls", tpls);
|
||||||
* @param model model
|
return "admin/admin_theme-editor";
|
||||||
* @return 模板路径admin/admin_theme-editor
|
}
|
||||||
*/
|
|
||||||
@GetMapping(value = "/editor")
|
/**
|
||||||
public String editor(Model model) {
|
* 获取模板文件内容
|
||||||
List<String> tpls = HaloUtils.getTplName(BaseController.THEME);
|
*
|
||||||
model.addAttribute("tpls", tpls);
|
* @param tplName 模板文件名
|
||||||
return "admin/admin_theme-editor";
|
* @return 模板内容
|
||||||
}
|
*/
|
||||||
|
@GetMapping(value = "/getTpl", produces = "text/text;charset=UTF-8")
|
||||||
/**
|
@ResponseBody
|
||||||
* 获取模板文件内容
|
public String getTplContent(@RequestParam("tplName") String tplName) {
|
||||||
*
|
String tplContent = "";
|
||||||
* @param tplName 模板文件名
|
try {
|
||||||
* @return 模板内容
|
//获取项目根路径
|
||||||
*/
|
File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
|
||||||
@GetMapping(value = "/getTpl", produces = "text/text;charset=UTF-8")
|
//获取主题路径
|
||||||
@ResponseBody
|
File themesPath = new File(basePath.getAbsolutePath(), new StringBuffer("templates/themes/").append(BaseController.THEME).append("/").append(tplName).toString());
|
||||||
public String getTplContent(@RequestParam("tplName") String tplName) {
|
FileReader fileReader = new FileReader(themesPath);
|
||||||
String tplContent = "";
|
tplContent = fileReader.readString();
|
||||||
try {
|
} catch (Exception e) {
|
||||||
//获取项目根路径
|
log.error("Get template file error: {}", e.getMessage());
|
||||||
File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
|
}
|
||||||
//获取主题路径
|
return tplContent;
|
||||||
File themesPath = new File(basePath.getAbsolutePath(), new StringBuffer("templates/themes/").append(BaseController.THEME).append("/").append(tplName).toString());
|
}
|
||||||
FileReader fileReader = new FileReader(themesPath);
|
|
||||||
tplContent = fileReader.readString();
|
/**
|
||||||
} catch (Exception e) {
|
* 保存修改模板
|
||||||
log.error("Get template file error: {}", e.getMessage());
|
*
|
||||||
}
|
* @param tplName 模板名称
|
||||||
return tplContent;
|
* @param tplContent 模板内容
|
||||||
}
|
* @return JsonResult
|
||||||
|
*/
|
||||||
/**
|
@PostMapping(value = "/editor/save")
|
||||||
* 保存修改模板
|
@ResponseBody
|
||||||
*
|
public JsonResult saveTpl(@RequestParam("tplName") String tplName,
|
||||||
* @param tplName 模板名称
|
@RequestParam("tplContent") String tplContent) {
|
||||||
* @param tplContent 模板内容
|
if (StrUtil.isBlank(tplContent)) {
|
||||||
* @return JsonResult
|
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.theme.edit.no-content"));
|
||||||
*/
|
}
|
||||||
@PostMapping(value = "/editor/save")
|
try {
|
||||||
@ResponseBody
|
//获取项目根路径
|
||||||
public JsonResult saveTpl(@RequestParam("tplName") String tplName,
|
File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
|
||||||
@RequestParam("tplContent") String tplContent) {
|
//获取主题路径
|
||||||
if (StrUtil.isBlank(tplContent)) {
|
File tplPath = new File(basePath.getAbsolutePath(), new StringBuffer("templates/themes/").append(BaseController.THEME).append("/").append(tplName).toString());
|
||||||
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.theme.edit.no-content"));
|
FileWriter fileWriter = new FileWriter(tplPath);
|
||||||
}
|
fileWriter.write(tplContent);
|
||||||
try {
|
} catch (Exception e) {
|
||||||
//获取项目根路径
|
log.error("Template save failed: {}", e.getMessage());
|
||||||
File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
|
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.save-failed"));
|
||||||
//获取主题路径
|
}
|
||||||
File tplPath = new File(basePath.getAbsolutePath(), new StringBuffer("templates/themes/").append(BaseController.THEME).append("/").append(tplName).toString());
|
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.save-success"));
|
||||||
FileWriter fileWriter = new FileWriter(tplPath);
|
}
|
||||||
fileWriter.write(tplContent);
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Template save failed: {}", e.getMessage());
|
|
||||||
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.save-failed"));
|
|
||||||
}
|
|
||||||
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.save-success"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -75,6 +75,7 @@ public class ApiPostController {
|
||||||
public JsonResult posts(@PathVariable(value = "postId") Long postId) {
|
public JsonResult posts(@PathVariable(value = "postId") Long postId) {
|
||||||
Post post = postService.findByPostId(postId, PostTypeEnum.POST_TYPE_POST.getDesc());
|
Post post = postService.findByPostId(postId, PostTypeEnum.POST_TYPE_POST.getDesc());
|
||||||
if (null != post) {
|
if (null != post) {
|
||||||
|
postService.cacheViews(post.getPostId());
|
||||||
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), post);
|
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), post);
|
||||||
} else {
|
} else {
|
||||||
return new JsonResult(ResponseStatusEnum.NOTFOUND.getCode(), ResponseStatusEnum.NOTFOUND.getMsg());
|
return new JsonResult(ResponseStatusEnum.NOTFOUND.getCode(), ResponseStatusEnum.NOTFOUND.getMsg());
|
||||||
|
|
|
@ -11,7 +11,6 @@ import cc.ryanc.halo.utils.MarkdownUtils;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.crypto.SecureUtil;
|
import cn.hutool.crypto.SecureUtil;
|
||||||
import cn.hutool.extra.servlet.ServletUtil;
|
|
||||||
import freemarker.template.Configuration;
|
import freemarker.template.Configuration;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -160,7 +159,7 @@ public class InstallController {
|
||||||
optionsService.saveOption(BlogPropertiesEnum.IS_INSTALL.getProp(), TrueFalseEnum.TRUE.getDesc());
|
optionsService.saveOption(BlogPropertiesEnum.IS_INSTALL.getProp(), TrueFalseEnum.TRUE.getDesc());
|
||||||
|
|
||||||
//语言设置
|
//语言设置
|
||||||
optionsService.saveOption(BlogPropertiesEnum.BLOG_LOCALE.getProp(),blogLocale);
|
optionsService.saveOption(BlogPropertiesEnum.BLOG_LOCALE.getProp(), blogLocale);
|
||||||
//保存博客标题和博客地址设置
|
//保存博客标题和博客地址设置
|
||||||
optionsService.saveOption(BlogPropertiesEnum.BLOG_TITLE.getProp(), blogTitle);
|
optionsService.saveOption(BlogPropertiesEnum.BLOG_TITLE.getProp(), blogTitle);
|
||||||
optionsService.saveOption(BlogPropertiesEnum.BLOG_URL.getProp(), blogUrl);
|
optionsService.saveOption(BlogPropertiesEnum.BLOG_URL.getProp(), blogUrl);
|
||||||
|
@ -169,7 +168,7 @@ public class InstallController {
|
||||||
optionsService.saveOption(BlogPropertiesEnum.THEME.getProp(), "anatole");
|
optionsService.saveOption(BlogPropertiesEnum.THEME.getProp(), "anatole");
|
||||||
|
|
||||||
//建立网站时间
|
//建立网站时间
|
||||||
optionsService.saveOption(BlogPropertiesEnum.BLOG_START.getProp(), DateUtil.format(DateUtil.date(),"yyyy-MM-dd"));
|
optionsService.saveOption(BlogPropertiesEnum.BLOG_START.getProp(), DateUtil.format(DateUtil.date(), "yyyy-MM-dd"));
|
||||||
|
|
||||||
//默认不配置邮件系统
|
//默认不配置邮件系统
|
||||||
optionsService.saveOption(BlogPropertiesEnum.SMTP_EMAIL_ENABLE.getProp(), TrueFalseEnum.FALSE.getDesc());
|
optionsService.saveOption(BlogPropertiesEnum.SMTP_EMAIL_ENABLE.getProp(), TrueFalseEnum.FALSE.getDesc());
|
||||||
|
@ -180,14 +179,7 @@ public class InstallController {
|
||||||
optionsService.saveOption(BlogPropertiesEnum.COMMENT_REPLY_NOTICE.getProp(), TrueFalseEnum.FALSE.getDesc());
|
optionsService.saveOption(BlogPropertiesEnum.COMMENT_REPLY_NOTICE.getProp(), TrueFalseEnum.FALSE.getDesc());
|
||||||
|
|
||||||
//更新日志
|
//更新日志
|
||||||
logsService.saveByLogs(
|
logsService.save(LogsRecord.INSTALL, "安装成功,欢迎使用Halo。", request);
|
||||||
new Logs(
|
|
||||||
LogsRecord.INSTALL,
|
|
||||||
"安装成功,欢迎使用Halo。",
|
|
||||||
ServletUtil.getClientIP(request),
|
|
||||||
DateUtil.date()
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
Menu menuIndex = new Menu();
|
Menu menuIndex = new Menu();
|
||||||
menuIndex.setMenuName("首页");
|
menuIndex.setMenuName("首页");
|
||||||
|
|
Loading…
Reference in New Issue