mirror of https://github.com/halo-dev/halo
feat: add data process apis.
parent
305d907531
commit
eed14995aa
|
@ -0,0 +1,50 @@
|
||||||
|
package run.halo.app.controller.admin.api;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import run.halo.app.service.DataProcessService;
|
||||||
|
import run.halo.app.service.ThemeSettingService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author ryanwang
|
||||||
|
* @date 2019-12-29
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/admin/data/process")
|
||||||
|
public class DataProcessController {
|
||||||
|
|
||||||
|
private final DataProcessService dataProcessService;
|
||||||
|
|
||||||
|
private final ThemeSettingService themeSettingService;
|
||||||
|
|
||||||
|
public DataProcessController(DataProcessService dataProcessService,
|
||||||
|
ThemeSettingService themeSettingService) {
|
||||||
|
this.dataProcessService = dataProcessService;
|
||||||
|
this.themeSettingService = themeSettingService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("url/replace")
|
||||||
|
@ApiOperation("Replace url in all table.")
|
||||||
|
public void replaceUrl(@RequestParam("oldUrl") String oldUrl,
|
||||||
|
@RequestParam("newUrl") String newUrl) {
|
||||||
|
dataProcessService.replaceAllUrl(oldUrl, newUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("themes/settings/inactivated")
|
||||||
|
@ApiOperation("Delete inactivated theme settings.")
|
||||||
|
public void deleteInactivatedThemeSettings() {
|
||||||
|
themeSettingService.deleteInactivated();
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("tags/unused")
|
||||||
|
@ApiOperation("Delete unused tags")
|
||||||
|
public void deleteUnusedTags() {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("categories/unused")
|
||||||
|
@ApiOperation("Delete unused categories")
|
||||||
|
public void deleteUnusedCategories() {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
}
|
|
@ -42,4 +42,11 @@ public interface ThemeSettingRepository extends BaseRepository<ThemeSetting, Int
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
Optional<ThemeSetting> findByThemeIdAndKey(@NonNull String themeId, @NonNull String key);
|
Optional<ThemeSetting> findByThemeIdAndKey(@NonNull String themeId, @NonNull String key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes inactivated theme settings.
|
||||||
|
*
|
||||||
|
* @param activatedThemeId activated theme id.
|
||||||
|
*/
|
||||||
|
void deleteByThemeIdIsNot(@NonNull String activatedThemeId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
package run.halo.app.service;
|
||||||
|
|
||||||
|
import org.springframework.lang.NonNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data process service interface.
|
||||||
|
*
|
||||||
|
* @author ryanwang
|
||||||
|
* @date 2019-12-29
|
||||||
|
*/
|
||||||
|
public interface DataProcessService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace all url.
|
||||||
|
*
|
||||||
|
* @param oldUrl old url must not be null.
|
||||||
|
* @param newUrl new url must not be null.
|
||||||
|
*/
|
||||||
|
void replaceAllUrl(@NonNull String oldUrl, @NonNull String newUrl);
|
||||||
|
}
|
|
@ -64,4 +64,9 @@ public interface ThemeSettingService {
|
||||||
* @return replaced theme settings.
|
* @return replaced theme settings.
|
||||||
*/
|
*/
|
||||||
List<ThemeSetting> replaceUrl(@NonNull String oldUrl, @NonNull String newUrl);
|
List<ThemeSetting> replaceUrl(@NonNull String oldUrl, @NonNull String newUrl);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete unused theme setting.
|
||||||
|
*/
|
||||||
|
void deleteInactivated();
|
||||||
}
|
}
|
||||||
|
|
|
@ -188,8 +188,12 @@ public class AttachmentServiceImpl extends AbstractCrudService<Attachment, Integ
|
||||||
List<Attachment> attachments = listAll();
|
List<Attachment> attachments = listAll();
|
||||||
List<Attachment> replaced = new ArrayList<>();
|
List<Attachment> replaced = new ArrayList<>();
|
||||||
attachments.forEach(attachment -> {
|
attachments.forEach(attachment -> {
|
||||||
attachment.setPath(attachment.getPath().replaceAll(oldUrl, newUrl));
|
if (StringUtils.isNotEmpty(attachment.getPath())) {
|
||||||
attachment.setThumbPath(attachment.getThumbPath().replaceAll(oldUrl, newUrl));
|
attachment.setPath(attachment.getPath().replaceAll(oldUrl, newUrl));
|
||||||
|
}
|
||||||
|
if (StringUtils.isNotEmpty(attachment.getThumbPath())) {
|
||||||
|
attachment.setThumbPath(attachment.getThumbPath().replaceAll(oldUrl, newUrl));
|
||||||
|
}
|
||||||
replaced.add(attachment);
|
replaced.add(attachment);
|
||||||
});
|
});
|
||||||
return updateInBatch(replaced);
|
return updateInBatch(replaced);
|
||||||
|
|
|
@ -589,7 +589,9 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
|
||||||
List<COMMENT> comments = listAll();
|
List<COMMENT> comments = listAll();
|
||||||
List<COMMENT> replaced = new ArrayList<>();
|
List<COMMENT> replaced = new ArrayList<>();
|
||||||
comments.forEach(comment -> {
|
comments.forEach(comment -> {
|
||||||
comment.setAuthorUrl(comment.getAuthorUrl().replaceAll(oldUrl, newUrl));
|
if (StringUtils.isNotEmpty(comment.getAuthorUrl())) {
|
||||||
|
comment.setAuthorUrl(comment.getAuthorUrl().replaceAll(oldUrl, newUrl));
|
||||||
|
}
|
||||||
replaced.add(comment);
|
replaced.add(comment);
|
||||||
});
|
});
|
||||||
List<COMMENT> updated = updateInBatch(replaced);
|
List<COMMENT> updated = updateInBatch(replaced);
|
||||||
|
|
|
@ -397,9 +397,15 @@ public abstract class BasePostServiceImpl<POST extends BasePost> extends Abstrac
|
||||||
List<POST> posts = listAll();
|
List<POST> posts = listAll();
|
||||||
List<POST> replaced = new ArrayList<>();
|
List<POST> replaced = new ArrayList<>();
|
||||||
posts.forEach(post -> {
|
posts.forEach(post -> {
|
||||||
post.setThumbnail(post.getThumbnail().replaceAll(oldUrl, newUrl));
|
if (StringUtils.isNotEmpty(post.getThumbnail())) {
|
||||||
post.setOriginalContent(post.getOriginalContent().replaceAll(oldUrl, newUrl));
|
post.setThumbnail(post.getThumbnail().replaceAll(oldUrl, newUrl));
|
||||||
post.setFormatContent(post.getFormatContent().replaceAll(oldUrl, newUrl));
|
}
|
||||||
|
if (StringUtils.isNotEmpty(post.getOriginalContent())) {
|
||||||
|
post.setOriginalContent(post.getOriginalContent().replaceAll(oldUrl, newUrl));
|
||||||
|
}
|
||||||
|
if (StringUtils.isNotEmpty(post.getFormatContent())) {
|
||||||
|
post.setFormatContent(post.getFormatContent().replaceAll(oldUrl, newUrl));
|
||||||
|
}
|
||||||
replaced.add(post);
|
replaced.add(post);
|
||||||
});
|
});
|
||||||
List<POST> updated = updateInBatch(replaced);
|
List<POST> updated = updateInBatch(replaced);
|
||||||
|
|
|
@ -0,0 +1,65 @@
|
||||||
|
package run.halo.app.service.impl;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import run.halo.app.service.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DataProcessService implementation.
|
||||||
|
*
|
||||||
|
* @author ryanwang
|
||||||
|
* @date 2019-12-29
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class DataProcessServiceImpl implements DataProcessService {
|
||||||
|
|
||||||
|
private final PostService postService;
|
||||||
|
|
||||||
|
private final SheetService sheetService;
|
||||||
|
|
||||||
|
private final PostCommentService postCommentService;
|
||||||
|
|
||||||
|
private final SheetCommentService sheetCommentService;
|
||||||
|
|
||||||
|
private final JournalCommentService journalCommentService;
|
||||||
|
|
||||||
|
private final AttachmentService attachmentService;
|
||||||
|
|
||||||
|
private final OptionService optionService;
|
||||||
|
|
||||||
|
private final PhotoService photoService;
|
||||||
|
|
||||||
|
private final ThemeSettingService themeSettingService;
|
||||||
|
|
||||||
|
public DataProcessServiceImpl(PostService postService,
|
||||||
|
SheetService sheetService,
|
||||||
|
PostCommentService postCommentService,
|
||||||
|
SheetCommentService sheetCommentService,
|
||||||
|
JournalCommentService journalCommentService,
|
||||||
|
AttachmentService attachmentService,
|
||||||
|
OptionService optionService,
|
||||||
|
PhotoService photoService,
|
||||||
|
ThemeSettingService themeSettingService) {
|
||||||
|
this.postService = postService;
|
||||||
|
this.sheetService = sheetService;
|
||||||
|
this.postCommentService = postCommentService;
|
||||||
|
this.sheetCommentService = sheetCommentService;
|
||||||
|
this.journalCommentService = journalCommentService;
|
||||||
|
this.attachmentService = attachmentService;
|
||||||
|
this.optionService = optionService;
|
||||||
|
this.photoService = photoService;
|
||||||
|
this.themeSettingService = themeSettingService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void replaceAllUrl(String oldUrl, String newUrl) {
|
||||||
|
postService.replaceUrl(oldUrl, newUrl);
|
||||||
|
sheetService.replaceUrl(oldUrl, newUrl);
|
||||||
|
postCommentService.replaceUrl(oldUrl, newUrl);
|
||||||
|
sheetCommentService.replaceUrl(oldUrl, newUrl);
|
||||||
|
journalCommentService.replaceUrl(oldUrl, newUrl);
|
||||||
|
attachmentService.replaceUrl(oldUrl, newUrl);
|
||||||
|
optionService.replaceUrl(oldUrl, newUrl);
|
||||||
|
photoService.replaceUrl(oldUrl, newUrl);
|
||||||
|
themeSettingService.replaceUrl(oldUrl, newUrl);
|
||||||
|
}
|
||||||
|
}
|
|
@ -466,10 +466,13 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
||||||
List<Option> options = listAll();
|
List<Option> options = listAll();
|
||||||
List<Option> replaced = new ArrayList<>();
|
List<Option> replaced = new ArrayList<>();
|
||||||
options.forEach(option -> {
|
options.forEach(option -> {
|
||||||
option.setValue(option.getValue().replaceAll(oldUrl, newUrl));
|
if (StringUtils.isNotEmpty(option.getValue())) {
|
||||||
|
option.setValue(option.getValue().replaceAll(oldUrl, newUrl));
|
||||||
|
}
|
||||||
replaced.add(option);
|
replaced.add(option);
|
||||||
});
|
});
|
||||||
List<Option> updated = updateInBatch(replaced);
|
List<Option> updated = updateInBatch(replaced);
|
||||||
|
publishOptionUpdatedEvent();
|
||||||
return updated.stream().map(this::convertToDto).collect(Collectors.toList());
|
return updated.stream().map(this::convertToDto).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -116,8 +116,12 @@ public class PhotoServiceImpl extends AbstractCrudService<Photo, Integer> implem
|
||||||
List<Photo> photos = listAll();
|
List<Photo> photos = listAll();
|
||||||
List<Photo> replaced = new ArrayList<>();
|
List<Photo> replaced = new ArrayList<>();
|
||||||
photos.forEach(photo -> {
|
photos.forEach(photo -> {
|
||||||
photo.setThumbnail(photo.getThumbnail().replace(oldUrl, newUrl));
|
if (StringUtils.isNotEmpty(photo.getThumbnail())) {
|
||||||
photo.setUrl(photo.getUrl().replaceAll(oldUrl, newUrl));
|
photo.setThumbnail(photo.getThumbnail().replace(oldUrl, newUrl));
|
||||||
|
}
|
||||||
|
if (StringUtils.isNotEmpty(photo.getUrl())) {
|
||||||
|
photo.setUrl(photo.getUrl().replaceAll(oldUrl, newUrl));
|
||||||
|
}
|
||||||
replaced.add(photo);
|
replaced.add(photo);
|
||||||
});
|
});
|
||||||
List<Photo> updated = updateInBatch(replaced);
|
List<Photo> updated = updateInBatch(replaced);
|
||||||
|
|
|
@ -6,6 +6,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import run.halo.app.exception.ServiceException;
|
import run.halo.app.exception.ServiceException;
|
||||||
|
@ -165,12 +166,20 @@ public class ThemeSettingServiceImpl extends AbstractCrudService<ThemeSetting, I
|
||||||
List<ThemeSetting> themeSettings = listAll();
|
List<ThemeSetting> themeSettings = listAll();
|
||||||
List<ThemeSetting> replaced = new ArrayList<>();
|
List<ThemeSetting> replaced = new ArrayList<>();
|
||||||
themeSettings.forEach(themeSetting -> {
|
themeSettings.forEach(themeSetting -> {
|
||||||
themeSetting.setValue(themeSetting.getValue().replaceAll(oldUrl, newUrl));
|
if (StringUtils.isNotEmpty(themeSetting.getValue())) {
|
||||||
|
themeSetting.setValue(themeSetting.getValue().replaceAll(oldUrl, newUrl));
|
||||||
|
}
|
||||||
replaced.add(themeSetting);
|
replaced.add(themeSetting);
|
||||||
});
|
});
|
||||||
return updateInBatch(replaced);
|
return updateInBatch(replaced);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional
|
||||||
|
public void deleteInactivated() {
|
||||||
|
themeSettingRepository.deleteByThemeIdIsNot(themeService.getActivatedThemeId());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets config item map. (key: item name, value: item)
|
* Gets config item map. (key: item name, value: item)
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue