From 305d9075310cb5312bd523f2f3e9f22790026b3c Mon Sep 17 00:00:00 2001 From: ruibaby Date: Sat, 28 Dec 2019 21:57:51 +0800 Subject: [PATCH] feat: support replace url method. --- .../halo/app/service/AttachmentService.java | 9 +++++++++ .../run/halo/app/service/OptionService.java | 9 +++++++++ .../run/halo/app/service/PhotoService.java | 9 +++++++++ .../halo/app/service/ThemeSettingService.java | 9 +++++++++ .../app/service/base/BaseCommentService.java | 9 +++++++++ .../app/service/base/BasePostService.java | 10 ++++++++++ .../service/impl/AttachmentServiceImpl.java | 12 ++++++++++++ .../service/impl/BaseCommentServiceImpl.java | 12 ++++++++++++ .../app/service/impl/BasePostServiceImpl.java | 19 +++++++++++++++---- .../app/service/impl/OptionServiceImpl.java | 13 +++++++++++++ .../app/service/impl/PhotoServiceImpl.java | 18 ++++++++++++++---- .../service/impl/ThemeSettingServiceImpl.java | 11 +++++++++++ 12 files changed, 132 insertions(+), 8 deletions(-) diff --git a/src/main/java/run/halo/app/service/AttachmentService.java b/src/main/java/run/halo/app/service/AttachmentService.java index a62a93c0e..c232a06cf 100644 --- a/src/main/java/run/halo/app/service/AttachmentService.java +++ b/src/main/java/run/halo/app/service/AttachmentService.java @@ -82,4 +82,13 @@ public interface AttachmentService extends CrudService { * @return list of type. */ List listAllType(); + + /** + * Replace attachment url in batch. + * + * @param oldUrl old blog url. + * @param newUrl new blog url. + * @return replaced attachments. + */ + List replaceUrl(@NonNull String oldUrl, @NonNull String newUrl); } diff --git a/src/main/java/run/halo/app/service/OptionService.java b/src/main/java/run/halo/app/service/OptionService.java index 7f8763425..ef29ca479 100755 --- a/src/main/java/run/halo/app/service/OptionService.java +++ b/src/main/java/run/halo/app/service/OptionService.java @@ -338,6 +338,15 @@ public interface OptionService extends CrudService { */ long getBirthday(); + /** + * Replace option url in batch. + * + * @param oldUrl old blog url. + * @param newUrl new blog url. + * @return replaced options. + */ + List replaceUrl(@NonNull String oldUrl, @NonNull String newUrl); + /** * Converts to option output dto. * diff --git a/src/main/java/run/halo/app/service/PhotoService.java b/src/main/java/run/halo/app/service/PhotoService.java index 105fd99e5..2437affbb 100644 --- a/src/main/java/run/halo/app/service/PhotoService.java +++ b/src/main/java/run/halo/app/service/PhotoService.java @@ -80,4 +80,13 @@ public interface PhotoService extends CrudService { * @return list of teams */ List listAllTeams(); + + /** + * Replace photo url in batch. + * + * @param oldUrl old blog url. + * @param newUrl new blog url. + * @return replaced photos. + */ + List replaceUrl(@NonNull String oldUrl, @NonNull String newUrl); } diff --git a/src/main/java/run/halo/app/service/ThemeSettingService.java b/src/main/java/run/halo/app/service/ThemeSettingService.java index bec2c5af1..04816e536 100644 --- a/src/main/java/run/halo/app/service/ThemeSettingService.java +++ b/src/main/java/run/halo/app/service/ThemeSettingService.java @@ -55,4 +55,13 @@ public interface ThemeSettingService { */ @NonNull Map listAsMapBy(@NonNull String themeId); + + /** + * Replace theme setting url in batch. + * + * @param oldUrl old blog url. + * @param newUrl new blog url. + * @return replaced theme settings. + */ + List replaceUrl(@NonNull String oldUrl, @NonNull String newUrl); } diff --git a/src/main/java/run/halo/app/service/base/BaseCommentService.java b/src/main/java/run/halo/app/service/base/BaseCommentService.java index dea641a6f..58e48ec72 100644 --- a/src/main/java/run/halo/app/service/base/BaseCommentService.java +++ b/src/main/java/run/halo/app/service/base/BaseCommentService.java @@ -285,4 +285,13 @@ public interface BaseCommentService extends CrudSer */ Page filterIpAddress(@NonNull Page commentPage); + /** + * Replace comment url in batch. + * + * @param oldUrl old blog url. + * @param newUrl new blog url. + * @return replaced comments. + */ + List replaceUrl(@NonNull String oldUrl, @NonNull String newUrl); + } diff --git a/src/main/java/run/halo/app/service/base/BasePostService.java b/src/main/java/run/halo/app/service/base/BasePostService.java index 6c737188f..9178278b8 100644 --- a/src/main/java/run/halo/app/service/base/BasePostService.java +++ b/src/main/java/run/halo/app/service/base/BasePostService.java @@ -245,4 +245,14 @@ public interface BasePostService extends CrudService updateStatusByIds(@NonNull List ids, @NonNull PostStatus status); + + /** + * Replace post blog url in batch. + * + * @param oldUrl old blog url. + * @param newUrl new blog url. + * @return replaced posts. + */ + @NonNull + List replaceUrl(@NonNull String oldUrl, @NonNull String newUrl); } diff --git a/src/main/java/run/halo/app/service/impl/AttachmentServiceImpl.java b/src/main/java/run/halo/app/service/impl/AttachmentServiceImpl.java index ed19dcb83..4f1bd8d26 100644 --- a/src/main/java/run/halo/app/service/impl/AttachmentServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/AttachmentServiceImpl.java @@ -183,6 +183,18 @@ public class AttachmentServiceImpl extends AbstractCrudService replaceUrl(String oldUrl, String newUrl) { + List attachments = listAll(); + List replaced = new ArrayList<>(); + attachments.forEach(attachment -> { + attachment.setPath(attachment.getPath().replaceAll(oldUrl, newUrl)); + attachment.setThumbPath(attachment.getThumbPath().replaceAll(oldUrl, newUrl)); + replaced.add(attachment); + }); + return updateInBatch(replaced); + } + @Override public Attachment create(Attachment attachment) { Assert.notNull(attachment, "Attachment must not be null"); diff --git a/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java b/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java index f2137ad24..12cb86733 100644 --- a/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java @@ -584,6 +584,18 @@ public abstract class BaseCommentServiceImpl extend return commentPage; } + @Override + public List replaceUrl(String oldUrl, String newUrl) { + List comments = listAll(); + List replaced = new ArrayList<>(); + comments.forEach(comment -> { + comment.setAuthorUrl(comment.getAuthorUrl().replaceAll(oldUrl, newUrl)); + replaced.add(comment); + }); + List updated = updateInBatch(replaced); + return convertTo(updated); + } + /** * Get children comments recursively. * diff --git a/src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java b/src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java index 43436dbaa..e4903953a 100644 --- a/src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java @@ -29,10 +29,7 @@ import run.halo.app.utils.HaloUtils; import run.halo.app.utils.MarkdownUtils; import run.halo.app.utils.ServiceUtils; -import java.util.Collections; -import java.util.Date; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -395,6 +392,20 @@ public abstract class BasePostServiceImpl extends Abstrac }).collect(Collectors.toList()); } + @Override + public List replaceUrl(String oldUrl, String newUrl) { + List posts = listAll(); + List replaced = new ArrayList<>(); + posts.forEach(post -> { + post.setThumbnail(post.getThumbnail().replaceAll(oldUrl, newUrl)); + post.setOriginalContent(post.getOriginalContent().replaceAll(oldUrl, newUrl)); + post.setFormatContent(post.getFormatContent().replaceAll(oldUrl, newUrl)); + replaced.add(post); + }); + List updated = updateInBatch(replaced); + return updated.stream().map(this::convertToDetail).collect(Collectors.toList()); + } + @Override public POST create(POST post) { // Check title diff --git a/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java b/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java index 4f5c7a79b..c627307d5 100644 --- a/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java @@ -36,6 +36,7 @@ import run.halo.app.utils.ValidationUtils; import javax.persistence.criteria.Predicate; import java.util.*; +import java.util.stream.Collectors; /** * OptionService implementation class @@ -460,6 +461,18 @@ public class OptionServiceImpl extends AbstractCrudService impl }); } + @Override + public List replaceUrl(String oldUrl, String newUrl) { + List