初始化
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package tech.easyflow.ai.utils;
|
||||
|
||||
import tech.easyflow.common.web.exceptions.ProgramException;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Date;
|
||||
|
||||
public class CommonFiledUtil {
|
||||
public static void commonFiled(Object t, BigInteger userId, BigInteger tenantId, BigInteger deptId) {
|
||||
Method[] methods = t.getClass().getMethods();
|
||||
try {
|
||||
for (Method m : methods) {
|
||||
String name = m.getName();
|
||||
if ("setDeptId".equals(name)) {
|
||||
m.invoke(t, deptId);
|
||||
}
|
||||
if ("setTenantId".equals(name)) {
|
||||
m.invoke(t, tenantId);
|
||||
}
|
||||
if ("setCreatedBy".equals(name)) {
|
||||
m.invoke(t, userId);
|
||||
}
|
||||
if ("setModifiedBy".equals(name)) {
|
||||
m.invoke(t, userId);
|
||||
}
|
||||
if ("setCreated".equals(name)) {
|
||||
m.invoke(t, new Date());
|
||||
}
|
||||
if ("setModified".equals(name)) {
|
||||
m.invoke(t, new Date());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new ProgramException("commonFiled反射出错:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
|
||||
package tech.easyflow.ai.utils;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.BeanWrapper;
|
||||
import org.springframework.beans.BeanWrapperImpl;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class CustomBeanUtils {
|
||||
|
||||
public static void copyPropertiesIgnoreNull(Object source, Object target) {
|
||||
BeanUtils.copyProperties(source, target, getNullPropertyNames(source));
|
||||
}
|
||||
|
||||
private static String[] getNullPropertyNames(Object source) {
|
||||
final BeanWrapper src = new BeanWrapperImpl(source);
|
||||
PropertyDescriptor[] pds = src.getPropertyDescriptors();
|
||||
|
||||
Set<String> emptyNames = new HashSet<>();
|
||||
for (PropertyDescriptor pd : pds) {
|
||||
Object srcValue = src.getPropertyValue(pd.getName());
|
||||
if (srcValue == null) {
|
||||
emptyNames.add(pd.getName());
|
||||
}
|
||||
}
|
||||
String[] result = new String[emptyNames.size()];
|
||||
return emptyNames.toArray(result);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
package tech.easyflow.ai.utils;
|
||||
|
||||
import com.easyagents.flow.core.util.OkHttpClientUtil;
|
||||
import okhttp3.Call;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import org.apache.pdfbox.multipdf.Splitter;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPageTree;
|
||||
import org.apache.pdfbox.text.PDFTextStripper;
|
||||
import org.apache.poi.hwpf.HWPFDocument;
|
||||
import org.apache.poi.hwpf.extractor.WordExtractor;
|
||||
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFTable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class DocUtil {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(DocUtil.class);
|
||||
|
||||
public static byte[] downloadFile(String url) {
|
||||
Request.Builder reqBuilder = new Request.Builder()
|
||||
.url(url);
|
||||
Request build = reqBuilder.build();
|
||||
OkHttpClient client = OkHttpClientUtil.buildDefaultClient();
|
||||
Call call = client.newCall(build);
|
||||
try (Response response = call.execute()) {
|
||||
if (response.body() != null) {
|
||||
return response.body().bytes();
|
||||
} else {
|
||||
throw new RuntimeException("下载内容为空");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("下载文件失败:", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String readWordFile(String suffix, InputStream is) {
|
||||
String content = "";
|
||||
try {
|
||||
if ("docx".equals(suffix)) {
|
||||
XWPFDocument document = new XWPFDocument(is);
|
||||
XWPFWordExtractor extractor = new XWPFWordExtractor(document);
|
||||
content = extractor.getText();
|
||||
// 关闭资源
|
||||
extractor.close();
|
||||
document.close();
|
||||
}
|
||||
if ("doc".equals(suffix)) {
|
||||
HWPFDocument document = new HWPFDocument(is);
|
||||
WordExtractor extractor = new WordExtractor(document);
|
||||
// 获取全部文本
|
||||
content = extractor.getText();
|
||||
// 关闭资源
|
||||
extractor.close();
|
||||
document.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("读取word文件失败:", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
public static String readPdfFile(InputStream is) {
|
||||
try (PDDocument document = PDDocument.load(is)) {
|
||||
PDFTextStripper stripper = new PDFTextStripper();
|
||||
return stripper.getText(document);
|
||||
} catch (Exception e) {
|
||||
log.error("读取pdf文件失败:", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<Integer, byte[]> splitPdf(byte[] bytes, int splitSize) {
|
||||
|
||||
Map<Integer, byte[]> map = new HashMap<>();
|
||||
|
||||
int i = 0;
|
||||
try (PDDocument document = PDDocument.load(bytes)) {
|
||||
|
||||
PDPageTree pages = document.getPages();
|
||||
// 判断页面数量是否小于等于拆分大小
|
||||
if (pages.getCount() <= splitSize) {
|
||||
map.put(1, bytes);
|
||||
return map;
|
||||
}
|
||||
// 创建Splitter实例
|
||||
Splitter splitter = new Splitter();
|
||||
|
||||
// 设置拆分大小(每份文档的页数)
|
||||
splitter.setSplitAtPage(splitSize);
|
||||
|
||||
// 拆分文档
|
||||
List<PDDocument> splitDocuments = splitter.split(document);
|
||||
|
||||
// 保存拆分后的文档
|
||||
i = 1;
|
||||
for (PDDocument splitDoc : splitDocuments) {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
splitDoc.save(baos);
|
||||
map.put(i, baos.toByteArray());
|
||||
splitDoc.close();
|
||||
i++;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("PDF拆分失败:", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
System.out.println("PDF拆分完成,共生成 " + (i - 1) + " 个文件。");
|
||||
return map;
|
||||
}
|
||||
|
||||
public static Map<Integer, byte[]> splitWord(byte[] bytes, int splitSize) {
|
||||
|
||||
Map<Integer, byte[]> map = new HashMap<>();
|
||||
|
||||
InputStream is = new ByteArrayInputStream(bytes);
|
||||
try {
|
||||
XWPFDocument document = new XWPFDocument(is);
|
||||
|
||||
List<XWPFParagraph> paragraphs = document.getParagraphs();
|
||||
List<XWPFTable> tables = document.getTables();
|
||||
|
||||
int totalParts = (int) Math.ceil((double) paragraphs.size() / splitSize);
|
||||
|
||||
for (int i = 0; i < totalParts; i++) {
|
||||
XWPFDocument newDoc = new XWPFDocument();
|
||||
|
||||
int start = i * splitSize;
|
||||
int end = Math.min((i + 1) * splitSize, paragraphs.size());
|
||||
|
||||
// 复制段落
|
||||
for (int j = start; j < end; j++) {
|
||||
XWPFParagraph newPara = newDoc.createParagraph();
|
||||
newPara.getCTP().set(paragraphs.get(j).getCTP());
|
||||
}
|
||||
|
||||
// 复制表格(可选,可以根据需要调整)
|
||||
for (XWPFTable table : tables) {
|
||||
XWPFTable newTable = newDoc.createTable();
|
||||
newTable.getCTTbl().set(table.getCTTbl());
|
||||
}
|
||||
|
||||
// 保存拆分后的文档
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
newDoc.write(baos);
|
||||
map.put(i, baos.toByteArray());
|
||||
newDoc.close();
|
||||
}
|
||||
document.close();
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
log.error("Word文档拆分失败:", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public static String getSuffix(String name) {
|
||||
return name.substring(name.lastIndexOf(".") + 1);
|
||||
}
|
||||
|
||||
public static byte[] readBytes(InputStream inputStream) {
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
try {
|
||||
byte[] data = new byte[1024];
|
||||
int bytesRead;
|
||||
|
||||
while ((bytesRead = inputStream.read(data, 0, data.length)) != -1) {
|
||||
buffer.write(data, 0, bytesRead);
|
||||
}
|
||||
buffer.flush();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return buffer.toByteArray();
|
||||
}
|
||||
|
||||
|
||||
public static String getFileNameByUrl(String url) {
|
||||
return url.substring(url.lastIndexOf("/") + 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
package tech.easyflow.ai.utils;
|
||||
|
||||
public class RegexUtils {
|
||||
|
||||
|
||||
public static final String ALL_NUMBER = "\\d+";
|
||||
|
||||
public static final String NUMBER_LETTER_INCLUDE_ALL_LETTER = "[a-zA-Z0-9]+";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package tech.easyflow.ai.utils;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.easyagents.flow.core.chain.Chain;
|
||||
import tech.easyflow.common.constant.Constants;
|
||||
import tech.easyflow.common.entity.LoginAccount;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class WorkFlowUtil {
|
||||
|
||||
public final static String USER_KEY = "user";
|
||||
public final static String WORKFLOW_KEY = "workflow";
|
||||
|
||||
public static String removeSensitiveInfo(String originJson) {
|
||||
JSONObject workflowInfo = JSON.parseObject(originJson);
|
||||
JSONArray nodes = workflowInfo.getJSONArray("nodes");
|
||||
for (Object node : nodes) {
|
||||
JSONObject nodeInfo = (JSONObject) node;
|
||||
JSONObject data = nodeInfo.getJSONObject("data");
|
||||
JSONObject newData = new JSONObject();
|
||||
newData.put("outputDefs", data.get("outputDefs"));
|
||||
newData.put("parameters", data.get("parameters"));
|
||||
newData.put("title", data.get("title"));
|
||||
newData.put("description", data.get("description"));
|
||||
nodeInfo.put("data", newData);
|
||||
}
|
||||
return workflowInfo.toJSONString();
|
||||
}
|
||||
|
||||
public static LoginAccount getOperator(Chain chain) {
|
||||
Object cache = chain.getState().getMemory().get(Constants.LOGIN_USER_KEY);
|
||||
return cache == null ? defaultAccount() : (LoginAccount) cache;
|
||||
}
|
||||
|
||||
public static LoginAccount defaultAccount() {
|
||||
LoginAccount account = new LoginAccount();
|
||||
account.setId(new BigInteger("0"));
|
||||
account.setDeptId(new BigInteger("0"));
|
||||
account.setTenantId(new BigInteger("0"));
|
||||
return account;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user