mirror of https://github.com/halo-dev/halo
commit
74be79d780
|
@ -1,49 +0,0 @@
|
|||
package run.halo.app.controller.admin.api;
|
||||
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import run.halo.app.cache.lock.CacheLock;
|
||||
import run.halo.app.exception.BadRequestException;
|
||||
import run.halo.app.model.properties.PrimaryProperties;
|
||||
import run.halo.app.service.OptionService;
|
||||
import run.halo.app.service.RecoveryService;
|
||||
|
||||
/**
|
||||
* Recovery controller
|
||||
*
|
||||
* @author johnniang
|
||||
* @date 19-4-26
|
||||
*/
|
||||
@Deprecated
|
||||
@RestController
|
||||
@RequestMapping("/api/admin/recoveries")
|
||||
public class RecoveryController {
|
||||
|
||||
private final RecoveryService recoveryService;
|
||||
|
||||
private final OptionService optionService;
|
||||
|
||||
public RecoveryController(RecoveryService recoveryService,
|
||||
OptionService optionService) {
|
||||
this.recoveryService = recoveryService;
|
||||
this.optionService = optionService;
|
||||
}
|
||||
|
||||
@PostMapping("migrations/v0_4_3")
|
||||
@ApiOperation("Migrates from halo v0.4.3")
|
||||
@CacheLock
|
||||
public void migrateFromVersion_0_4_3(
|
||||
@ApiParam("This file content type should be json")
|
||||
@RequestPart("file") MultipartFile file) {
|
||||
if (optionService.getByPropertyOrDefault(PrimaryProperties.IS_INSTALLED, Boolean.class, false)) {
|
||||
throw new BadRequestException("无法在博客初始化完成之后迁移数据");
|
||||
}
|
||||
|
||||
recoveryService.migrateFromV0_4_3(file);
|
||||
}
|
||||
}
|
|
@ -124,9 +124,6 @@ public class PostModel {
|
|||
model.addAttribute("tags", tagService.convertTo(tags));
|
||||
model.addAttribute("metas", postMetaService.convertToMap(metas));
|
||||
|
||||
// TODO,Will be deprecated
|
||||
model.addAttribute("comments", Page.empty());
|
||||
|
||||
if (themeService.templateExists(
|
||||
ThemeService.CUSTOM_POST_PREFIX + post.getTemplate() + HaloConst.SUFFIX_FTL)) {
|
||||
return themeService.render(ThemeService.CUSTOM_POST_PREFIX + post.getTemplate());
|
||||
|
|
|
@ -1,21 +1,24 @@
|
|||
package run.halo.app.controller.content.model;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.ui.Model;
|
||||
import run.halo.app.cache.StringCacheStore;
|
||||
import run.halo.app.exception.ForbiddenException;
|
||||
import run.halo.app.model.entity.Sheet;
|
||||
import run.halo.app.model.entity.SheetMeta;
|
||||
import run.halo.app.model.enums.PostEditorType;
|
||||
import run.halo.app.model.enums.PostStatus;
|
||||
import run.halo.app.model.support.HaloConst;
|
||||
import run.halo.app.model.vo.SheetDetailVO;
|
||||
import run.halo.app.service.OptionService;
|
||||
import run.halo.app.service.SheetMetaService;
|
||||
import run.halo.app.service.SheetService;
|
||||
import run.halo.app.service.ThemeService;
|
||||
import run.halo.app.utils.MarkdownUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Sheet model.
|
||||
*
|
||||
|
@ -27,14 +30,21 @@ public class SheetModel {
|
|||
|
||||
private final SheetService sheetService;
|
||||
|
||||
private final SheetMetaService sheetMetaService;
|
||||
|
||||
private final StringCacheStore cacheStore;
|
||||
|
||||
private final ThemeService themeService;
|
||||
|
||||
private final OptionService optionService;
|
||||
|
||||
public SheetModel(SheetService sheetService, StringCacheStore cacheStore, ThemeService themeService, OptionService optionService) {
|
||||
public SheetModel(SheetService sheetService,
|
||||
SheetMetaService sheetMetaService,
|
||||
StringCacheStore cacheStore,
|
||||
ThemeService themeService,
|
||||
OptionService optionService) {
|
||||
this.sheetService = sheetService;
|
||||
this.sheetMetaService = sheetMetaService;
|
||||
this.cacheStore = cacheStore;
|
||||
this.themeService = themeService;
|
||||
this.optionService = optionService;
|
||||
|
@ -70,6 +80,8 @@ public class SheetModel {
|
|||
|
||||
SheetDetailVO sheetDetailVO = sheetService.convertToDetailVo(sheet);
|
||||
|
||||
List<SheetMeta> metas = sheetMetaService.listBy(sheet.getId());
|
||||
|
||||
// Generate meta keywords.
|
||||
if (StringUtils.isNotEmpty(sheet.getMetaKeywords())) {
|
||||
model.addAttribute("meta_keywords", sheet.getMetaKeywords());
|
||||
|
@ -88,9 +100,7 @@ public class SheetModel {
|
|||
model.addAttribute("sheet", sheetDetailVO);
|
||||
model.addAttribute("post", sheetDetailVO);
|
||||
model.addAttribute("is_sheet", true);
|
||||
|
||||
// TODO,Will be deprecated
|
||||
model.addAttribute("comments", Page.empty());
|
||||
model.addAttribute("metas", sheetMetaService.convertToMap(metas));
|
||||
|
||||
if (themeService.templateExists(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate() + HaloConst.SUFFIX_FTL)) {
|
||||
return themeService.render(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate());
|
||||
|
|
|
@ -47,8 +47,7 @@ public class PostTagDirective implements TemplateDirectiveModel {
|
|||
switch (method) {
|
||||
case "latest":
|
||||
int top = Integer.parseInt(params.get("top").toString());
|
||||
List<Post> posts = postService.listLatest(top);
|
||||
env.setVariable("posts", builder.build().wrap(postService.convertToListVo(posts)));
|
||||
env.setVariable("posts", builder.build().wrap(postService.convertToListVo(postService.listLatest(top))));
|
||||
break;
|
||||
case "count":
|
||||
env.setVariable("count", builder.build().wrap(postService.countByStatus(PostStatus.PUBLISHED)));
|
||||
|
@ -69,7 +68,8 @@ public class PostTagDirective implements TemplateDirectiveModel {
|
|||
break;
|
||||
case "listByCategorySlug":
|
||||
String categorySlug = params.get("categorySlug").toString();
|
||||
env.setVariable("posts", builder.build().wrap(postService.convertToListVo(postCategoryService.listPostBy(categorySlug, PostStatus.PUBLISHED))));
|
||||
List<Post> posts = postCategoryService.listPostBy(categorySlug, PostStatus.PUBLISHED);
|
||||
env.setVariable("posts", builder.build().wrap(postService.convertToListVo(posts)));
|
||||
break;
|
||||
case "listByTagId":
|
||||
Integer tagId = Integer.parseInt(params.get("tagId").toString());
|
||||
|
|
|
@ -4,6 +4,8 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import org.flywaydb.core.Flyway;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.ansi.AnsiColor;
|
||||
import org.springframework.boot.ansi.AnsiOutput;
|
||||
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
@ -64,12 +66,12 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
|
|||
private void printStartInfo() {
|
||||
String blogUrl = optionService.getBlogBaseUrl();
|
||||
|
||||
log.info("Halo started at {}", blogUrl);
|
||||
log.info("Halo admin started at {}/{}", blogUrl, haloProperties.getAdminPath());
|
||||
log.info(AnsiOutput.toString(AnsiColor.BRIGHT_BLUE, "Halo started at ", blogUrl));
|
||||
log.info(AnsiOutput.toString(AnsiColor.BRIGHT_BLUE, "Halo admin started at ", blogUrl, "/", haloProperties.getAdminPath()));
|
||||
if (!haloProperties.isDocDisabled()) {
|
||||
log.debug("Halo api doc was enabled at {}/swagger-ui.html", blogUrl);
|
||||
log.debug(AnsiOutput.toString(AnsiColor.BRIGHT_BLUE, "Halo api doc was enabled at ", blogUrl, "/swagger-ui.html"));
|
||||
}
|
||||
log.info("Halo has started successfully!");
|
||||
log.info(AnsiOutput.toString(AnsiColor.BRIGHT_YELLOW, "Halo has started successfully!"));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -67,7 +67,7 @@ public class PostParam implements InputConverter<Post> {
|
|||
|
||||
private Set<Integer> categoryIds;
|
||||
|
||||
private Set<PostMetaParam> postMetas;
|
||||
private Set<PostMetaParam> metas;
|
||||
|
||||
@Override
|
||||
public Post convertTo() {
|
||||
|
@ -101,11 +101,11 @@ public class PostParam implements InputConverter<Post> {
|
|||
|
||||
public Set<PostMeta> getPostMetas() {
|
||||
Set<PostMeta> postMetaSet = new HashSet<>();
|
||||
if (CollectionUtils.isEmpty(postMetas)) {
|
||||
if (CollectionUtils.isEmpty(metas)) {
|
||||
return postMetaSet;
|
||||
}
|
||||
|
||||
for (PostMetaParam postMetaParam : postMetas) {
|
||||
for (PostMetaParam postMetaParam : metas) {
|
||||
PostMeta postMeta = postMetaParam.convertTo();
|
||||
postMetaSet.add(postMeta);
|
||||
}
|
||||
|
|
|
@ -62,7 +62,7 @@ public class SheetParam implements InputConverter<Sheet> {
|
|||
|
||||
private String metaDescription;
|
||||
|
||||
private Set<SheetMetaParam> sheetMetas;
|
||||
private Set<SheetMetaParam> metas;
|
||||
|
||||
@Override
|
||||
public Sheet convertTo() {
|
||||
|
@ -96,11 +96,11 @@ public class SheetParam implements InputConverter<Sheet> {
|
|||
|
||||
public Set<SheetMeta> getSheetMetas() {
|
||||
Set<SheetMeta> sheetMetasSet = new HashSet<>();
|
||||
if (CollectionUtils.isEmpty(sheetMetas)) {
|
||||
if (CollectionUtils.isEmpty(metas)) {
|
||||
return sheetMetasSet;
|
||||
}
|
||||
|
||||
for (SheetMetaParam sheetMetaParam : sheetMetas) {
|
||||
for (SheetMetaParam sheetMetaParam : metas) {
|
||||
SheetMeta sheetMeta = sheetMetaParam.convertTo();
|
||||
sheetMetasSet.add(sheetMeta);
|
||||
}
|
||||
|
|
|
@ -31,8 +31,8 @@ public class PostDetailVO extends BasePostDetailDTO {
|
|||
|
||||
private List<CategoryDTO> categories;
|
||||
|
||||
private Set<Long> postMetaIds;
|
||||
private Set<Long> metaIds;
|
||||
|
||||
private List<BaseMetaDTO> postMetas;
|
||||
private List<BaseMetaDTO> metas;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,12 +2,12 @@ package run.halo.app.model.vo;
|
|||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import run.halo.app.model.dto.BaseMetaDTO;
|
||||
import run.halo.app.model.dto.CategoryDTO;
|
||||
import run.halo.app.model.dto.TagDTO;
|
||||
import run.halo.app.model.dto.post.BasePostSimpleDTO;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Post list vo.
|
||||
|
@ -27,5 +27,5 @@ public class PostListVO extends BasePostSimpleDTO {
|
|||
|
||||
private List<CategoryDTO> categories;
|
||||
|
||||
private List<BaseMetaDTO> postMetas;
|
||||
private Map<String, Object> metas;
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ import java.util.Set;
|
|||
@EqualsAndHashCode(callSuper = true)
|
||||
public class SheetDetailVO extends BasePostDetailDTO {
|
||||
|
||||
private Set<Long> sheetMetaIds;
|
||||
private Set<Long> metaIds;
|
||||
|
||||
private List<BaseMetaDTO> sheetMetas;
|
||||
private List<BaseMetaDTO> metas;
|
||||
}
|
||||
|
|
|
@ -42,15 +42,10 @@ import static run.halo.app.model.support.HaloConst.ONE_TIME_TOKEN_QUERY_NAME;
|
|||
public abstract class AbstractAuthenticationFilter extends OncePerRequestFilter {
|
||||
|
||||
protected final AntPathMatcher antPathMatcher;
|
||||
|
||||
private final UrlPathHelper urlPathHelper = new UrlPathHelper();
|
||||
|
||||
protected final HaloProperties haloProperties;
|
||||
|
||||
protected final OptionService optionService;
|
||||
|
||||
protected final StringCacheStore cacheStore;
|
||||
|
||||
private final UrlPathHelper urlPathHelper = new UrlPathHelper();
|
||||
private OneTimeTokenService oneTimeTokenService;
|
||||
|
||||
private volatile AuthenticationFailureHandler failureHandler;
|
||||
|
@ -128,15 +123,15 @@ public abstract class AbstractAuthenticationFilter extends OncePerRequestFilter
|
|||
this.excludeUrlPatterns = new HashSet<>(excludeUrlPatterns);
|
||||
}
|
||||
|
||||
public Collection<String> getUrlPatterns() {
|
||||
return this.urlPatterns;
|
||||
}
|
||||
|
||||
public void setUrlPatterns(Collection<String> urlPatterns) {
|
||||
Assert.notNull(urlPatterns, "UrlPatterns must not be null");
|
||||
this.urlPatterns = new LinkedHashSet<>(urlPatterns);
|
||||
}
|
||||
|
||||
public Collection<String> getUrlPatterns() {
|
||||
return this.urlPatterns;
|
||||
}
|
||||
|
||||
public void addUrlPatterns(String... urlPatterns) {
|
||||
Assert.notNull(urlPatterns, "UrlPatterns must not be null");
|
||||
Collections.addAll(this.urlPatterns, urlPatterns);
|
||||
|
|
|
@ -60,7 +60,6 @@ public class AdminAuthenticationFilter extends AbstractAuthenticationFilter {
|
|||
"/api/admin/login",
|
||||
"/api/admin/refresh/*",
|
||||
"/api/admin/installations",
|
||||
"/api/admin/recoveries/migrations/**",
|
||||
"/api/admin/migrations/**",
|
||||
"/api/admin/is_installed",
|
||||
"/api/admin/password/code",
|
||||
|
|
|
@ -52,12 +52,12 @@ public interface PostService extends BasePostService<Post> {
|
|||
* @param post post must not be null
|
||||
* @param tagIds tag id set
|
||||
* @param categoryIds category id set
|
||||
* @param postMetas post metas
|
||||
* @param metas metas
|
||||
* @param autoSave autoSave
|
||||
* @return post created
|
||||
*/
|
||||
@NonNull
|
||||
PostDetailVO createBy(@NonNull Post post, Set<Integer> tagIds, Set<Integer> categoryIds, Set<PostMeta> postMetas, boolean autoSave);
|
||||
PostDetailVO createBy(@NonNull Post post, Set<Integer> tagIds, Set<Integer> categoryIds, Set<PostMeta> metas, boolean autoSave);
|
||||
|
||||
/**
|
||||
* Creates post by post param.
|
||||
|
@ -77,11 +77,12 @@ public interface PostService extends BasePostService<Post> {
|
|||
* @param postToUpdate post to update must not be null
|
||||
* @param tagIds tag id set
|
||||
* @param categoryIds category id set
|
||||
* @param metas metas
|
||||
* @param autoSave autoSave
|
||||
* @return updated post
|
||||
*/
|
||||
@NonNull
|
||||
PostDetailVO updateBy(@NonNull Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds, Set<PostMeta> postMetas, boolean autoSave);
|
||||
PostDetailVO updateBy(@NonNull Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds, Set<PostMeta> metas, boolean autoSave);
|
||||
|
||||
/**
|
||||
* Gets post by post status and slug.
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
package run.halo.app.service;
|
||||
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
* Recovery service interface.
|
||||
*
|
||||
* @author johnniang
|
||||
* @date 2019-04-26
|
||||
*/
|
||||
@Deprecated
|
||||
public interface RecoveryService {
|
||||
|
||||
/**
|
||||
* Migrates from halo version 0.4.3.
|
||||
*
|
||||
* @param file multipart file must not be null
|
||||
*/
|
||||
void migrateFromV0_4_3(@NonNull MultipartFile file);
|
||||
}
|
|
@ -35,12 +35,12 @@ public interface SheetService extends BasePostService<Sheet> {
|
|||
/**
|
||||
* Creates a sheet.
|
||||
*
|
||||
* @param sheet sheet must not be null
|
||||
* @param sheetMetas sheet metas
|
||||
* @param autoSave autoSave
|
||||
* @param sheet sheet must not be null
|
||||
* @param metas sheet metas
|
||||
* @param autoSave autoSave
|
||||
* @return created sheet
|
||||
*/
|
||||
Sheet createBy(@NonNull Sheet sheet, Set<SheetMeta> sheetMetas, boolean autoSave);
|
||||
Sheet createBy(@NonNull Sheet sheet, Set<SheetMeta> metas, boolean autoSave);
|
||||
|
||||
/**
|
||||
* Updates a sheet.
|
||||
|
@ -55,12 +55,12 @@ public interface SheetService extends BasePostService<Sheet> {
|
|||
/**
|
||||
* Updates a sheet.
|
||||
*
|
||||
* @param sheet sheet must not be null
|
||||
* @param sheetMetas sheet metas
|
||||
* @param autoSave autoSave
|
||||
* @param sheet sheet must not be null
|
||||
* @param metas sheet metas
|
||||
* @param autoSave autoSave
|
||||
* @return updated sheet
|
||||
*/
|
||||
Sheet updateBy(@NonNull Sheet sheet, Set<SheetMeta> sheetMetas, boolean autoSave);
|
||||
Sheet updateBy(@NonNull Sheet sheet, Set<SheetMeta> metas, boolean autoSave);
|
||||
|
||||
/**
|
||||
* Gets by url
|
||||
|
|
|
@ -21,11 +21,11 @@ public interface BaseMetaService<META extends BaseMeta> extends CrudService<META
|
|||
/**
|
||||
* Creates by post metas and post id.
|
||||
*
|
||||
* @param postId post id must not be null
|
||||
* @param postMetas post metas must not be null
|
||||
* @param postId post id must not be null
|
||||
* @param metas metas must not be null
|
||||
* @return a list of post meta
|
||||
*/
|
||||
List<META> createOrUpdateByPostId(@NonNull Integer postId, Set<META> postMetas);
|
||||
List<META> createOrUpdateByPostId(@NonNull Integer postId, Set<META> metas);
|
||||
|
||||
/**
|
||||
* Remove post metas by post id.
|
||||
|
|
|
@ -36,24 +36,24 @@ public abstract class BaseMetaServiceImpl<META extends BaseMeta> extends Abstrac
|
|||
|
||||
@Override
|
||||
@Transactional
|
||||
public List<META> createOrUpdateByPostId(Integer postId, Set<META> postMetas) {
|
||||
public List<META> createOrUpdateByPostId(Integer postId, Set<META> metas) {
|
||||
Assert.notNull(postId, "Post id must not be null");
|
||||
|
||||
// firstly remove post metas by post id
|
||||
removeByPostId(postId);
|
||||
|
||||
if (CollectionUtils.isEmpty(postMetas)) {
|
||||
if (CollectionUtils.isEmpty(metas)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
// Save post metas
|
||||
postMetas.forEach(postMeta -> {
|
||||
metas.forEach(postMeta -> {
|
||||
if (StringUtils.isNotEmpty(postMeta.getValue()) && StringUtils.isNotEmpty(postMeta.getKey())) {
|
||||
postMeta.setPostId(postId);
|
||||
baseMetaRepository.save(postMeta);
|
||||
}
|
||||
});
|
||||
return new ArrayList<>(postMetas);
|
||||
return new ArrayList<>(metas);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -69,18 +69,18 @@ public abstract class BaseMetaServiceImpl<META extends BaseMeta> extends Abstrac
|
|||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
// Find all post metas
|
||||
List<META> postMetas = baseMetaRepository.findAllByPostIdIn(postIds);
|
||||
// Find all metas
|
||||
List<META> metas = baseMetaRepository.findAllByPostIdIn(postIds);
|
||||
|
||||
// Convert to post meta map
|
||||
Map<Long, META> postMetaMap = ServiceUtils.convertToMap(postMetas, META::getId);
|
||||
// Convert to meta map
|
||||
Map<Long, META> postMetaMap = ServiceUtils.convertToMap(metas, META::getId);
|
||||
|
||||
// Create category list map
|
||||
Map<Integer, List<META>> postMetaListMap = new HashMap<>();
|
||||
|
||||
// Foreach and collect
|
||||
postMetas.forEach(postMeta -> postMetaListMap.computeIfAbsent(postMeta.getPostId(), postId -> new LinkedList<>())
|
||||
.add(postMetaMap.get(postMeta.getId())));
|
||||
metas.forEach(meta -> postMetaListMap.computeIfAbsent(meta.getPostId(), postId -> new LinkedList<>())
|
||||
.add(postMetaMap.get(meta.getId())));
|
||||
|
||||
return postMetaListMap;
|
||||
}
|
||||
|
|
|
@ -20,7 +20,6 @@ import org.springframework.util.CollectionUtils;
|
|||
import run.halo.app.event.logger.LogEvent;
|
||||
import run.halo.app.event.post.PostVisitEvent;
|
||||
import run.halo.app.exception.NotFoundException;
|
||||
import run.halo.app.model.dto.BaseMetaDTO;
|
||||
import run.halo.app.model.dto.post.BasePostMinimalDTO;
|
||||
import run.halo.app.model.dto.post.BasePostSimpleDTO;
|
||||
import run.halo.app.model.entity.*;
|
||||
|
@ -126,8 +125,8 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
@Override
|
||||
@Transactional
|
||||
public PostDetailVO createBy(Post postToCreate, Set<Integer> tagIds, Set<Integer> categoryIds,
|
||||
Set<PostMeta> postMetas, boolean autoSave) {
|
||||
PostDetailVO createdPost = createOrUpdate(postToCreate, tagIds, categoryIds, postMetas);
|
||||
Set<PostMeta> metas, boolean autoSave) {
|
||||
PostDetailVO createdPost = createOrUpdate(postToCreate, tagIds, categoryIds, metas);
|
||||
if (!autoSave) {
|
||||
// Log the creation
|
||||
LogEvent logEvent = new LogEvent(this, createdPost.getId().toString(),
|
||||
|
@ -153,10 +152,10 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
@Override
|
||||
@Transactional
|
||||
public PostDetailVO updateBy(Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds,
|
||||
Set<PostMeta> postMetas, boolean autoSave) {
|
||||
Set<PostMeta> metas, boolean autoSave) {
|
||||
// Set edit time
|
||||
postToUpdate.setEditTime(DateUtils.now());
|
||||
PostDetailVO updatedPost = createOrUpdate(postToUpdate, tagIds, categoryIds, postMetas);
|
||||
PostDetailVO updatedPost = createOrUpdate(postToUpdate, tagIds, categoryIds, metas);
|
||||
if (!autoSave) {
|
||||
// Log the creation
|
||||
LogEvent logEvent = new LogEvent(this, updatedPost.getId().toString(),
|
||||
|
@ -437,11 +436,11 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
}
|
||||
}
|
||||
|
||||
List<PostMeta> postMetas = postMetaService.listBy(post.getId());
|
||||
List<PostMeta> metas = postMetaService.listBy(post.getId());
|
||||
|
||||
if (postMetas.size() > 0) {
|
||||
content.append("postMetas:").append("\n");
|
||||
for (PostMeta postMeta : postMetas) {
|
||||
if (metas.size() > 0) {
|
||||
content.append("metas:").append("\n");
|
||||
for (PostMeta postMeta : metas) {
|
||||
content.append(" - ").append(postMeta.getKey()).append(" : ")
|
||||
.append(postMeta.getValue()).append("\n");
|
||||
}
|
||||
|
@ -458,10 +457,10 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
List<Tag> tags = postTagService.listTagsBy(post.getId());
|
||||
// List categories
|
||||
List<Category> categories = postCategoryService.listCategoriesBy(post.getId());
|
||||
// List postMetas
|
||||
List<PostMeta> postMetas = postMetaService.listBy(post.getId());
|
||||
// List metas
|
||||
List<PostMeta> metas = postMetaService.listBy(post.getId());
|
||||
// Convert to detail vo
|
||||
return convertTo(post, tags, categories, postMetas);
|
||||
return convertTo(post, tags, categories, metas);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -480,9 +479,9 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
|
||||
log.debug("Removed post categories: [{}]", postCategories);
|
||||
|
||||
// Remove post metas
|
||||
List<PostMeta> postMetas = postMetaService.removeByPostId(postId);
|
||||
log.debug("Removed post metas: [{}]", postMetas);
|
||||
// Remove metas
|
||||
List<PostMeta> metas = postMetaService.removeByPostId(postId);
|
||||
log.debug("Removed post metas: [{}]", metas);
|
||||
|
||||
// Remove post comments
|
||||
List<PostComment> postComments = postCommentService.removeByPostId(postId);
|
||||
|
@ -544,12 +543,9 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
.collect(Collectors.toList()));
|
||||
|
||||
// Set post metas
|
||||
postListVO.setPostMetas(Optional.ofNullable(postMetaListMap.get(post.getId()))
|
||||
.orElseGet(LinkedList::new)
|
||||
.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.map(postMeta -> (BaseMetaDTO) new BaseMetaDTO().convertFrom(postMeta))
|
||||
.collect(Collectors.toList()));
|
||||
List<PostMeta> metas = Optional.ofNullable(postMetaListMap.get(post.getId()))
|
||||
.orElseGet(LinkedList::new);
|
||||
postListVO.setMetas(postMetaService.convertToMap(metas));
|
||||
|
||||
// Set comment count
|
||||
postListVO.setCommentCount(commentCountMap.getOrDefault(post.getId(), 0L));
|
||||
|
@ -605,12 +601,9 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
.collect(Collectors.toList()));
|
||||
|
||||
// Set post metas
|
||||
postListVO.setPostMetas(Optional.ofNullable(postMetaListMap.get(post.getId()))
|
||||
.orElseGet(LinkedList::new)
|
||||
.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.map(postMeta -> (BaseMetaDTO) new BaseMetaDTO().convertFrom(postMeta))
|
||||
.collect(Collectors.toList()));
|
||||
List<PostMeta> metas = Optional.ofNullable(postMetaListMap.get(post.getId()))
|
||||
.orElseGet(LinkedList::new);
|
||||
postListVO.setMetas(postMetaService.convertToMap(metas));
|
||||
|
||||
// Set comment count
|
||||
postListVO.setCommentCount(commentCountMap.getOrDefault(post.getId(), 0L));
|
||||
|
@ -670,7 +663,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
* @param post post must not be null
|
||||
* @param tags tags
|
||||
* @param categories categories
|
||||
* @param postMetaList postMetas
|
||||
* @param postMetaList postMetaList
|
||||
* @return post detail vo
|
||||
*/
|
||||
@NonNull
|
||||
|
@ -688,7 +681,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
// Extract ids
|
||||
Set<Integer> tagIds = ServiceUtils.fetchProperty(tags, Tag::getId);
|
||||
Set<Integer> categoryIds = ServiceUtils.fetchProperty(categories, Category::getId);
|
||||
Set<Long> postMetaIds = ServiceUtils.fetchProperty(postMetaList, PostMeta::getId);
|
||||
Set<Long> metaIds = ServiceUtils.fetchProperty(postMetaList, PostMeta::getId);
|
||||
|
||||
// Get post tag ids
|
||||
postDetailVO.setTagIds(tagIds);
|
||||
|
@ -699,8 +692,8 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
postDetailVO.setCategories(categoryService.convertTo(categories));
|
||||
|
||||
// Get post meta ids
|
||||
postDetailVO.setPostMetaIds(postMetaIds);
|
||||
postDetailVO.setPostMetas(postMetaService.convertTo(postMetaList));
|
||||
postDetailVO.setMetaIds(metaIds);
|
||||
postDetailVO.setMetas(postMetaService.convertTo(postMetaList));
|
||||
|
||||
postDetailVO.setCommentCount(postCommentService.countByPostId(post.getId()));
|
||||
|
||||
|
@ -755,7 +748,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
}
|
||||
|
||||
private PostDetailVO createOrUpdate(@NonNull Post post, Set<Integer> tagIds,
|
||||
Set<Integer> categoryIds, Set<PostMeta> postMetas) {
|
||||
Set<Integer> categoryIds, Set<PostMeta> metas) {
|
||||
Assert.notNull(post, "Post param must not be null");
|
||||
|
||||
// Create or update post
|
||||
|
@ -786,8 +779,8 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
|
||||
// Create post meta data
|
||||
List<PostMeta> postMetaList = postMetaService
|
||||
.createOrUpdateByPostId(post.getId(), postMetas);
|
||||
log.debug("Created post postMetas: [{}]", postMetaList);
|
||||
.createOrUpdateByPostId(post.getId(), metas);
|
||||
log.debug("Created post metas: [{}]", postMetaList);
|
||||
|
||||
// Convert to post detail vo
|
||||
return convertTo(post, tags, categories, postMetaList);
|
||||
|
|
|
@ -1,698 +0,0 @@
|
|||
package run.halo.app.service.impl;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import run.halo.app.exception.ServiceException;
|
||||
import run.halo.app.model.entity.*;
|
||||
import run.halo.app.model.enums.AttachmentType;
|
||||
import run.halo.app.model.enums.CommentStatus;
|
||||
import run.halo.app.model.enums.PostStatus;
|
||||
import run.halo.app.repository.PostCommentRepository;
|
||||
import run.halo.app.repository.SheetCommentRepository;
|
||||
import run.halo.app.service.*;
|
||||
import run.halo.app.utils.BeanUtils;
|
||||
import run.halo.app.utils.JsonUtils;
|
||||
import run.halo.app.utils.ServiceUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Recovery service implementation.
|
||||
*
|
||||
* @author johnniang
|
||||
* @author ryanwang
|
||||
* @date 2019-04-26
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@SuppressWarnings("unchecked")
|
||||
public class RecoveryServiceImpl implements RecoveryService {
|
||||
|
||||
private final AttachmentService attachmentService;
|
||||
|
||||
private final PostService postService;
|
||||
|
||||
private final LinkService linkService;
|
||||
|
||||
private final MenuService menuService;
|
||||
|
||||
private final CategoryService categoryService;
|
||||
|
||||
private final TagService tagService;
|
||||
|
||||
private final PostCommentService postCommentService;
|
||||
|
||||
private final PostCommentRepository postCommentRepository;
|
||||
|
||||
private final SheetCommentService sheetCommentService;
|
||||
|
||||
private final SheetCommentRepository sheetCommentRepository;
|
||||
|
||||
private final SheetService sheetService;
|
||||
|
||||
private final PhotoService photoService;
|
||||
|
||||
private final PostCategoryService postCategoryService;
|
||||
|
||||
private final PostTagService postTagService;
|
||||
|
||||
public RecoveryServiceImpl(AttachmentService attachmentService,
|
||||
PostService postService,
|
||||
LinkService linkService,
|
||||
MenuService menuService,
|
||||
CategoryService categoryService,
|
||||
TagService tagService,
|
||||
PostCommentService postCommentService,
|
||||
PostCommentRepository postCommentRepository,
|
||||
SheetCommentService sheetCommentService,
|
||||
SheetCommentRepository sheetCommentRepository,
|
||||
SheetService sheetService,
|
||||
PhotoService photoService,
|
||||
PostCategoryService postCategoryService,
|
||||
PostTagService postTagService) {
|
||||
this.attachmentService = attachmentService;
|
||||
this.postService = postService;
|
||||
this.linkService = linkService;
|
||||
this.menuService = menuService;
|
||||
this.categoryService = categoryService;
|
||||
this.tagService = tagService;
|
||||
this.postCommentService = postCommentService;
|
||||
this.postCommentRepository = postCommentRepository;
|
||||
this.sheetCommentService = sheetCommentService;
|
||||
this.sheetCommentRepository = sheetCommentRepository;
|
||||
this.sheetService = sheetService;
|
||||
this.photoService = photoService;
|
||||
this.postCategoryService = postCategoryService;
|
||||
this.postTagService = postTagService;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Async
|
||||
public void migrateFromV0_4_3(MultipartFile file) {
|
||||
// Get migration content
|
||||
try {
|
||||
String migrationContent = FileCopyUtils.copyToString(new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8));
|
||||
|
||||
Object migrationObject = JsonUtils.jsonToObject(migrationContent, Object.class);
|
||||
|
||||
if (migrationObject instanceof Map) {
|
||||
Map<String, Object> migrationMap = (Map<String, Object>) migrationObject;
|
||||
|
||||
// Handle attachments
|
||||
List<Attachment> attachments = handleAttachments(migrationMap.get("attachments"));
|
||||
|
||||
log.debug("Migrated attachments: [{}]", attachments);
|
||||
|
||||
// Handle links
|
||||
List<Link> links = handleLinks(migrationMap.get("links"));
|
||||
|
||||
log.debug("Migrated links: [{}]", links);
|
||||
|
||||
// Handle galleries
|
||||
List<Photo> photos = handleGalleries(migrationMap.get("galleries"));
|
||||
|
||||
log.debug("Migrated photos: [{}]", photos);
|
||||
|
||||
// Handle menus
|
||||
List<Menu> menus = handleMenus(migrationMap.get("menus"));
|
||||
|
||||
log.debug("Migrated menus: [{}]", menus);
|
||||
|
||||
// Handle posts
|
||||
List<BasePost> posts = handleBasePosts(migrationMap.get("posts"));
|
||||
|
||||
log.debug("Migrated posts: [{}]", posts);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new ServiceException("备份文件 " + file.getOriginalFilename() + " 读取失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private List<BasePost> handleBasePosts(@Nullable Object postsObject) {
|
||||
if (!(postsObject instanceof List)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<Object> postObjectList = (List<Object>) postsObject;
|
||||
|
||||
List<BasePost> result = new LinkedList<>();
|
||||
|
||||
postObjectList.forEach(postObject -> {
|
||||
if (!(postObject instanceof Map)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, Object> postMap = (Map<String, Object>) postObject;
|
||||
|
||||
BasePost post = new BasePost();
|
||||
post.setTitle(postMap.getOrDefault("postTitle", "").toString());
|
||||
post.setSlug(postMap.getOrDefault("postUrl", "").toString());
|
||||
post.setOriginalContent(postMap.getOrDefault("postContentMd", "").toString());
|
||||
post.setFormatContent(postMap.getOrDefault("postContent", "").toString());
|
||||
post.setSummary(postMap.getOrDefault("postSummary", "").toString());
|
||||
post.setThumbnail(postMap.getOrDefault("postThumbnail", "").toString());
|
||||
post.setVisits(getLongOrDefault(postMap.getOrDefault("postViews", "").toString(), 0L));
|
||||
post.setDisallowComment(false);
|
||||
post.setTemplate(postMap.getOrDefault("customTpl", "").toString());
|
||||
|
||||
// Set disallow comment
|
||||
Integer allowComment = getIntegerOrDefault(postMap.getOrDefault("allowComment", "1").toString(), 1);
|
||||
if (allowComment != 1) {
|
||||
post.setDisallowComment(true);
|
||||
}
|
||||
|
||||
// Set create time
|
||||
Long createTime = getLongOrDefault(postMap.getOrDefault("postDate", "").toString(), 0L);
|
||||
if (createTime != 0L) {
|
||||
post.setCreateTime(new Date(createTime));
|
||||
}
|
||||
|
||||
// Set update time
|
||||
Long updateTime = getLongOrDefault(postMap.getOrDefault("postUpdate", "").toString(), 0L);
|
||||
if (updateTime != 0L) {
|
||||
post.setUpdateTime(new Date(updateTime));
|
||||
}
|
||||
|
||||
// Set status (default draft)
|
||||
Integer postStatus = getIntegerOrDefault(postMap.getOrDefault("postStatus", "").toString(), 1);
|
||||
if (postStatus == 0) {
|
||||
post.setStatus(PostStatus.PUBLISHED);
|
||||
} else if (postStatus == 1) {
|
||||
post.setStatus(PostStatus.DRAFT);
|
||||
} else {
|
||||
post.setStatus(PostStatus.RECYCLE);
|
||||
}
|
||||
|
||||
String postType = postMap.getOrDefault("postType", "post").toString();
|
||||
|
||||
try {
|
||||
if ("post".equalsIgnoreCase(postType)) {
|
||||
// Handle post
|
||||
result.add(handlePost(post, postMap));
|
||||
} else {
|
||||
// Handle page
|
||||
result.add(handleSheet(post, postMap));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to migrate a post or sheet", e);
|
||||
// Ignore this exception
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Post handlePost(@NonNull BasePost basePost, @NonNull Map<String, Object> postMap) {
|
||||
Post post = BeanUtils.transformFrom(basePost, Post.class);
|
||||
|
||||
// Create it
|
||||
Post createdPost = postService.createOrUpdateBy(post);
|
||||
|
||||
Object commentsObject = postMap.get("comments");
|
||||
Object categoriesObject = postMap.get("categories");
|
||||
Object tagsObject = postMap.get("tags");
|
||||
// Handle comments
|
||||
List<BaseComment> baseComments = handleComment(commentsObject, createdPost.getId());
|
||||
|
||||
// Handle categories
|
||||
List<Category> categories = handleCategories(categoriesObject, createdPost.getId());
|
||||
|
||||
log.debug("Migrated categories of post [{}]: [{}]", categories, createdPost.getId());
|
||||
|
||||
// Handle tags
|
||||
List<Tag> tags = handleTags(tagsObject, createdPost.getId());
|
||||
|
||||
log.debug("Migrated tags of post [{}]: [{}]", tags, createdPost.getId());
|
||||
|
||||
List<PostComment> postComments = baseComments.stream()
|
||||
.map(baseComment -> BeanUtils.transformFrom(baseComment, PostComment.class))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
try {
|
||||
// Build virtual comment
|
||||
PostComment virtualPostComment = new PostComment();
|
||||
virtualPostComment.setId(0L);
|
||||
// Create comments
|
||||
createPostCommentRecursively(virtualPostComment, postComments);
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to create post comments for post with id " + createdPost.getId(), e);
|
||||
// Ignore this exception
|
||||
}
|
||||
|
||||
return createdPost;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Sheet handleSheet(@NonNull BasePost basePost, @NonNull Map<String, Object> postMap) {
|
||||
Sheet sheet = BeanUtils.transformFrom(basePost, Sheet.class);
|
||||
|
||||
// Create it
|
||||
Sheet createdSheet = sheetService.createOrUpdateBy(sheet);
|
||||
|
||||
Object commentsObject = postMap.get("comments");
|
||||
// Handle comments
|
||||
List<BaseComment> baseComments = handleComment(commentsObject, createdSheet.getId());
|
||||
|
||||
List<SheetComment> sheetComments = baseComments.stream()
|
||||
.map(baseComment -> BeanUtils.transformFrom(baseComment, SheetComment.class))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// Create comments
|
||||
try {
|
||||
// Build virtual comment
|
||||
SheetComment virtualSheetComment = new SheetComment();
|
||||
virtualSheetComment.setId(0L);
|
||||
// Create comments
|
||||
createSheetCommentRecursively(virtualSheetComment, sheetComments);
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to create sheet comments for sheet with id " + createdSheet.getId(), e);
|
||||
// Ignore this exception
|
||||
}
|
||||
|
||||
return createdSheet;
|
||||
}
|
||||
|
||||
|
||||
private void createPostCommentRecursively(@NonNull final PostComment parentComment, List<PostComment> postComments) {
|
||||
Long oldParentId = parentComment.getId();
|
||||
|
||||
// Create parent
|
||||
if (!ServiceUtils.isEmptyId(parentComment.getId())) {
|
||||
PostComment createdComment = postCommentRepository.save(parentComment);
|
||||
log.debug("Created post comment: [{}]", createdComment);
|
||||
parentComment.setId(createdComment.getId());
|
||||
}
|
||||
|
||||
if (CollectionUtils.isEmpty(postComments)) {
|
||||
return;
|
||||
}
|
||||
// Get all children
|
||||
List<PostComment> children = postComments.stream()
|
||||
.filter(postComment -> Objects.equals(oldParentId, postComment.getParentId()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
|
||||
// Set parent id again
|
||||
children.forEach(postComment -> postComment.setParentId(parentComment.getId()));
|
||||
|
||||
// Remove children
|
||||
postComments.removeAll(children);
|
||||
|
||||
// Create children recursively
|
||||
children.forEach(childComment -> createPostCommentRecursively(childComment, postComments));
|
||||
}
|
||||
|
||||
private void createSheetCommentRecursively(@NonNull final SheetComment parentComment, List<SheetComment> sheetComments) {
|
||||
Long oldParentId = parentComment.getId();
|
||||
// Create parent
|
||||
if (!ServiceUtils.isEmptyId(parentComment.getId())) {
|
||||
SheetComment createComment = sheetCommentRepository.save(parentComment);
|
||||
parentComment.setId(createComment.getId());
|
||||
}
|
||||
|
||||
if (CollectionUtils.isEmpty(sheetComments)) {
|
||||
return;
|
||||
}
|
||||
// Get all children
|
||||
List<SheetComment> children = sheetComments.stream()
|
||||
.filter(sheetComment -> Objects.equals(oldParentId, sheetComment.getParentId()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// Set parent id again
|
||||
children.forEach(postComment -> postComment.setParentId(parentComment.getId()));
|
||||
|
||||
// Remove children
|
||||
sheetComments.removeAll(children);
|
||||
|
||||
// Create children recursively
|
||||
children.forEach(childComment -> createSheetCommentRecursively(childComment, sheetComments));
|
||||
}
|
||||
|
||||
private List<BaseComment> handleComment(@Nullable Object commentsObject, @NonNull Integer postId) {
|
||||
Assert.notNull(postId, "Post id must not be null");
|
||||
|
||||
if (!(commentsObject instanceof List)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<Object> commentObjectList = (List<Object>) commentsObject;
|
||||
|
||||
List<BaseComment> result = new LinkedList<>();
|
||||
|
||||
commentObjectList.forEach(commentObject -> {
|
||||
if (!(commentObject instanceof Map)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, Object> commentMap = (Map<String, Object>) commentObject;
|
||||
|
||||
BaseComment baseComment = new BaseComment();
|
||||
baseComment.setId(getLongOrDefault(commentMap.getOrDefault("commentId", "").toString(), null));
|
||||
baseComment.setAuthor(commentMap.getOrDefault("commentAuthor", "").toString());
|
||||
baseComment.setEmail(commentMap.getOrDefault("commentAuthorEmail", "").toString());
|
||||
baseComment.setIpAddress(commentMap.getOrDefault("commentAuthorIp", "").toString());
|
||||
baseComment.setAuthorUrl(commentMap.getOrDefault("commentAuthorUrl", "").toString());
|
||||
baseComment.setGravatarMd5(commentMap.getOrDefault("commentAuthorAvatarMd5", "").toString());
|
||||
baseComment.setContent(commentMap.getOrDefault("commentContent", "").toString());
|
||||
baseComment.setUserAgent(commentMap.getOrDefault("commentAgent", "").toString());
|
||||
baseComment.setIsAdmin(getBooleanOrDefault(commentMap.getOrDefault("isAdmin", "").toString(), false));
|
||||
baseComment.setPostId(postId);
|
||||
baseComment.setParentId(getLongOrDefault(commentMap.getOrDefault("commentParent", "").toString(), 0L));
|
||||
|
||||
// Set create date
|
||||
Long createTimestamp = getLongOrDefault(commentMap.getOrDefault("createDate", "").toString(), System.currentTimeMillis());
|
||||
baseComment.setCreateTime(new Date(createTimestamp));
|
||||
|
||||
Integer commentStatus = getIntegerOrDefault(commentMap.getOrDefault("commentStatus", "").toString(), 1);
|
||||
if (commentStatus == 0) {
|
||||
baseComment.setStatus(CommentStatus.PUBLISHED);
|
||||
} else if (commentStatus == 1) {
|
||||
baseComment.setStatus(CommentStatus.AUDITING);
|
||||
} else {
|
||||
baseComment.setStatus(CommentStatus.RECYCLE);
|
||||
}
|
||||
|
||||
result.add(baseComment);
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private List<Category> handleCategories(@Nullable Object categoriesObject, @NonNull Integer postId) {
|
||||
Assert.notNull(postId, "Post id must not be null");
|
||||
|
||||
if (!(categoriesObject instanceof List)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<Object> categoryObjectList = (List<Object>) categoriesObject;
|
||||
|
||||
List<Category> result = new LinkedList<>();
|
||||
|
||||
categoryObjectList.forEach(categoryObject -> {
|
||||
if (!(categoryObject instanceof Map)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, Object> categoryMap = (Map<String, Object>) categoryObject;
|
||||
|
||||
String slug = categoryMap.getOrDefault("cateUrl", "").toString();
|
||||
|
||||
Category category = categoryService.getBySlug(slug);
|
||||
|
||||
if (null == category) {
|
||||
category = new Category();
|
||||
category.setName(categoryMap.getOrDefault("cateName", "").toString());
|
||||
category.setSlug(slug);
|
||||
category.setDescription(categoryMap.getOrDefault("cateDesc", "").toString());
|
||||
category = categoryService.create(category);
|
||||
}
|
||||
|
||||
PostCategory postCategory = new PostCategory();
|
||||
postCategory.setCategoryId(category.getId());
|
||||
postCategory.setPostId(postId);
|
||||
postCategoryService.create(postCategory);
|
||||
|
||||
try {
|
||||
result.add(category);
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to migrate a category", e);
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private List<Tag> handleTags(@Nullable Object tagsObject, @NonNull Integer postId) {
|
||||
Assert.notNull(postId, "Post id must not be null");
|
||||
|
||||
if (!(tagsObject instanceof List)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<Object> tagObjectList = (List<Object>) tagsObject;
|
||||
|
||||
List<Tag> result = new LinkedList<>();
|
||||
|
||||
tagObjectList.forEach(tagObject -> {
|
||||
if (!(tagObject instanceof Map)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, Object> tagMap = (Map<String, Object>) tagObject;
|
||||
|
||||
String slug = tagMap.getOrDefault("tagUrl", "").toString();
|
||||
|
||||
Tag tag = tagService.getBySlug(slug);
|
||||
|
||||
if (null == tag) {
|
||||
tag = new Tag();
|
||||
tag.setName(tagMap.getOrDefault("tagName", "").toString());
|
||||
tag.setSlug(slug);
|
||||
tag = tagService.create(tag);
|
||||
}
|
||||
|
||||
PostTag postTag = new PostTag();
|
||||
postTag.setTagId(tag.getId());
|
||||
postTag.setPostId(postId);
|
||||
postTagService.create(postTag);
|
||||
|
||||
try {
|
||||
result.add(tag);
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to migrate a tag", e);
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private List<Menu> handleMenus(@Nullable Object menusObject) {
|
||||
if (!(menusObject instanceof List)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<Object> menuObjectList = (List<Object>) menusObject;
|
||||
|
||||
List<Menu> result = new LinkedList<>();
|
||||
|
||||
menuObjectList.forEach(menuObject -> {
|
||||
if (!(menuObject instanceof Map)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, Object> menuMap = (Map<String, Object>) menuObject;
|
||||
|
||||
Menu menu = new Menu();
|
||||
|
||||
menu.setName(menuMap.getOrDefault("menuName", "").toString());
|
||||
menu.setUrl(menuMap.getOrDefault("menuUrl", "").toString());
|
||||
// Set priority
|
||||
String sortString = menuMap.getOrDefault("menuSort", "0").toString();
|
||||
menu.setPriority(getIntegerOrDefault(sortString, 0));
|
||||
menu.setTarget(menuMap.getOrDefault("menuTarget", "_self").toString());
|
||||
menu.setIcon(menuMap.getOrDefault("menuIcon", "").toString());
|
||||
|
||||
try {
|
||||
// Create menu
|
||||
result.add(menuService.create(menu));
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to migrate a menu", e);
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private List<Photo> handleGalleries(@Nullable Object galleriesObject) {
|
||||
if (!(galleriesObject instanceof List)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<Object> galleryObjectList = (List<Object>) galleriesObject;
|
||||
|
||||
List<Photo> result = new LinkedList<>();
|
||||
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||
|
||||
galleryObjectList.forEach(galleryObject -> {
|
||||
if (!(galleriesObject instanceof Map)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, Object> galleryMap = (Map<String, Object>) galleryObject;
|
||||
|
||||
Photo photo = new Photo();
|
||||
photo.setName(galleryMap.getOrDefault("galleryName", "").toString());
|
||||
photo.setDescription(galleryMap.getOrDefault("galleryDesc", "").toString());
|
||||
photo.setLocation(galleryMap.getOrDefault("galleryLocation", "").toString());
|
||||
photo.setThumbnail(galleryMap.getOrDefault("galleryThumbnailUrl", "").toString());
|
||||
photo.setUrl(galleryMap.getOrDefault("galleryUrl", "").toString());
|
||||
|
||||
Object galleryDate = galleryMap.get("galleryDate");
|
||||
|
||||
try {
|
||||
if (galleryDate != null) {
|
||||
photo.setTakeTime(dateFormat.parse(galleryDate.toString()));
|
||||
}
|
||||
|
||||
// Create it
|
||||
result.add(photoService.create(photo));
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to create a photo", e);
|
||||
// Ignore this exception
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private List<Link> handleLinks(@Nullable Object linksObject) {
|
||||
if (!(linksObject instanceof List)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<Object> linkObjectList = (List<Object>) linksObject;
|
||||
|
||||
List<Link> result = new LinkedList<>();
|
||||
|
||||
linkObjectList.forEach(linkObject -> {
|
||||
if (!(linkObject instanceof Map)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, Object> linkMap = (Map<String, Object>) linkObject;
|
||||
|
||||
Link link = new Link();
|
||||
|
||||
link.setName(linkMap.getOrDefault("linkName", "").toString());
|
||||
link.setUrl(linkMap.getOrDefault("linkUrl", "").toString());
|
||||
link.setLogo(linkMap.getOrDefault("linkPic", "").toString());
|
||||
link.setDescription(linkMap.getOrDefault("linkDesc", "").toString());
|
||||
try {
|
||||
result.add(linkService.create(link));
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to migrate a link", e);
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private List<Attachment> handleAttachments(@Nullable Object attachmentsObject) {
|
||||
if (!(attachmentsObject instanceof List)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<Object> attachmentObjectList = (List<Object>) attachmentsObject;
|
||||
|
||||
List<Attachment> result = new LinkedList<>();
|
||||
|
||||
attachmentObjectList.forEach(attachmentObject -> {
|
||||
if (!(attachmentObject instanceof Map)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, Object> attachmentMap = (Map<String, Object>) attachmentObject;
|
||||
// Convert to attachment param
|
||||
Attachment attachment = new Attachment();
|
||||
|
||||
attachment.setName(attachmentMap.getOrDefault("attachName", "").toString());
|
||||
attachment.setPath(StringUtils.removeStart(attachmentMap.getOrDefault("attachPath", "").toString(), "/"));
|
||||
attachment.setThumbPath(attachmentMap.getOrDefault("attachSmallPath", "").toString());
|
||||
attachment.setMediaType(attachmentMap.getOrDefault("attachType", "").toString());
|
||||
attachment.setSuffix(StringUtils.removeStart(attachmentMap.getOrDefault("attachSuffix", "").toString(), "."));
|
||||
attachment.setSize(0L);
|
||||
|
||||
if (StringUtils.startsWith(attachment.getPath(), "/upload")) {
|
||||
// Set this key
|
||||
attachment.setFileKey(attachment.getPath());
|
||||
}
|
||||
|
||||
// Set location
|
||||
String attachLocation = attachmentMap.getOrDefault("attachLocation", "").toString();
|
||||
if (StringUtils.equalsIgnoreCase(attachLocation, "qiniu")) {
|
||||
attachment.setType(AttachmentType.QINIUOSS);
|
||||
} else if (StringUtils.equalsIgnoreCase(attachLocation, "upyun")) {
|
||||
attachment.setType(AttachmentType.UPOSS);
|
||||
} else {
|
||||
attachment.setType(AttachmentType.LOCAL);
|
||||
}
|
||||
|
||||
try {
|
||||
// Save to db
|
||||
Attachment createdAttachment = attachmentService.create(attachment);
|
||||
|
||||
result.add(createdAttachment);
|
||||
|
||||
} catch (Exception e) {
|
||||
// Ignore this exception
|
||||
log.warn("Failed to migrate an attachment " + attachment.getPath(), e);
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Integer getIntegerOrDefault(@Nullable String numberString, @Nullable Integer defaultNumber) {
|
||||
try {
|
||||
return Integer.valueOf(numberString);
|
||||
} catch (Exception e) {
|
||||
// Ignore this exception
|
||||
return defaultNumber;
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Long getLongOrDefault(@Nullable String numberString, @Nullable Long defaultNumber) {
|
||||
try {
|
||||
return Long.valueOf(numberString);
|
||||
} catch (Exception e) {
|
||||
// Ignore this exception
|
||||
return defaultNumber;
|
||||
}
|
||||
}
|
||||
|
||||
private Boolean getBooleanOrDefault(@Nullable String boolString, @Nullable Boolean defaultValue) {
|
||||
if (StringUtils.equalsIgnoreCase(boolString, "0")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (StringUtils.equalsIgnoreCase(boolString, "1")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (StringUtils.equalsIgnoreCase(boolString, "true")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (StringUtils.equalsIgnoreCase(boolString, "false")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
|
@ -82,11 +82,11 @@ public class SheetServiceImpl extends BasePostServiceImpl<Sheet> implements Shee
|
|||
}
|
||||
|
||||
@Override
|
||||
public Sheet createBy(Sheet sheet, Set<SheetMeta> sheetMetas, boolean autoSave) {
|
||||
public Sheet createBy(Sheet sheet, Set<SheetMeta> metas, boolean autoSave) {
|
||||
Sheet createdSheet = createOrUpdateBy(sheet);
|
||||
|
||||
// Create sheet meta data
|
||||
List<SheetMeta> sheetMetaList = sheetMetaService.createOrUpdateByPostId(sheet.getId(), sheetMetas);
|
||||
List<SheetMeta> sheetMetaList = sheetMetaService.createOrUpdateByPostId(sheet.getId(), metas);
|
||||
log.debug("Created sheet metas: [{}]", sheetMetaList);
|
||||
|
||||
if (!autoSave) {
|
||||
|
@ -109,11 +109,11 @@ public class SheetServiceImpl extends BasePostServiceImpl<Sheet> implements Shee
|
|||
}
|
||||
|
||||
@Override
|
||||
public Sheet updateBy(Sheet sheet, Set<SheetMeta> sheetMetas, boolean autoSave) {
|
||||
public Sheet updateBy(Sheet sheet, Set<SheetMeta> metas, boolean autoSave) {
|
||||
Sheet updatedSheet = createOrUpdateBy(sheet);
|
||||
|
||||
// Create sheet meta data
|
||||
List<SheetMeta> sheetMetaList = sheetMetaService.createOrUpdateByPostId(updatedSheet.getId(), sheetMetas);
|
||||
List<SheetMeta> sheetMetaList = sheetMetaService.createOrUpdateByPostId(updatedSheet.getId(), metas);
|
||||
log.debug("Created sheet metas: [{}]", sheetMetaList);
|
||||
|
||||
if (!autoSave) {
|
||||
|
@ -220,8 +220,8 @@ public class SheetServiceImpl extends BasePostServiceImpl<Sheet> implements Shee
|
|||
public Sheet removeById(Integer id) {
|
||||
|
||||
// Remove sheet metas
|
||||
List<SheetMeta> sheetMetas = sheetMetaService.removeByPostId(id);
|
||||
log.debug("Removed sheet metas: [{}]", sheetMetas);
|
||||
List<SheetMeta> metas = sheetMetaService.removeByPostId(id);
|
||||
log.debug("Removed sheet metas: [{}]", metas);
|
||||
|
||||
// Remove sheet comments
|
||||
List<SheetComment> sheetComments = sheetCommentService.removeByPostId(id);
|
||||
|
@ -264,10 +264,10 @@ public class SheetServiceImpl extends BasePostServiceImpl<Sheet> implements Shee
|
|||
|
||||
@Override
|
||||
public SheetDetailVO convertToDetailVo(Sheet sheet) {
|
||||
// List sheetMetas
|
||||
List<SheetMeta> sheetMetas = sheetMetaService.listBy(sheet.getId());
|
||||
// List metas
|
||||
List<SheetMeta> metas = sheetMetaService.listBy(sheet.getId());
|
||||
// Convert to detail vo
|
||||
return convertTo(sheet, sheetMetas);
|
||||
return convertTo(sheet, metas);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -292,17 +292,17 @@ public class SheetServiceImpl extends BasePostServiceImpl<Sheet> implements Shee
|
|||
}
|
||||
|
||||
@NonNull
|
||||
private SheetDetailVO convertTo(@NonNull Sheet sheet, List<SheetMeta> sheetMetas) {
|
||||
private SheetDetailVO convertTo(@NonNull Sheet sheet, List<SheetMeta> metas) {
|
||||
Assert.notNull(sheet, "Sheet must not be null");
|
||||
|
||||
// Convert to base detail vo
|
||||
SheetDetailVO sheetDetailVO = new SheetDetailVO().convertFrom(sheet);
|
||||
|
||||
Set<Long> sheetMetaIds = ServiceUtils.fetchProperty(sheetMetas, SheetMeta::getId);
|
||||
Set<Long> metaIds = ServiceUtils.fetchProperty(metas, SheetMeta::getId);
|
||||
|
||||
// Get sheet meta ids
|
||||
sheetDetailVO.setSheetMetaIds(sheetMetaIds);
|
||||
sheetDetailVO.setSheetMetas(sheetMetaService.convertTo(sheetMetas));
|
||||
sheetDetailVO.setMetaIds(metaIds);
|
||||
sheetDetailVO.setMetas(sheetMetaService.convertTo(metas));
|
||||
|
||||
if (StringUtils.isBlank(sheetDetailVO.getSummary())) {
|
||||
sheetDetailVO.setSummary(generateSummary(sheet.getFormatContent()));
|
||||
|
|
|
@ -4,5 +4,5 @@ ${AnsiColor.BLUE}
|
|||
/ /_/ / __ `/ / __ \
|
||||
/ __ / /_/ / / /_/ /
|
||||
/_/ /_/\__,_/_/\____/
|
||||
${AnsiColor.BRIGHT_YELLOW}
|
||||
::: Spring-Boot ${spring-boot.version} ::: Halo (version:${application.formatted-version})
|
||||
${AnsiColor.BRIGHT_YELLOW}
|
||||
Version: ${application.version}
|
Loading…
Reference in New Issue