mirror of https://github.com/halo-dev/halo
🎨 代码优化
parent
850a4f3956
commit
ccb80fdf12
|
@ -13,7 +13,18 @@ import lombok.Data;
|
||||||
@Data
|
@Data
|
||||||
public class QiNiuPutSet {
|
public class QiNiuPutSet {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图片大小
|
||||||
|
*/
|
||||||
private Long size;
|
private Long size;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 长
|
||||||
|
*/
|
||||||
private Integer w;
|
private Integer w;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 宽
|
||||||
|
*/
|
||||||
private Integer h;
|
private Integer h;
|
||||||
}
|
}
|
||||||
|
|
|
@ -113,8 +113,8 @@ public class AttachmentServiceImpl implements AttachmentService {
|
||||||
@CacheEvict(value = ATTACHMENTS_CACHE_NAME, allEntries = true, beforeInvocation = true)
|
@CacheEvict(value = ATTACHMENTS_CACHE_NAME, allEntries = true, beforeInvocation = true)
|
||||||
public Attachment remove(Long attachId) {
|
public Attachment remove(Long attachId) {
|
||||||
Optional<Attachment> attachment = this.findByAttachId(attachId);
|
Optional<Attachment> attachment = this.findByAttachId(attachId);
|
||||||
attachmentRepository.delete(attachment.get());
|
attachmentRepository.delete(attachment.orElse(null));
|
||||||
return attachment.get();
|
return attachment.orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -49,8 +49,8 @@ public class CategoryServiceImpl implements CategoryService {
|
||||||
@CacheEvict(value = POSTS_CACHE_NAME, allEntries = true, beforeInvocation = true)
|
@CacheEvict(value = POSTS_CACHE_NAME, allEntries = true, beforeInvocation = true)
|
||||||
public Category remove(Long cateId) {
|
public Category remove(Long cateId) {
|
||||||
final Optional<Category> category = this.findByCateId(cateId);
|
final Optional<Category> category = this.findByCateId(cateId);
|
||||||
categoryRepository.delete(category.get());
|
categoryRepository.delete(category.orElse(null));
|
||||||
return category.get();
|
return category.orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -54,7 +54,7 @@ public class CommentServiceImpl implements CommentService {
|
||||||
@CacheEvict(value = {COMMENTS_CACHE_NAME, POSTS_CACHE_NAME}, allEntries = true, beforeInvocation = true)
|
@CacheEvict(value = {COMMENTS_CACHE_NAME, POSTS_CACHE_NAME}, allEntries = true, beforeInvocation = true)
|
||||||
public Optional<Comment> remove(Long commentId) {
|
public Optional<Comment> remove(Long commentId) {
|
||||||
final Optional<Comment> comment = this.findCommentById(commentId);
|
final Optional<Comment> comment = this.findCommentById(commentId);
|
||||||
commentRepository.delete(comment.get());
|
commentRepository.delete(comment.orElse(null));
|
||||||
return comment;
|
return comment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import cc.ryanc.halo.model.dto.HaloConst;
|
||||||
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
|
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
|
||||||
import cc.ryanc.halo.service.MailService;
|
import cc.ryanc.halo.service.MailService;
|
||||||
import cc.ryanc.halo.utils.HaloUtils;
|
import cc.ryanc.halo.utils.HaloUtils;
|
||||||
|
import cn.hutool.core.text.StrBuilder;
|
||||||
import freemarker.template.Template;
|
import freemarker.template.Template;
|
||||||
import io.github.biezhi.ome.OhMyEmail;
|
import io.github.biezhi.ome.OhMyEmail;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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_HOST.getProp()),
|
||||||
HaloConst.OPTIONS.get(BlogPropertiesEnum.MAIL_SMTP_USERNAME.getProp()),
|
HaloConst.OPTIONS.get(BlogPropertiesEnum.MAIL_SMTP_USERNAME.getProp()),
|
||||||
HaloConst.OPTIONS.get(BlogPropertiesEnum.MAIL_SMTP_PASSWORD.getProp()));
|
HaloConst.OPTIONS.get(BlogPropertiesEnum.MAIL_SMTP_PASSWORD.getProp()));
|
||||||
String text = "";
|
StrBuilder text = new StrBuilder();
|
||||||
try {
|
try {
|
||||||
final Template template = freeMarker.getConfiguration().getTemplate(templateName);
|
final Template template = freeMarker.getConfiguration().getTemplate(templateName);
|
||||||
text = FreeMarkerTemplateUtils.processTemplateIntoString(template, content);
|
text.append(FreeMarkerTemplateUtils.processTemplateIntoString(template, content));
|
||||||
OhMyEmail.subject(subject)
|
OhMyEmail.subject(subject)
|
||||||
.from(HaloConst.OPTIONS.get(BlogPropertiesEnum.MAIL_FROM_NAME.getProp()))
|
.from(HaloConst.OPTIONS.get(BlogPropertiesEnum.MAIL_FROM_NAME.getProp()))
|
||||||
.to(to)
|
.to(to)
|
||||||
.html(text)
|
.html(text.toString())
|
||||||
.send();
|
.send();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
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_USERNAME.getProp()),
|
||||||
HaloConst.OPTIONS.get(BlogPropertiesEnum.MAIL_SMTP_PASSWORD.getProp()));
|
HaloConst.OPTIONS.get(BlogPropertiesEnum.MAIL_SMTP_PASSWORD.getProp()));
|
||||||
File file = new File(attachSrc);
|
File file = new File(attachSrc);
|
||||||
String text = "";
|
StrBuilder text = new StrBuilder();
|
||||||
try {
|
try {
|
||||||
final Template template = freeMarker.getConfiguration().getTemplate(templateName);
|
final Template template = freeMarker.getConfiguration().getTemplate(templateName);
|
||||||
text = FreeMarkerTemplateUtils.processTemplateIntoString(template, content);
|
text.append(FreeMarkerTemplateUtils.processTemplateIntoString(template, content));
|
||||||
OhMyEmail.subject(subject)
|
OhMyEmail.subject(subject)
|
||||||
.from(HaloConst.OPTIONS.get(BlogPropertiesEnum.MAIL_FROM_NAME.getProp()))
|
.from(HaloConst.OPTIONS.get(BlogPropertiesEnum.MAIL_FROM_NAME.getProp()))
|
||||||
.to(to)
|
.to(to)
|
||||||
.html(text)
|
.html(text.toString())
|
||||||
.attach(file, file.getName())
|
.attach(file, file.getName())
|
||||||
.send();
|
.send();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
@ -62,8 +62,8 @@ public class MenuServiceImpl implements MenuService {
|
||||||
@CacheEvict(value = MENUS_CACHE_NAME, allEntries = true, beforeInvocation = true)
|
@CacheEvict(value = MENUS_CACHE_NAME, allEntries = true, beforeInvocation = true)
|
||||||
public Menu remove(Long menuId) {
|
public Menu remove(Long menuId) {
|
||||||
final Optional<Menu> menu = this.findByMenuId(menuId);
|
final Optional<Menu> menu = this.findByMenuId(menuId);
|
||||||
menuRepository.delete(menu.get());
|
menuRepository.delete(menu.orElse(null));
|
||||||
return menu.get();
|
return menu.orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -5,6 +5,7 @@ import cc.ryanc.halo.model.domain.Post;
|
||||||
import cc.ryanc.halo.model.domain.Tag;
|
import cc.ryanc.halo.model.domain.Tag;
|
||||||
import cc.ryanc.halo.model.dto.Archive;
|
import cc.ryanc.halo.model.dto.Archive;
|
||||||
import cc.ryanc.halo.model.dto.HaloConst;
|
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.PostStatusEnum;
|
||||||
import cc.ryanc.halo.model.enums.PostTypeEnum;
|
import cc.ryanc.halo.model.enums.PostTypeEnum;
|
||||||
import cc.ryanc.halo.repository.PostRepository;
|
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.PostService;
|
||||||
import cc.ryanc.halo.service.TagService;
|
import cc.ryanc.halo.service.TagService;
|
||||||
import cc.ryanc.halo.utils.HaloUtils;
|
import cc.ryanc.halo.utils.HaloUtils;
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.http.HtmlUtil;
|
import cn.hutool.http.HtmlUtil;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -61,6 +63,18 @@ public class PostServiceImpl implements PostService {
|
||||||
@Override
|
@Override
|
||||||
@CacheEvict(value = {POSTS_CACHE_NAME, COMMENTS_CACHE_NAME}, allEntries = true, beforeInvocation = true)
|
@CacheEvict(value = {POSTS_CACHE_NAME, COMMENTS_CACHE_NAME}, allEntries = true, beforeInvocation = true)
|
||||||
public Post save(Post post) {
|
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);
|
return postRepository.save(post);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -347,17 +347,6 @@ public class AdminController extends BaseController {
|
||||||
post.setTags(tags);
|
post.setTags(tags);
|
||||||
post.setCategories(categories);
|
post.setCategories(categories);
|
||||||
post.setPostUrl(StrUtil.removeSuffix(file.getOriginalFilename(), ".md"));
|
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()) {
|
if (null == post.getPostDate()) {
|
||||||
post.setPostDate(new Date());
|
post.setPostDate(new Date());
|
||||||
}
|
}
|
||||||
|
|
|
@ -183,23 +183,8 @@ public class PostController extends BaseController {
|
||||||
final User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY);
|
final User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY);
|
||||||
try {
|
try {
|
||||||
post.setPostContent(MarkdownUtils.renderMarkdown(post.getPostContentMd()));
|
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.setPostDate(DateUtil.date());
|
||||||
post.setPostUpdate(DateUtil.date());
|
|
||||||
post.setUser(user);
|
post.setUser(user);
|
||||||
|
|
||||||
post = postService.buildCategoriesAndTags(post, cateList, tagList);
|
post = postService.buildCategoriesAndTags(post, cateList, tagList);
|
||||||
post.setPostUrl(urlFilter(post.getPostUrl()));
|
post.setPostUrl(urlFilter(post.getPostUrl()));
|
||||||
//当没有选择文章缩略图的时候,自动分配一张内置的缩略图
|
//当没有选择文章缩略图的时候,自动分配一张内置的缩略图
|
||||||
|
@ -232,26 +217,12 @@ public class PostController extends BaseController {
|
||||||
@RequestParam("tagList") String tagList) {
|
@RequestParam("tagList") String tagList) {
|
||||||
//old data
|
//old data
|
||||||
final Post oldPost = postService.findByPostId(post.getPostId()).orElse(new Post());
|
final Post oldPost = postService.findByPostId(post.getPostId()).orElse(new Post());
|
||||||
post.setPostUpdate(new Date());
|
|
||||||
post.setPostViews(oldPost.getPostViews());
|
post.setPostViews(oldPost.getPostViews());
|
||||||
post.setPostContent(MarkdownUtils.renderMarkdown(post.getPostContentMd()));
|
post.setPostContent(MarkdownUtils.renderMarkdown(post.getPostContentMd()));
|
||||||
post.setUser(oldPost.getUser());
|
post.setUser(oldPost.getUser());
|
||||||
if (null == post.getPostDate()) {
|
if (null == post.getPostDate()) {
|
||||||
post.setPostDate(new Date());
|
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);
|
post = postService.buildCategoriesAndTags(post, cateList, tagList);
|
||||||
//当没有选择文章缩略图的时候,自动分配一张内置的缩略图
|
//当没有选择文章缩略图的时候,自动分配一张内置的缩略图
|
||||||
if (StrUtil.equals(post.getPostThumbnail(), BlogPropertiesEnum.DEFAULT_THUMBNAIL.getProp())) {
|
if (StrUtil.equals(post.getPostThumbnail(), BlogPropertiesEnum.DEFAULT_THUMBNAIL.getProp())) {
|
||||||
|
|
Loading…
Reference in New Issue