feat: support cdn for post content image.

pull/435/head
ruibaby 2019-12-20 21:06:38 +08:00
parent 0c189ee06c
commit f60938ec87
1 changed files with 33 additions and 2 deletions

View File

@ -5,6 +5,8 @@ import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
@ -26,6 +28,7 @@ import run.halo.app.model.entity.*;
import run.halo.app.model.enums.LogType; import run.halo.app.model.enums.LogType;
import run.halo.app.model.enums.PostStatus; import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.params.PostQuery; import run.halo.app.model.params.PostQuery;
import run.halo.app.model.properties.OtherProperties;
import run.halo.app.model.vo.ArchiveMonthVO; import run.halo.app.model.vo.ArchiveMonthVO;
import run.halo.app.model.vo.ArchiveYearVO; import run.halo.app.model.vo.ArchiveYearVO;
import run.halo.app.model.vo.PostDetailVO; import run.halo.app.model.vo.PostDetailVO;
@ -73,6 +76,8 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
private final PostMetaService postMetaService; private final PostMetaService postMetaService;
private final OptionService optionService;
public PostServiceImpl(PostRepository postRepository, public PostServiceImpl(PostRepository postRepository,
TagService tagService, TagService tagService,
CategoryService categoryService, CategoryService categoryService,
@ -80,8 +85,8 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
PostCategoryService postCategoryService, PostCategoryService postCategoryService,
PostCommentService postCommentService, PostCommentService postCommentService,
ApplicationEventPublisher eventPublisher, ApplicationEventPublisher eventPublisher,
OptionService optionService, PostMetaService postMetaService,
PostMetaService postMetaService) { OptionService optionService) {
super(postRepository, optionService); super(postRepository, optionService);
this.postRepository = postRepository; this.postRepository = postRepository;
this.tagService = tagService; this.tagService = tagService;
@ -91,6 +96,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
this.postCommentService = postCommentService; this.postCommentService = postCommentService;
this.eventPublisher = eventPublisher; this.eventPublisher = eventPublisher;
this.postMetaService = postMetaService; this.postMetaService = postMetaService;
this.optionService = optionService;
} }
@Override @Override
@ -451,6 +457,11 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
// Get post meta list map // Get post meta list map
Map<Integer, List<PostMeta>> postMetaListMap = postMetaService.listPostMetaAsMap(postIds); Map<Integer, List<PostMeta>> postMetaListMap = postMetaService.listPostMetaAsMap(postIds);
// Get cdn domain
String cdnDomain = optionService.getByPropertyOrDefault(OtherProperties.CDN_DOMAIN, String.class, StringUtils.EMPTY);
String blogUrl = optionService.getBlogBaseUrl();
return postPage.map(post -> { return postPage.map(post -> {
PostListVO postListVO = new PostListVO().convertFrom(post); PostListVO postListVO = new PostListVO().convertFrom(post);
@ -487,6 +498,10 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
// Set comment count // Set comment count
postListVO.setCommentCount(commentCountMap.getOrDefault(post.getId(), 0L)); postListVO.setCommentCount(commentCountMap.getOrDefault(post.getId(), 0L));
if (StringUtils.isNotEmpty(cdnDomain) && StringUtils.isNotEmpty(postListVO.getThumbnail())) {
postListVO.setThumbnail(postListVO.getThumbnail().replace(blogUrl, cdnDomain));
}
return postListVO; return postListVO;
}); });
} }
@ -546,6 +561,22 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
// Get post meta ids // Get post meta ids
postDetailVO.setPostMetaIds(postMetaIds); postDetailVO.setPostMetaIds(postMetaIds);
postDetailVO.setPostMetas(postMetaService.convertTo(postMetaList)); postDetailVO.setPostMetas(postMetaService.convertTo(postMetaList));
// Get cdn domain
String cdnDomain = optionService.getByPropertyOrDefault(OtherProperties.CDN_DOMAIN, String.class, StringUtils.EMPTY);
String blogUrl = optionService.getBlogBaseUrl();
if (StringUtils.isNotEmpty(cdnDomain) && StringUtils.isNotEmpty(postDetailVO.getThumbnail())) {
postDetailVO.setThumbnail(postDetailVO.getThumbnail().replace(blogUrl, cdnDomain));
}
Document document = Jsoup.parse(postDetailVO.getFormatContent());
document.select("img").forEach(img -> {
String src = img.attr("src");
img.attr("src", src.replace(blogUrl, cdnDomain));
});
postDetailVO.setFormatContent(document.html());
return postDetailVO; return postDetailVO;
} }