zhangjinyang
2025-05-23 aff1785df0f25c60f0a248154f30886e2b081478
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package tech.aiflowy.ai.node;
 
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.agentsflex.core.chain.Chain;
import com.agentsflex.core.chain.node.BaseNode;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFStyle;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTInd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPrGeneral;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle;
import tech.aiflowy.common.filestorage.FileStorageService;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class MakeFileNode extends BaseNode {
 
    private final FileStorageService fileStorageService;
    private final String suffix;
 
    public MakeFileNode(FileStorageService fileStorageService, String suffix) {
        this.fileStorageService = fileStorageService;
        this.suffix = suffix;
    }
 
    @Override
    protected Map<String, Object> execute(Chain chain) {
        Map<String, Object> map = chain.getParameterValues(this);
 
        Map<String, Object> res = new HashMap<>();
 
        String content = map.get("content").toString();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        createFile(suffix, content, os);
 
        String fileName = IdUtil.fastSimpleUUID() + "." + suffix;
        CustomFile file = new CustomFile(fileName, os.toByteArray());
        String url = fileStorageService.save(file);
        res.put("url", url);
        return res;
    }
 
    private void createFile(String suffix, String content, ByteArrayOutputStream os) {
        if ("docx".equals(suffix)) {
            docx(content, os);
        }
    }
 
    private void docx(String content, ByteArrayOutputStream os) {
        String separator = "\n";
        List<String> split = StrUtil.split(content, separator);
        // 创建一个新的Word文档
        XWPFDocument doc = new XWPFDocument();
        // 创建样式
        CTStyle ctStyle = CTStyle.Factory.newInstance();
        ctStyle.setStyleId("IndentStyle");
        CTPPrGeneral pPr = ctStyle.addNewPPr();
        CTInd ind = pPr.addNewInd();
        ind.setFirstLine(400);
        doc.createStyles().addStyle(new XWPFStyle(ctStyle));
 
        for (String str : split) {
            // 创建段落
            XWPFParagraph paragraph = doc.createParagraph();
            paragraph.setStyle("IndentStyle");
            XWPFRun run = paragraph.createRun();
            run.setText(str);
        }
        try {
            doc.write(os);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                os.close();
                doc.close();
            } catch (IOException e) {
                System.out.println("关闭流异常" + e.getMessage());
            }
        }
    }
}