Search in sources :

Example 1 with PostMarkdownVO

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);
    }
}
Also used : Path(java.nio.file.Path) PostMarkdownVO(run.halo.app.model.vo.PostMarkdownVO) ServiceException(run.halo.app.exception.ServiceException) ZipOutputStream(java.util.zip.ZipOutputStream) IOException(java.io.IOException)

Example 2 with PostMarkdownVO

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;
}
Also used : PostMarkdownVO(run.halo.app.model.vo.PostMarkdownVO) PostCategory(run.halo.app.model.entity.PostCategory) Category(run.halo.app.model.entity.Category) PostTag(run.halo.app.model.entity.PostTag) Tag(run.halo.app.model.entity.Tag) PatchedContent(run.halo.app.model.entity.Content.PatchedContent)

Example 3 with 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);
    }
}
Also used : Path(java.nio.file.Path) PostMarkdownVO(run.halo.app.model.vo.PostMarkdownVO) ServiceException(run.halo.app.exception.ServiceException) ZipOutputStream(java.util.zip.ZipOutputStream) IOException(java.io.IOException)

Example 4 with PostMarkdownVO

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;
}
Also used : PostMarkdownVO(run.halo.app.model.vo.PostMarkdownVO) Post(run.halo.app.model.entity.Post) Content(run.halo.app.model.entity.Content) PatchedContent(run.halo.app.model.entity.Content.PatchedContent) ArrayList(java.util.ArrayList)

Example 5 with PostMarkdownVO

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);
    }
}
Also used : Path(java.nio.file.Path) PostMarkdownVO(run.halo.app.model.vo.PostMarkdownVO) ServiceException(run.halo.app.exception.ServiceException) ZipOutputStream(java.util.zip.ZipOutputStream) IOException(java.io.IOException)

Aggregations

PostMarkdownVO (run.halo.app.model.vo.PostMarkdownVO)9 PatchedContent (run.halo.app.model.entity.Content.PatchedContent)4 IOException (java.io.IOException)3 Path (java.nio.file.Path)3 ArrayList (java.util.ArrayList)3 ZipOutputStream (java.util.zip.ZipOutputStream)3 ServiceException (run.halo.app.exception.ServiceException)3 Category (run.halo.app.model.entity.Category)3 Post (run.halo.app.model.entity.Post)3 PostCategory (run.halo.app.model.entity.PostCategory)3 PostTag (run.halo.app.model.entity.PostTag)3 Tag (run.halo.app.model.entity.Tag)3 Content (run.halo.app.model.entity.Content)2