mirror of https://github.com/halo-dev/halo
Support import post.
parent
76e6f6eb2b
commit
084902c719
|
@ -1,9 +1,18 @@
|
|||
package run.halo.app.controller.admin.api;
|
||||
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
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.model.dto.post.BasePostDetailDTO;
|
||||
import run.halo.app.service.BackupService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Backup controller
|
||||
*
|
||||
|
@ -19,4 +28,15 @@ public class BackupController {
|
|||
public BackupController(BackupService backupService) {
|
||||
this.backupService = backupService;
|
||||
}
|
||||
|
||||
@PostMapping("import/markdowns")
|
||||
@ApiOperation("Import markdowns")
|
||||
public List<BasePostDetailDTO> backupMarkdowns(@RequestPart("files") MultipartFile[] files) throws IOException {
|
||||
List<BasePostDetailDTO> result = new LinkedList<>();
|
||||
for (MultipartFile file : files) {
|
||||
BasePostDetailDTO post = backupService.importMarkdowns(file);
|
||||
result.add(post);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,6 +28,8 @@ public class ThemeProperty {
|
|||
*/
|
||||
private String website;
|
||||
|
||||
private String repo;
|
||||
|
||||
/**
|
||||
* Theme description.
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package run.halo.app.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author ryanwang
|
||||
* @date 2019-05-25
|
||||
*/
|
||||
@Data
|
||||
public class BackupDTO {
|
||||
|
||||
private String fileName;
|
||||
|
||||
private Date createTime;
|
||||
|
||||
private String fileSize;
|
||||
|
||||
private String fileType;
|
||||
|
||||
private String type;
|
||||
}
|
|
@ -1,5 +1,10 @@
|
|||
package run.halo.app.service;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import run.halo.app.model.dto.post.BasePostDetailDTO;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Backup service interface.
|
||||
*
|
||||
|
@ -7,4 +12,12 @@ package run.halo.app.service;
|
|||
* @date 19-4-26
|
||||
*/
|
||||
public interface BackupService {
|
||||
|
||||
/**
|
||||
* Backup posts and sheets
|
||||
*
|
||||
* @param file file
|
||||
* @return post info
|
||||
*/
|
||||
BasePostDetailDTO importMarkdowns(MultipartFile file) throws IOException;
|
||||
}
|
||||
|
|
|
@ -101,10 +101,11 @@ public interface PostService extends BasePostService<Post> {
|
|||
* Import post from markdown document.
|
||||
*
|
||||
* @param markdown markdown document.
|
||||
* @param filename filename
|
||||
* @return imported post
|
||||
*/
|
||||
@NonNull
|
||||
PostDetailVO importMarkdown(@NonNull String markdown);
|
||||
PostDetailVO importMarkdown(@NonNull String markdown, String filename);
|
||||
|
||||
/**
|
||||
* Export post to markdown file by post id.
|
||||
|
|
|
@ -1,7 +1,13 @@
|
|||
package run.halo.app.service.impl;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import run.halo.app.model.dto.post.BasePostDetailDTO;
|
||||
import run.halo.app.service.BackupService;
|
||||
import run.halo.app.service.PostService;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Backup service implementation.
|
||||
|
@ -12,4 +18,20 @@ import run.halo.app.service.BackupService;
|
|||
@Service
|
||||
public class BackupServiceImpl implements BackupService {
|
||||
|
||||
private final PostService postService;
|
||||
|
||||
public BackupServiceImpl(PostService postService) {
|
||||
this.postService = postService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BasePostDetailDTO importMarkdowns(MultipartFile file) throws IOException {
|
||||
|
||||
// Read markdown content.
|
||||
String markdown = IoUtil.read(file.getInputStream(), "UTF-8");
|
||||
|
||||
// TODO sheet import
|
||||
|
||||
return postService.importMarkdown(markdown, file.getOriginalFilename());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package run.halo.app.service.impl;
|
|||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.text.StrBuilder;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
|
@ -277,7 +278,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
}
|
||||
|
||||
@Override
|
||||
public PostDetailVO importMarkdown(String markdown) {
|
||||
public PostDetailVO importMarkdown(String markdown, String filename) {
|
||||
Assert.notNull(markdown, "Markdown document must not be null");
|
||||
|
||||
// Render markdown to html document.
|
||||
|
@ -345,8 +346,19 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
}
|
||||
}
|
||||
|
||||
if (null == post.getStatus()) {
|
||||
post.setStatus(PostStatus.PUBLISHED);
|
||||
}
|
||||
|
||||
if (StrUtil.isEmpty(post.getTitle())) {
|
||||
post.setTitle(filename);
|
||||
}
|
||||
|
||||
if (StrUtil.isEmpty(post.getUrl())) {
|
||||
post.setUrl(DateUtil.format(new Date(), "yyyyMMddHHmmss"));
|
||||
}
|
||||
|
||||
post.setOriginalContent(markdown);
|
||||
post.setFormatContent(content);
|
||||
|
||||
return createBy(post, tagIds, categoryIds, false);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue