package com.heima.article.test;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.ArticleApplication;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.service.ApArticleService;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest(classes = ArticleApplication.class)
@RunWith(SpringRunner.class)
public class ArticleFreemarkerTest {
@Autowired
private ApArticleContentMapper apArticleContentMapper;
@Autowired
private Configuration configuration;
@Autowired
private FileStorageService fileStorageService;
@Autowired
private ApArticleService articleService;
@Test
public void createStaticUrlTest() throws IOException, TemplateException {
//已知文章id
//1、获取文章内容
List<ApArticleContent> apArticleContents = apArticleContentMapper.selectList(Wrappers.<ApArticleContent>lambdaQuery().select(ApArticleContent::getArticleId, ApArticleContent::getContent));
for(ApArticleContent apArticleContent: apArticleContents){
if(apArticleContent != null && StringUtils.isNotBlank(apArticleContent.getContent())){
//2、文章内容通过freemarker生成html文件
Template template = configuration.getTemplate("article.ftl");
//数据模型
Map<String, Object> content = new HashMap<>();
content.put("content", JSONArray.parseArray(apArticleContent.getContent()));
StringWriter out = new StringWriter();
//合成
template.process(content, out);
//3、把html文件上传到minio中
ByteArrayInputStream in = new ByteArrayInputStream(out.toString().getBytes());
String path = fileStorageService.uploadHtmlFile("", apArticleContent.getArticleId() + ".html", in);
//4、修改ap_article表,保存static_url字段
articleService.update(Wrappers.<ApArticle>lambdaUpdate().eq(ApArticle::getId ,apArticleContent.getArticleId())
.set(ApArticle::getStaticUrl, path));
}
}
}
}