🎨 代码优化

pull/78/head
ruibaby 2019-01-10 13:59:32 +08:00
parent 850a4f3956
commit ccb80fdf12
9 changed files with 39 additions and 53 deletions

View File

@ -13,7 +13,18 @@ import lombok.Data;
@Data
public class QiNiuPutSet {
/**
*
*/
private Long size;
/**
*
*/
private Integer w;
/**
*
*/
private Integer h;
}

View File

@ -113,8 +113,8 @@ public class AttachmentServiceImpl implements AttachmentService {
@CacheEvict(value = ATTACHMENTS_CACHE_NAME, allEntries = true, beforeInvocation = true)
public Attachment remove(Long attachId) {
Optional<Attachment> attachment = this.findByAttachId(attachId);
attachmentRepository.delete(attachment.get());
return attachment.get();
attachmentRepository.delete(attachment.orElse(null));
return attachment.orElse(null);
}
/**

View File

@ -49,8 +49,8 @@ public class CategoryServiceImpl implements CategoryService {
@CacheEvict(value = POSTS_CACHE_NAME, allEntries = true, beforeInvocation = true)
public Category remove(Long cateId) {
final Optional<Category> category = this.findByCateId(cateId);
categoryRepository.delete(category.get());
return category.get();
categoryRepository.delete(category.orElse(null));
return category.orElse(null);
}
/**

View File

@ -54,7 +54,7 @@ public class CommentServiceImpl implements CommentService {
@CacheEvict(value = {COMMENTS_CACHE_NAME, POSTS_CACHE_NAME}, allEntries = true, beforeInvocation = true)
public Optional<Comment> remove(Long commentId) {
final Optional<Comment> comment = this.findCommentById(commentId);
commentRepository.delete(comment.get());
commentRepository.delete(comment.orElse(null));
return comment;
}

View File

@ -4,6 +4,7 @@ import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
import cc.ryanc.halo.service.MailService;
import cc.ryanc.halo.utils.HaloUtils;
import cn.hutool.core.text.StrBuilder;
import freemarker.template.Template;
import io.github.biezhi.ome.OhMyEmail;
import org.springframework.beans.factory.annotation.Autowired;
@ -68,14 +69,14 @@ public class MailServiceImpl implements MailService {
HaloConst.OPTIONS.get(BlogPropertiesEnum.MAIL_SMTP_HOST.getProp()),
HaloConst.OPTIONS.get(BlogPropertiesEnum.MAIL_SMTP_USERNAME.getProp()),
HaloConst.OPTIONS.get(BlogPropertiesEnum.MAIL_SMTP_PASSWORD.getProp()));
String text = "";
StrBuilder text = new StrBuilder();
try {
final Template template = freeMarker.getConfiguration().getTemplate(templateName);
text = FreeMarkerTemplateUtils.processTemplateIntoString(template, content);
text.append(FreeMarkerTemplateUtils.processTemplateIntoString(template, content));
OhMyEmail.subject(subject)
.from(HaloConst.OPTIONS.get(BlogPropertiesEnum.MAIL_FROM_NAME.getProp()))
.to(to)
.html(text)
.html(text.toString())
.send();
} catch (Exception e) {
e.printStackTrace();
@ -99,14 +100,14 @@ public class MailServiceImpl implements MailService {
HaloConst.OPTIONS.get(BlogPropertiesEnum.MAIL_SMTP_USERNAME.getProp()),
HaloConst.OPTIONS.get(BlogPropertiesEnum.MAIL_SMTP_PASSWORD.getProp()));
File file = new File(attachSrc);
String text = "";
StrBuilder text = new StrBuilder();
try {
final Template template = freeMarker.getConfiguration().getTemplate(templateName);
text = FreeMarkerTemplateUtils.processTemplateIntoString(template, content);
text.append(FreeMarkerTemplateUtils.processTemplateIntoString(template, content));
OhMyEmail.subject(subject)
.from(HaloConst.OPTIONS.get(BlogPropertiesEnum.MAIL_FROM_NAME.getProp()))
.to(to)
.html(text)
.html(text.toString())
.attach(file, file.getName())
.send();
} catch (Exception e) {

View File

@ -62,8 +62,8 @@ public class MenuServiceImpl implements MenuService {
@CacheEvict(value = MENUS_CACHE_NAME, allEntries = true, beforeInvocation = true)
public Menu remove(Long menuId) {
final Optional<Menu> menu = this.findByMenuId(menuId);
menuRepository.delete(menu.get());
return menu.get();
menuRepository.delete(menu.orElse(null));
return menu.orElse(null);
}
/**

View File

@ -5,6 +5,7 @@ import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.domain.Tag;
import cc.ryanc.halo.model.dto.Archive;
import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
import cc.ryanc.halo.model.enums.PostStatusEnum;
import cc.ryanc.halo.model.enums.PostTypeEnum;
import cc.ryanc.halo.repository.PostRepository;
@ -12,6 +13,7 @@ import cc.ryanc.halo.service.CategoryService;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.service.TagService;
import cc.ryanc.halo.utils.HaloUtils;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import org.springframework.beans.factory.annotation.Autowired;
@ -61,6 +63,18 @@ public class PostServiceImpl implements PostService {
@Override
@CacheEvict(value = {POSTS_CACHE_NAME, COMMENTS_CACHE_NAME}, allEntries = true, beforeInvocation = true)
public Post save(Post post) {
int postSummary = 50;
if (StrUtil.isNotEmpty(HaloConst.OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()))) {
postSummary = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()));
}
final String summaryText = StrUtil.cleanBlank(HtmlUtil.cleanHtmlTag(post.getPostContent()));
if (summaryText.length() > postSummary) {
final String summary = summaryText.substring(0, postSummary);
post.setPostSummary(summary);
} else {
post.setPostSummary(summaryText);
}
post.setPostUpdate(DateUtil.date());
return postRepository.save(post);
}

View File

@ -347,17 +347,6 @@ public class AdminController extends BaseController {
post.setTags(tags);
post.setCategories(categories);
post.setPostUrl(StrUtil.removeSuffix(file.getOriginalFilename(), ".md"));
int postSummary = 50;
if (StrUtil.isNotEmpty(HaloConst.OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()))) {
postSummary = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()));
}
final String summaryText = StrUtil.cleanBlank(HtmlUtil.cleanHtmlTag(post.getPostContent()));
if (summaryText.length() > postSummary) {
final String summary = summaryText.substring(0, postSummary);
post.setPostSummary(summary);
} else {
post.setPostSummary(summaryText);
}
if (null == post.getPostDate()) {
post.setPostDate(new Date());
}

View File

@ -183,23 +183,8 @@ public class PostController extends BaseController {
final User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY);
try {
post.setPostContent(MarkdownUtils.renderMarkdown(post.getPostContentMd()));
//摘要字数
int postSummary = 50;
if (StrUtil.isNotEmpty(HaloConst.OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()))) {
postSummary = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()));
}
//设置文章摘要
final String summaryText = StrUtil.cleanBlank(HtmlUtil.cleanHtmlTag(post.getPostContent()));
if (summaryText.length() > postSummary) {
final String summary = summaryText.substring(0, postSummary);
post.setPostSummary(summary);
} else {
post.setPostSummary(summaryText);
}
post.setPostDate(DateUtil.date());
post.setPostUpdate(DateUtil.date());
post.setUser(user);
post = postService.buildCategoriesAndTags(post, cateList, tagList);
post.setPostUrl(urlFilter(post.getPostUrl()));
//当没有选择文章缩略图的时候,自动分配一张内置的缩略图
@ -232,26 +217,12 @@ public class PostController extends BaseController {
@RequestParam("tagList") String tagList) {
//old data
final Post oldPost = postService.findByPostId(post.getPostId()).orElse(new Post());
post.setPostUpdate(new Date());
post.setPostViews(oldPost.getPostViews());
post.setPostContent(MarkdownUtils.renderMarkdown(post.getPostContentMd()));
post.setUser(oldPost.getUser());
if (null == post.getPostDate()) {
post.setPostDate(new Date());
}
//摘要字数
int postSummary = 50;
if (StrUtil.isNotEmpty(HaloConst.OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()))) {
postSummary = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()));
}
//设置文章摘要
final String summaryText = StrUtil.cleanBlank(HtmlUtil.cleanHtmlTag(post.getPostContent()));
if (summaryText.length() > postSummary) {
final String summary = summaryText.substring(0, postSummary);
post.setPostSummary(summary);
} else {
post.setPostSummary(summaryText);
}
post = postService.buildCategoriesAndTags(post, cateList, tagList);
//当没有选择文章缩略图的时候,自动分配一张内置的缩略图
if (StrUtil.equals(post.getPostThumbnail(), BlogPropertiesEnum.DEFAULT_THUMBNAIL.getProp())) {