Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add post backup for contents and content_patch_logs #1669

Merged
merged 2 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 2 additions & 18 deletions src/main/java/run/halo/app/service/ContentPatchLogService.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package run.halo.app.service;

import java.util.List;
import run.halo.app.exception.NotFoundException;
import run.halo.app.model.entity.Content.ContentDiff;
import run.halo.app.model.entity.Content.PatchedContent;
import run.halo.app.model.entity.ContentPatchLog;
import run.halo.app.service.base.CrudService;

/**
* Content patch log service.
*
* @author guqing
* @since 2022-01-04
*/
public interface ContentPatchLogService {
public interface ContentPatchLogService extends CrudService<ContentPatchLog, Integer> {

/**
* Create or update content patch log by post content.
Expand Down Expand Up @@ -42,13 +42,6 @@ public interface ContentPatchLogService {
*/
ContentDiff generateDiff(Integer postId, String content, String originalContent);

/**
* Creates or updates the {@link ContentPatchLog}.
*
* @param contentPatchLog param to create or update
*/
void save(ContentPatchLog contentPatchLog);

/**
* Gets the patch log record of the draft status of the content by post id.
*
Expand All @@ -57,15 +50,6 @@ public interface ContentPatchLogService {
*/
ContentPatchLog getDraftByPostId(Integer postId);

/**
* Gets content patch log by id.
*
* @param id id
* @return a content patch log
* @throws NotFoundException if record not found.
*/
ContentPatchLog getById(Integer id);

/**
* Gets content patch log by post id.
*
Expand Down
33 changes: 32 additions & 1 deletion src/main/java/run/halo/app/service/impl/BackupServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -47,6 +48,8 @@
import run.halo.app.model.entity.Attachment;
import run.halo.app.model.entity.Category;
import run.halo.app.model.entity.CommentBlackList;
import run.halo.app.model.entity.Content;
import run.halo.app.model.entity.ContentPatchLog;
import run.halo.app.model.entity.Journal;
import run.halo.app.model.entity.JournalComment;
import run.halo.app.model.entity.Link;
Expand All @@ -73,6 +76,8 @@
import run.halo.app.service.BackupService;
import run.halo.app.service.CategoryService;
import run.halo.app.service.CommentBlackListService;
import run.halo.app.service.ContentPatchLogService;
import run.halo.app.service.ContentService;
import run.halo.app.service.JournalCommentService;
import run.halo.app.service.JournalService;
import run.halo.app.service.LinkService;
Expand All @@ -96,6 +101,7 @@
import run.halo.app.utils.FileUtils;
import run.halo.app.utils.HaloUtils;
import run.halo.app.utils.JsonUtils;
import run.halo.app.utils.VersionUtil;

/**
* Backup service implementation.
Expand Down Expand Up @@ -141,6 +147,10 @@ public class BackupServiceImpl implements BackupService {

private final PostService postService;

private final ContentService contentService;

private final ContentPatchLogService contentPatchLogService;

private final PostCategoryService postCategoryService;

private final PostCommentService postCommentService;
Expand Down Expand Up @@ -171,7 +181,9 @@ public BackupServiceImpl(AttachmentService attachmentService, CategoryService ca
CommentBlackListService commentBlackListService, JournalService journalService,
JournalCommentService journalCommentService, LinkService linkService, LogService logService,
MenuService menuService, OptionService optionService, PhotoService photoService,
PostService postService, PostCategoryService postCategoryService,
PostService postService, ContentService contentService,
ContentPatchLogService contentPatchLogService,
PostCategoryService postCategoryService,
PostCommentService postCommentService, PostMetaService postMetaService,
PostTagService postTagService, SheetService sheetService,
SheetCommentService sheetCommentService, SheetMetaService sheetMetaService,
Expand All @@ -189,6 +201,8 @@ public BackupServiceImpl(AttachmentService attachmentService, CategoryService ca
this.optionService = optionService;
this.photoService = photoService;
this.postService = postService;
this.contentService = contentService;
this.contentPatchLogService = contentPatchLogService;
this.postCategoryService = postCategoryService;
this.postCommentService = postCommentService;
this.postMetaService = postMetaService;
Expand Down Expand Up @@ -358,6 +372,8 @@ public BackupDTO exportData() {
data.put("options", optionService.listAll());
data.put("photos", photoService.listAll());
data.put("posts", postService.listAll());
data.put("contents", contentService.listAll());
data.put("content_patch_logs", contentPatchLogService.listAll());
data.put("post_categories", postCategoryService.listAll());
data.put("post_comments", postCommentService.listAll());
data.put("post_metas", postMetaService.listAll());
Expand Down Expand Up @@ -438,6 +454,11 @@ public void importData(MultipartFile file) throws IOException {
};
HashMap<String, Object> data = mapper.readValue(jsonContent, typeRef);

String version = (String) Objects.requireNonNullElse(data.get("version"), "");
if (!VersionUtil.hasSameMajorAndMinorVersion(HaloConst.HALO_VERSION, version)) {
throw new IllegalArgumentException("导入数据的主次版本号与当前系统版本号不匹配,不支持导入!");
}

List<Attachment> attachments = Arrays.asList(mapper
.readValue(mapper.writeValueAsString(data.get("attachments")), Attachment[].class));
attachmentService.createInBatch(attachments);
Expand Down Expand Up @@ -490,6 +511,16 @@ public void importData(MultipartFile file) throws IOException {
.asList(mapper.readValue(mapper.writeValueAsString(data.get("posts")), Post[].class));
postService.createInBatch(posts);

List<Content> contents = Arrays
.asList(
mapper.readValue(mapper.writeValueAsString(data.get("contents")), Content[].class));
contentService.createInBatch(contents);

List<ContentPatchLog> contentPatchLogs = Arrays
.asList(mapper.readValue(mapper.writeValueAsString(data.get("content_patch_logs")),
ContentPatchLog[].class));
contentPatchLogService.createInBatch(contentPatchLogs);

List<PostCategory> postCategories = Arrays.asList(mapper
.readValue(mapper.writeValueAsString(data.get("post_categories")),
PostCategory[].class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import run.halo.app.repository.ContentPatchLogRepository;
import run.halo.app.repository.ContentRepository;
import run.halo.app.service.ContentPatchLogService;
import run.halo.app.service.base.AbstractCrudService;
import run.halo.app.utils.PatchUtils;

/**
Expand All @@ -25,7 +26,8 @@
* @since 2022-01-04
*/
@Service
public class ContentPatchLogServiceImpl implements ContentPatchLogService {
public class ContentPatchLogServiceImpl extends AbstractCrudService<ContentPatchLog, Integer>
implements ContentPatchLogService {

/**
* base version of content patch log.
Expand All @@ -38,6 +40,7 @@ public class ContentPatchLogServiceImpl implements ContentPatchLogService {

public ContentPatchLogServiceImpl(ContentPatchLogRepository contentPatchLogRepository,
ContentRepository contentRepository) {
super(contentPatchLogRepository);
this.contentPatchLogRepository = contentPatchLogRepository;
this.contentRepository = contentRepository;
}
Expand Down Expand Up @@ -82,12 +85,6 @@ private Integer getVersionByPostId(Integer postId) {
return version;
}

@Override
@Transactional(rollbackFor = Exception.class)
public void save(ContentPatchLog contentPatchLog) {
contentPatchLogRepository.save(contentPatchLog);
}

private ContentPatchLog createDraftContent(Integer postId, Integer version,
String formatContent, String originalContent) {
ContentPatchLog contentPatchLog =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public Content publishContent(Integer postId) {
}
contentPatchLog.setStatus(PostStatus.PUBLISHED);
contentPatchLog.setPublishTime(new Date());
contentPatchLogService.save(contentPatchLog);
contentPatchLogService.create(contentPatchLog);

Content postContent = getById(postId);
postContent.setPatchLogId(contentPatchLog.getId());
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/run/halo/app/service/impl/PostServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,8 @@ public List<PostMarkdownVO> listPostMarkdowns() {
List<Post> allPostList = listAll();
List<PostMarkdownVO> result = new ArrayList<>(allPostList.size());
for (Post post : allPostList) {
Content postContent = getContentById(post.getId());
post.setContent(PatchedContent.of(postContent));
result.add(convertToPostMarkdownVo(post));
}
return result;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/run/halo/app/utils/VersionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,17 @@ public static boolean compareVersion(String current, String require) {
return leftVersion.compareTo(rightVersion) >= 0;
}

/**
* Compare two versions to see if they have the same major and minor versions.
*
* @param left version A to compare
* @param right version B to compare
* @return {@code true} if they have the same major and minor version.
*/
public static boolean hasSameMajorAndMinorVersion(String left, String right) {
Version leftVersion = Version.resolve(left).orElse(Version.emptyVersion());
Version rightVersion = Version.resolve(right).orElse(Version.emptyVersion());
return leftVersion.getMajor() == rightVersion.getMajor()
&& leftVersion.getMinor() == rightVersion.getMinor();
}
}
20 changes: 20 additions & 0 deletions src/test/java/run/halo/app/utils/VersionUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package run.halo.app.utils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -33,4 +34,23 @@ void unknownVersionCompareTest() {
assertTrue(VersionUtil.compareVersion(HaloConst.UNKNOWN_VERSION, randomVersion));
}

@Test
void hasSameMajorAndMinorVersionTest() {
assertThat(
VersionUtil.hasSameMajorAndMinorVersion(HaloConst.UNKNOWN_VERSION, "1.4.0")).isFalse();

assertThat(VersionUtil.hasSameMajorAndMinorVersion("1.3.1", "1.5.8")).isFalse();

assertThat(VersionUtil.hasSameMajorAndMinorVersion("1.2.4.alpha-1", "1.3.5")).isFalse();

assertThat(VersionUtil.hasSameMajorAndMinorVersion("0.0.4", "0.0.1")).isTrue();

assertThat(VersionUtil.hasSameMajorAndMinorVersion("1.0.4", "1.0.5-alpha.1")).isTrue();

assertThat(VersionUtil.hasSameMajorAndMinorVersion("1.0.4", "1.0.5-rc.1")).isTrue();

assertThat(VersionUtil.hasSameMajorAndMinorVersion("2.0.0", "2.0.1")).isTrue();

assertThat(VersionUtil.hasSameMajorAndMinorVersion(null, null)).isTrue();
}
}