🎨 代码优化

pull/33/merge
ruibaby 2018-07-22 20:03:14 +08:00
parent e61f82c8e3
commit eceb8339b3
5 changed files with 127 additions and 48 deletions

View File

@ -0,0 +1,33 @@
package cc.ryanc.halo.model.enums;
/**
* @author : RYAN0UP
* @date : 2018/7/22
*/
public enum BackupType {
/**
*
*/
RESOURCES("resources"),
/**
*
*/
DATABASES("databases"),
/**
*
*/
POSTS("posts");
private String desc;
BackupType(String desc) {
this.desc = desc;
}
public String getDesc() {
return desc;
}
}

View File

@ -5,10 +5,7 @@ import cc.ryanc.halo.model.domain.User;
import cc.ryanc.halo.model.dto.BackupDto;
import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.enums.PostType;
import cc.ryanc.halo.model.enums.ResultCode;
import cc.ryanc.halo.model.enums.TrueFalse;
import cc.ryanc.halo.model.enums.*;
import cc.ryanc.halo.service.MailService;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.utils.HaloUtils;
@ -58,12 +55,12 @@ public class BackupController {
@GetMapping
public String backup(@RequestParam(value = "type", defaultValue = "resources") String type, Model model) {
List<BackupDto> backups = null;
if (StringUtils.equals(type, "resources")) {
backups = HaloUtils.getBackUps("resources");
} else if (StringUtils.equals(type, "databases")) {
backups = HaloUtils.getBackUps("databases");
} else if (StringUtils.equals(type, "posts")) {
backups = HaloUtils.getBackUps("posts");
if (StringUtils.equals(type,BackupType.RESOURCES.getDesc())) {
backups = HaloUtils.getBackUps(BackupType.RESOURCES.getDesc());
} else if (StringUtils.equals(type, BackupType.DATABASES.getDesc())) {
backups = HaloUtils.getBackUps(BackupType.DATABASES.getDesc());
} else if (StringUtils.equals(type, BackupType.POSTS.getDesc())) {
backups = HaloUtils.getBackUps(BackupType.POSTS.getDesc());
} else {
backups = new ArrayList<>();
}
@ -81,11 +78,11 @@ public class BackupController {
@GetMapping(value = "doBackup")
@ResponseBody
public JsonResult doBackup(@RequestParam("type") String type) {
if (StringUtils.equals("resources", type)) {
if (StringUtils.equals(BackupType.RESOURCES.getDesc(), type)) {
return this.backupResources();
} else if (StringUtils.equals("databases", type)) {
} else if (StringUtils.equals(BackupType.DATABASES.getDesc(), type)) {
return this.backupDatabase();
} else if (StringUtils.equals("posts", type)) {
} else if (StringUtils.equals(BackupType.POSTS.getDesc(), type)) {
return this.backupPosts();
} else {
return new JsonResult(ResultCode.FAIL.getCode(), "备份失败!");
@ -99,7 +96,7 @@ public class BackupController {
*/
public JsonResult backupDatabase() {
try {
if (HaloUtils.getBackUps("databases").size() > 10) {
if (HaloUtils.getBackUps(BackupType.DATABASES.getDesc()).size() > 10) {
FileUtil.del(System.getProperties().getProperty("user.home") + "/halo/backup/databases/");
}
String srcPath = System.getProperties().getProperty("user.home") + "/halo/";
@ -121,7 +118,7 @@ public class BackupController {
*/
public JsonResult backupResources() {
try {
if (HaloUtils.getBackUps("resources").size() > 10) {
if (HaloUtils.getBackUps(BackupType.RESOURCES.getDesc()).size() > 10) {
FileUtil.del(System.getProperties().getProperty("user.home") + "/halo/backup/resources/");
}
File path = new File(ResourceUtils.getURL("classpath:").getPath());
@ -146,7 +143,7 @@ public class BackupController {
List<Post> posts = postService.findAllPosts(PostType.POST_TYPE_POST.getDesc());
posts.addAll(postService.findAllPosts(PostType.POST_TYPE_PAGE.getDesc()));
try {
if (HaloUtils.getBackUps("posts").size() > 10) {
if (HaloUtils.getBackUps(BackupType.POSTS.getDesc()).size() > 10) {
FileUtil.del(System.getProperties().getProperty("user.home") + "/halo/backup/posts/");
}
//打包好的文件名
@ -210,11 +207,14 @@ public class BackupController {
return new JsonResult(ResultCode.SUCCESS.getCode(), "邮件发送成功!");
}
/**
*
*/
class EmailToAdmin extends Thread {
private String srcPath;
private User user;
public EmailToAdmin(String srcPath, User user) {
private EmailToAdmin(String srcPath, User user) {
this.srcPath = srcPath;
this.user = user;
}

View File

@ -11,7 +11,6 @@ import cc.ryanc.halo.model.enums.TrueFalse;
import cc.ryanc.halo.service.CommentService;
import cc.ryanc.halo.service.MailService;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.service.UserService;
import cc.ryanc.halo.utils.OwoUtil;
import cc.ryanc.halo.web.controller.core.BaseController;
import cn.hutool.core.date.DateUtil;
@ -54,9 +53,6 @@ public class CommentController extends BaseController {
@Autowired
private MailService mailService;
@Autowired
private UserService userService;
@Autowired
private PostService postService;
@ -121,28 +117,7 @@ public class CommentController extends BaseController {
User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY);
//判断是否启用邮件服务
if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), TrueFalse.TRUE.getDesc()) && StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.COMMENT_REPLY_NOTICE.getProp()), TrueFalse.TRUE.getDesc())) {
try {
if (status == 1 && Validator.isEmail(comment.getCommentAuthorEmail())) {
Map<String, Object> map = new HashMap<>();
if (StringUtils.equals(post.getPostType(), PostType.POST_TYPE_POST.getDesc())) {
map.put("pageUrl", HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp()) + "/archives/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId());
} else {
map.put("pageUrl", HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp()) + "/p/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId());
}
map.put("pageName", post.getPostTitle());
map.put("commentContent", comment.getCommentContent());
map.put("blogUrl", HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp()));
map.put("blogTitle", HaloConst.OPTIONS.get(BlogProperties.BLOG_TITLE.getProp()));
map.put("author", user.getUserDisplayName());
mailService.sendTemplateMail(
comment.getCommentAuthorEmail(),
"您在" + HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp()) + "的评论已审核通过!", map, "common/mail/mail_passed.ftl");
}
} catch (Exception e) {
log.error("邮件服务器未配置:{}", e.getMessage());
}
}
new NoticeToAuthor(comment, post, user, status).start();
return "redirect:/admin/comments?status=" + status;
}
@ -151,7 +126,7 @@ public class CommentController extends BaseController {
*
* @param commentId commentId
* @param status status
* @param session session session
* @param page
* @return string /admin/comments
*/
@GetMapping("/remove")
@ -212,6 +187,34 @@ public class CommentController extends BaseController {
commentService.saveByComment(comment);
//邮件通知
new EmailToAuthor(comment, lastComment, post, user, commentContent).start();
} catch (Exception e) {
log.error("回复评论失败:{}", e.getMessage());
}
return "redirect:/admin/comments";
}
/**
*
*/
class EmailToAuthor extends Thread {
private Comment comment;
private Comment lastComment;
private Post post;
private User user;
private String commentContent;
private EmailToAuthor(Comment comment, Comment lastComment, Post post, User user, String commentContent) {
this.comment = comment;
this.lastComment = lastComment;
this.post = post;
this.user = user;
this.commentContent = commentContent;
}
@Override
public void run() {
if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), TrueFalse.TRUE.getDesc()) && StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.COMMENT_REPLY_NOTICE.getProp()), TrueFalse.TRUE.getDesc())) {
if (Validator.isEmail(lastComment.getCommentAuthorEmail())) {
Map<String, Object> map = new HashMap<>();
@ -231,9 +234,50 @@ public class CommentController extends BaseController {
lastComment.getCommentAuthorEmail(), "您在" + HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp()) + "的评论有了新回复", map, "common/mail/mail_reply.ftl");
}
}
}
}
/**
*
*/
class NoticeToAuthor extends Thread {
private Comment comment;
private Post post;
private User user;
private Integer status;
private NoticeToAuthor(Comment comment, Post post, User user, Integer status) {
this.comment = comment;
this.post = post;
this.user = user;
this.status = status;
}
@Override
public void run() {
if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), TrueFalse.TRUE.getDesc()) && StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.COMMENT_REPLY_NOTICE.getProp()), TrueFalse.TRUE.getDesc())) {
try {
if (status == 1 && Validator.isEmail(comment.getCommentAuthorEmail())) {
Map<String, Object> map = new HashMap<>();
if (StringUtils.equals(post.getPostType(), PostType.POST_TYPE_POST.getDesc())) {
map.put("pageUrl", HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp()) + "/archives/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId());
} else {
map.put("pageUrl", HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp()) + "/p/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId());
}
map.put("pageName", post.getPostTitle());
map.put("commentContent", comment.getCommentContent());
map.put("blogUrl", HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp()));
map.put("blogTitle", HaloConst.OPTIONS.get(BlogProperties.BLOG_TITLE.getProp()));
map.put("author", user.getUserDisplayName());
mailService.sendTemplateMail(
comment.getCommentAuthorEmail(),
"您在" + HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp()) + "的评论已审核通过!", map, "common/mail/mail_passed.ftl");
}
} catch (Exception e) {
log.error("回复评论失败:{}", e.getMessage());
}
return "redirect:/admin/comments";
log.error("邮件服务器未配置:{}", e.getMessage());
}
}
}
}
}

View File

@ -3,6 +3,7 @@ package cc.ryanc.halo.web.controller.core;
import cc.ryanc.halo.model.domain.*;
import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.dto.LogsRecord;
import cc.ryanc.halo.model.enums.AllowComment;
import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.enums.TrueFalse;
import cc.ryanc.halo.service.*;
@ -132,6 +133,7 @@ public class InstallController {
post.setPostUrl("hello-halo");
post.setUser(user);
post.setCategories(categories);
post.setAllowComment(AllowComment.ALLOW.getCode());
postService.saveByPost(post);
//第一个评论

View File

@ -345,7 +345,7 @@
<div class="modal-body">
<p>「${options.blog_title?if_exists}」已经运行了<span id="blogStartDay"></span>天了。</p>
<p>在此期间:</p>
<p>累计发表了${postCount?default(0)}篇文章。</p>
<p>累计发表了<@articleTag method="postsCount">${postsCount?default(0)}</@articleTag>篇文章。</p>
<p>累计创建了<@commonTag method="tags">${tags?size}</@commonTag>个标签。</p>
<p>累计获得了${commentCount}条评论。</p>
<p>累计添加了<@commonTag method="links">${links?size}</@commonTag>个友链。</p>