Support import post.

pull/172/head
ruibaby 2019-05-29 23:40:49 +08:00
parent 76e6f6eb2b
commit 084902c719
7 changed files with 96 additions and 3 deletions

View File

@ -1,9 +1,18 @@
package run.halo.app.controller.admin.api; 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.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController; 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 run.halo.app.service.BackupService;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
/** /**
* Backup controller * Backup controller
* *
@ -19,4 +28,15 @@ public class BackupController {
public BackupController(BackupService backupService) { public BackupController(BackupService backupService) {
this.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;
}
} }

View File

@ -28,6 +28,8 @@ public class ThemeProperty {
*/ */
private String website; private String website;
private String repo;
/** /**
* Theme description. * Theme description.
*/ */

View File

@ -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;
}

View File

@ -1,5 +1,10 @@
package run.halo.app.service; 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. * Backup service interface.
* *
@ -7,4 +12,12 @@ package run.halo.app.service;
* @date 19-4-26 * @date 19-4-26
*/ */
public interface BackupService { public interface BackupService {
/**
* Backup posts and sheets
*
* @param file file
* @return post info
*/
BasePostDetailDTO importMarkdowns(MultipartFile file) throws IOException;
} }

View File

@ -101,10 +101,11 @@ public interface PostService extends BasePostService<Post> {
* Import post from markdown document. * Import post from markdown document.
* *
* @param markdown markdown document. * @param markdown markdown document.
* @param filename filename
* @return imported post * @return imported post
*/ */
@NonNull @NonNull
PostDetailVO importMarkdown(@NonNull String markdown); PostDetailVO importMarkdown(@NonNull String markdown, String filename);
/** /**
* Export post to markdown file by post id. * Export post to markdown file by post id.

View File

@ -1,7 +1,13 @@
package run.halo.app.service.impl; package run.halo.app.service.impl;
import cn.hutool.core.io.IoUtil;
import org.springframework.stereotype.Service; 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.BackupService;
import run.halo.app.service.PostService;
import java.io.IOException;
/** /**
* Backup service implementation. * Backup service implementation.
@ -12,4 +18,20 @@ import run.halo.app.service.BackupService;
@Service @Service
public class BackupServiceImpl implements BackupService { 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());
}
} }

View File

@ -2,6 +2,7 @@ package run.halo.app.service.impl;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrBuilder; import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
@ -277,7 +278,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
} }
@Override @Override
public PostDetailVO importMarkdown(String markdown) { public PostDetailVO importMarkdown(String markdown, String filename) {
Assert.notNull(markdown, "Markdown document must not be null"); Assert.notNull(markdown, "Markdown document must not be null");
// Render markdown to html document. // 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.setOriginalContent(markdown);
post.setFormatContent(content);
return createBy(post, tagIds, categoryIds, false); return createBy(post, tagIds, categoryIds, false);
} }