feat: support replace url method.

pull/471/head^2
ruibaby 2019-12-28 21:57:51 +08:00
parent c37f36f61e
commit 305d907531
12 changed files with 132 additions and 8 deletions

View File

@ -82,4 +82,13 @@ public interface AttachmentService extends CrudService<Attachment, Integer> {
* @return list of type.
*/
List<AttachmentType> listAllType();
/**
* Replace attachment url in batch.
*
* @param oldUrl old blog url.
* @param newUrl new blog url.
* @return replaced attachments.
*/
List<Attachment> replaceUrl(@NonNull String oldUrl, @NonNull String newUrl);
}

View File

@ -338,6 +338,15 @@ public interface OptionService extends CrudService<Option, Integer> {
*/
long getBirthday();
/**
* Replace option url in batch.
*
* @param oldUrl old blog url.
* @param newUrl new blog url.
* @return replaced options.
*/
List<OptionDTO> replaceUrl(@NonNull String oldUrl, @NonNull String newUrl);
/**
* Converts to option output dto.
*

View File

@ -80,4 +80,13 @@ public interface PhotoService extends CrudService<Photo, Integer> {
* @return list of teams
*/
List<String> listAllTeams();
/**
* Replace photo url in batch.
*
* @param oldUrl old blog url.
* @param newUrl new blog url.
* @return replaced photos.
*/
List<PhotoDTO> replaceUrl(@NonNull String oldUrl, @NonNull String newUrl);
}

View File

@ -55,4 +55,13 @@ public interface ThemeSettingService {
*/
@NonNull
Map<String, Object> 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<ThemeSetting> replaceUrl(@NonNull String oldUrl, @NonNull String newUrl);
}

View File

@ -285,4 +285,13 @@ public interface BaseCommentService<COMMENT extends BaseComment> extends CrudSer
*/
<T extends BaseCommentDTO> Page<T> filterIpAddress(@NonNull Page<T> commentPage);
/**
* Replace comment url in batch.
*
* @param oldUrl old blog url.
* @param newUrl new blog url.
* @return replaced comments.
*/
List<BaseCommentDTO> replaceUrl(@NonNull String oldUrl, @NonNull String newUrl);
}

View File

@ -245,4 +245,14 @@ public interface BasePostService<POST extends BasePost> extends CrudService<POST
*/
@NonNull
List<POST> updateStatusByIds(@NonNull List<Integer> 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<BasePostDetailDTO> replaceUrl(@NonNull String oldUrl, @NonNull String newUrl);
}

View File

@ -183,6 +183,18 @@ public class AttachmentServiceImpl extends AbstractCrudService<Attachment, Integ
return attachmentRepository.findAllType();
}
@Override
public List<Attachment> replaceUrl(String oldUrl, String newUrl) {
List<Attachment> attachments = listAll();
List<Attachment> 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");

View File

@ -584,6 +584,18 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
return commentPage;
}
@Override
public List<BaseCommentDTO> replaceUrl(String oldUrl, String newUrl) {
List<COMMENT> comments = listAll();
List<COMMENT> replaced = new ArrayList<>();
comments.forEach(comment -> {
comment.setAuthorUrl(comment.getAuthorUrl().replaceAll(oldUrl, newUrl));
replaced.add(comment);
});
List<COMMENT> updated = updateInBatch(replaced);
return convertTo(updated);
}
/**
* Get children comments recursively.
*

View File

@ -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<POST extends BasePost> extends Abstrac
}).collect(Collectors.toList());
}
@Override
public List<BasePostDetailDTO> replaceUrl(String oldUrl, String newUrl) {
List<POST> posts = listAll();
List<POST> 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<POST> updated = updateInBatch(replaced);
return updated.stream().map(this::convertToDetail).collect(Collectors.toList());
}
@Override
public POST create(POST post) {
// Check title

View File

@ -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<Option, Integer> impl
});
}
@Override
public List<OptionDTO> replaceUrl(String oldUrl, String newUrl) {
List<Option> options = listAll();
List<Option> replaced = new ArrayList<>();
options.forEach(option -> {
option.setValue(option.getValue().replaceAll(oldUrl, newUrl));
replaced.add(option);
});
List<Option> updated = updateInBatch(replaced);
return updated.stream().map(this::convertToDto).collect(Collectors.toList());
}
@Override
public OptionSimpleDTO convertToDto(Option option) {
Assert.notNull(option, "Option must not be null");

View File

@ -19,10 +19,7 @@ import run.halo.app.service.base.AbstractCrudService;
import run.halo.app.utils.ServiceUtils;
import javax.persistence.criteria.Predicate;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
/**
@ -114,6 +111,19 @@ public class PhotoServiceImpl extends AbstractCrudService<Photo, Integer> implem
return photoRepository.findAllTeams();
}
@Override
public List<PhotoDTO> replaceUrl(String oldUrl, String newUrl) {
List<Photo> photos = listAll();
List<Photo> replaced = new ArrayList<>();
photos.forEach(photo -> {
photo.setThumbnail(photo.getThumbnail().replace(oldUrl, newUrl));
photo.setUrl(photo.getUrl().replaceAll(oldUrl, newUrl));
replaced.add(photo);
});
List<Photo> updated = updateInBatch(replaced);
return updated.stream().map(photo -> (PhotoDTO) new PhotoDTO().convertFrom(photo)).collect(Collectors.toList());
}
@NonNull
private Specification<Photo> buildSpecByQuery(@NonNull PhotoQuery photoQuery) {
Assert.notNull(photoQuery, "Photo query must not be null");

View File

@ -160,6 +160,17 @@ public class ThemeSettingServiceImpl extends AbstractCrudService<ThemeSetting, I
return result;
}
@Override
public List<ThemeSetting> replaceUrl(String oldUrl, String newUrl) {
List<ThemeSetting> themeSettings = listAll();
List<ThemeSetting> replaced = new ArrayList<>();
themeSettings.forEach(themeSetting -> {
themeSetting.setValue(themeSetting.getValue().replaceAll(oldUrl, newUrl));
replaced.add(themeSetting);
});
return updateInBatch(replaced);
}
/**
* Gets config item map. (key: item name, value: item)
*