静态文件生成

文章端创建app相关文章时,生成文章详情静态页上传到MinIO中

审核文章——feign远程——>修改或创建app相关数据——异步——>生成静态文件

                    文章微服务

 

自媒体用户发布文章后 审核成功后将 异步 feign 远程调用文章微服务,在文章微服务中有个方法将通过审核的文章保存或修改文章,以供移动端app用户查看。因此将生成静态文件的模块放到此方法中:保存或修改完成后,生成文章静态详情页上传到MinIO中。

在文章微服务引导类上开启异步调用

复制代码
package com.heima.article.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@Transactional
public class ArticleFreemarkerServiceImpl implements ArticleFreemarkerService {
    

    @Autowired
    private Configuration configuration;

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private ApArticleService articleService;

    /**
     * 生成静态文件上传到minIO中
     * @param apArticle  获取文章内容
     * @param content    静态文件名称含有aritlceId
     */
    @Async
    @Override
    public void buildArticleToMinIO(ApArticle apArticle, String content) {
        //已知文章id
        //1、获取文章内容
        if(content != null && StringUtils.isNotBlank(content)){
            //2、文章内容通过freemarker生成html文件
            Template template = null;
            StringWriter out = new StringWriter();
            try {
                template = configuration.getTemplate("article.ftl");
                //数据模型
                Map<String, Object> contentDataModel = new HashMap<>();
                contentDataModel.put("content", JSONArray.parseArray(content));
                //合成
                template.process(contentDataModel, out);
                
            } catch (Exception e) {
                e.printStackTrace();
            }
           
            //3、把html文件上传到minio中
            ByteArrayInputStream in = new ByteArrayInputStream(out.toString().getBytes());
            String path = fileStorageService.uploadHtmlFile("", apArticle.getId()+ ".html", in);
            //4、修改ap_article表,保存static_url字段
            articleService.update(Wrappers.<ApArticle>lambdaUpdate().eq(ApArticle::getId ,apArticle.getId())
                    .set(ApArticle::getStaticUrl, path));
            
        }
    }
}
复制代码

 

posted @   佛系粥米  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示