use of run.halo.app.model.vo.PostMarkdownVO in project halo by ruibaby.
the class BackupServiceImpl method exportMarkdowns.
@Override
public BackupDTO exportMarkdowns(PostMarkdownParam postMarkdownParam) throws IOException {
// Query all Post data
List<PostMarkdownVO> postMarkdownList = postService.listPostMarkdowns();
Assert.notEmpty(postMarkdownList, "当前无文章可以导出");
// Write files to the temporary directory
String markdownFileTempPathName = haloProperties.getBackupMarkdownDir() + HaloUtils.simpleUUID().hashCode();
for (PostMarkdownVO postMarkdownVo : postMarkdownList) {
StringBuilder content = new StringBuilder();
Boolean needFrontMatter = Optional.ofNullable(postMarkdownParam.getNeedFrontMatter()).orElse(false);
if (needFrontMatter) {
// Add front-matter
content.append(postMarkdownVo.getFrontMatter()).append("\n");
}
content.append(postMarkdownVo.getOriginalContent());
try {
String markdownFileName = postMarkdownVo.getTitle() + "-" + postMarkdownVo.getSlug() + ".md";
Path markdownFilePath = Paths.get(markdownFileTempPathName, markdownFileName);
if (!Files.exists(markdownFilePath.getParent())) {
Files.createDirectories(markdownFilePath.getParent());
}
Path markdownDataPath = Files.createFile(markdownFilePath);
FileUtils.writeStringToFile(markdownDataPath.toFile(), content.toString());
} catch (IOException e) {
throw new ServiceException("导出数据失败", e);
}
}
// Create zip path
String markdownZipFileName = HALO_BACKUP_MARKDOWN_PREFIX + DateTimeUtils.format(LocalDateTime.now(), HORIZONTAL_LINE_DATETIME_FORMATTER) + HaloUtils.simpleUUID().hashCode() + ".zip";
// Create zip file
Path markdownZipFilePath = Paths.get(haloProperties.getBackupMarkdownDir(), markdownZipFileName);
if (!Files.exists(markdownZipFilePath.getParent())) {
Files.createDirectories(markdownZipFilePath.getParent());
}
Path markdownZipPath = Files.createFile(markdownZipFilePath);
// Zip file
try (ZipOutputStream markdownZipOut = new ZipOutputStream(Files.newOutputStream(markdownZipPath))) {
// Zip temporary directory
Path markdownFileTempPath = Paths.get(markdownFileTempPathName);
run.halo.app.utils.FileUtils.zip(markdownFileTempPath, markdownZipOut);
// Zip upload sub-directory
String uploadPathName = FileHandler.normalizeDirectory(haloProperties.getWorkDir()) + UPLOAD_SUB_DIR;
Path uploadPath = Paths.get(uploadPathName);
if (Files.exists(uploadPath)) {
run.halo.app.utils.FileUtils.zip(uploadPath, markdownZipOut);
}
// Remove files in the temporary directory
run.halo.app.utils.FileUtils.deleteFolder(markdownFileTempPath);
// Build backup dto
return buildBackupDto(DATA_EXPORT_MARKDOWN_BASE_URI, markdownZipPath);
} catch (IOException e) {
throw new ServiceException("Failed to export markdowns", e);
}
}
use of run.halo.app.model.vo.PostMarkdownVO in project halo by ruibaby.
the class PostServiceImpl method convertToPostMarkdownVo.
private PostMarkdownVO convertToPostMarkdownVo(Post post) {
PostMarkdownVO postMarkdownVO = new PostMarkdownVO();
StringBuilder frontMatter = new StringBuilder("---\n");
frontMatter.append("title: ").append(post.getTitle()).append("\n");
frontMatter.append("date: ").append(post.getCreateTime()).append("\n");
frontMatter.append("updated: ").append(post.getUpdateTime()).append("\n");
// set fullPath
frontMatter.append("url: ").append(postAssembler.buildFullPath(post)).append("\n");
// set category
List<Category> categories = postCategoryService.listCategoriesBy(post.getId());
StringBuilder categoryContent = new StringBuilder();
for (int i = 0; i < categories.size(); i++) {
Category category = categories.get(i);
String categoryName = category.getName();
if (i == 0) {
categoryContent.append(categoryName);
} else {
categoryContent.append(" | ").append(categoryName);
}
}
frontMatter.append("categories: ").append(categoryContent.toString()).append("\n");
// set tags
List<Tag> tags = postTagService.listTagsBy(post.getId());
StringBuilder tagContent = new StringBuilder();
for (int i = 0; i < tags.size(); i++) {
Tag tag = tags.get(i);
String tagName = tag.getName();
if (i == 0) {
tagContent.append(tagName);
} else {
tagContent.append(" | ").append(tagName);
}
}
frontMatter.append("tags: ").append(tagContent).append("\n");
frontMatter.append("---\n");
postMarkdownVO.setFrontMatter(frontMatter.toString());
PatchedContent postContent = post.getContent();
postMarkdownVO.setOriginalContent(postContent.getOriginalContent());
postMarkdownVO.setTitle(post.getTitle());
postMarkdownVO.setSlug(post.getSlug());
return postMarkdownVO;
}
use of run.halo.app.model.vo.PostMarkdownVO in project halo by halo-dev.
the class BackupServiceImpl method exportMarkdowns.
@Override
public BackupDTO exportMarkdowns(PostMarkdownParam postMarkdownParam) throws IOException {
// Query all Post data
List<PostMarkdownVO> postMarkdownList = postService.listPostMarkdowns();
Assert.notEmpty(postMarkdownList, "当前无文章可以导出");
// Write files to the temporary directory
String markdownFileTempPathName = haloProperties.getBackupMarkdownDir() + HaloUtils.simpleUUID().hashCode();
for (PostMarkdownVO postMarkdownVo : postMarkdownList) {
StringBuilder content = new StringBuilder();
Boolean needFrontMatter = Optional.ofNullable(postMarkdownParam.getNeedFrontMatter()).orElse(false);
if (needFrontMatter) {
// Add front-matter
content.append(postMarkdownVo.getFrontMatter()).append("\n");
}
content.append(postMarkdownVo.getOriginalContent());
try {
String markdownFileName = postMarkdownVo.getTitle() + "-" + postMarkdownVo.getSlug() + ".md";
Path markdownFilePath = Paths.get(markdownFileTempPathName, markdownFileName);
if (!Files.exists(markdownFilePath.getParent())) {
Files.createDirectories(markdownFilePath.getParent());
}
Path markdownDataPath = Files.createFile(markdownFilePath);
FileUtils.writeStringToFile(markdownDataPath.toFile(), content.toString());
} catch (IOException e) {
throw new ServiceException("导出数据失败", e);
}
}
// Create zip path
String markdownZipFileName = HALO_BACKUP_MARKDOWN_PREFIX + DateTimeUtils.format(LocalDateTime.now(), HORIZONTAL_LINE_DATETIME_FORMATTER) + HaloUtils.simpleUUID().hashCode() + ".zip";
// Create zip file
Path markdownZipFilePath = Paths.get(haloProperties.getBackupMarkdownDir(), markdownZipFileName);
if (!Files.exists(markdownZipFilePath.getParent())) {
Files.createDirectories(markdownZipFilePath.getParent());
}
Path markdownZipPath = Files.createFile(markdownZipFilePath);
// Zip file
try (ZipOutputStream markdownZipOut = new ZipOutputStream(Files.newOutputStream(markdownZipPath))) {
// Zip temporary directory
Path markdownFileTempPath = Paths.get(markdownFileTempPathName);
run.halo.app.utils.FileUtils.zip(markdownFileTempPath, markdownZipOut);
// Zip upload sub-directory
String uploadPathName = FileHandler.normalizeDirectory(haloProperties.getWorkDir()) + UPLOAD_SUB_DIR;
Path uploadPath = Paths.get(uploadPathName);
if (Files.exists(uploadPath)) {
run.halo.app.utils.FileUtils.zip(uploadPath, markdownZipOut);
}
// Remove files in the temporary directory
run.halo.app.utils.FileUtils.deleteFolder(markdownFileTempPath);
// Build backup dto
return buildBackupDto(DATA_EXPORT_MARKDOWN_BASE_URI, markdownZipPath);
} catch (IOException e) {
throw new ServiceException("Failed to export markdowns", e);
}
}
use of run.halo.app.model.vo.PostMarkdownVO in project halo by halo-dev.
the class PostServiceImpl method listPostMarkdowns.
@Override
public List<PostMarkdownVO> listPostMarkdowns() {
List<Post> allPostList = listAll();
List<PostMarkdownVO> result = new ArrayList<>(allPostList.size());
for (Post post : allPostList) {
Content postContent = getContentById(post.getId());
post.setContent(PatchedContent.of(postContent));
result.add(convertToPostMarkdownVo(post));
}
return result;
}
use of run.halo.app.model.vo.PostMarkdownVO in project halo-plugin-experimental by guqing.
the class BackupServiceImpl method exportMarkdowns.
@Override
public BackupDTO exportMarkdowns(PostMarkdownParam postMarkdownParam) throws IOException {
// Query all Post data
List<PostMarkdownVO> postMarkdownList = postService.listPostMarkdowns();
Assert.notEmpty(postMarkdownList, "当前无文章可以导出");
// Write files to the temporary directory
String markdownFileTempPathName = haloProperties.getBackupMarkdownDir() + HaloUtils.simpleUUID().hashCode();
for (PostMarkdownVO postMarkdownVo : postMarkdownList) {
StringBuilder content = new StringBuilder();
Boolean needFrontMatter = Optional.ofNullable(postMarkdownParam.getNeedFrontMatter()).orElse(false);
if (needFrontMatter) {
// Add front-matter
content.append(postMarkdownVo.getFrontMatter()).append("\n");
}
content.append(postMarkdownVo.getOriginalContent());
try {
String markdownFileName = postMarkdownVo.getTitle() + "-" + postMarkdownVo.getSlug() + ".md";
Path markdownFilePath = Paths.get(markdownFileTempPathName, markdownFileName);
if (!Files.exists(markdownFilePath.getParent())) {
Files.createDirectories(markdownFilePath.getParent());
}
Path markdownDataPath = Files.createFile(markdownFilePath);
FileUtils.writeStringToFile(markdownDataPath.toFile(), content.toString());
} catch (IOException e) {
throw new ServiceException("导出数据失败", e);
}
}
// Create zip path
String markdownZipFileName = HALO_BACKUP_MARKDOWN_PREFIX + DateTimeUtils.format(LocalDateTime.now(), HORIZONTAL_LINE_DATETIME_FORMATTER) + HaloUtils.simpleUUID().hashCode() + ".zip";
// Create zip file
Path markdownZipFilePath = Paths.get(haloProperties.getBackupMarkdownDir(), markdownZipFileName);
if (!Files.exists(markdownZipFilePath.getParent())) {
Files.createDirectories(markdownZipFilePath.getParent());
}
Path markdownZipPath = Files.createFile(markdownZipFilePath);
// Zip file
try (ZipOutputStream markdownZipOut = new ZipOutputStream(Files.newOutputStream(markdownZipPath))) {
// Zip temporary directory
Path markdownFileTempPath = Paths.get(markdownFileTempPathName);
FileUtils.zip(markdownFileTempPath, markdownZipOut);
// Zip upload sub-directory
String uploadPathName = FileHandler.normalizeDirectory(haloProperties.getWorkDir()) + UPLOAD_SUB_DIR;
Path uploadPath = Paths.get(uploadPathName);
if (Files.exists(uploadPath)) {
FileUtils.zip(uploadPath, markdownZipOut);
}
// Remove files in the temporary directory
FileUtils.deleteFolder(markdownFileTempPath);
// Build backup dto
return buildBackupDto(DATA_EXPORT_MARKDOWN_BASE_URI, markdownZipPath);
} catch (IOException e) {
throw new ServiceException("Failed to export markdowns", e);
}
}
Aggregations