|
@@ -0,0 +1,99 @@
|
|
|
+package com.github.jfcloud.gene.handler;
|
|
|
+
|
|
|
+import cn.hutool.core.io.IoUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.crypto.digest.DigestUtil;
|
|
|
+import com.github.jfcloud.common.core.util.SpringContextHolder;
|
|
|
+import com.github.jfcloud.gene.file.service.FileInfoService;
|
|
|
+import com.github.jfcloud.gene.file.vo.FileVo;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.ibatis.type.JdbcType;
|
|
|
+import org.apache.ibatis.type.StringTypeHandler;
|
|
|
+
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.sql.CallableStatement;
|
|
|
+import java.sql.PreparedStatement;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.SQLException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 自定义长文本类型处理器,将长文本存储在minIO上
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public class LongTextHandler extends StringTypeHandler {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 长文本字段前缀
|
|
|
+ */
|
|
|
+ public static final String LONG_TEXT_FIELD_PREFIX = "@SampleLongText@";
|
|
|
+ public static final String FILE_PREFIX = "SampleLongText_Md5_";
|
|
|
+ /**
|
|
|
+ * 长文本字段最大长度,过短的文本没必要使用minIO
|
|
|
+ */
|
|
|
+ public static final int MAX_TEXT_LENGTH = 200;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
|
|
|
+ ps.setString(i, saveLongText(parameter));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
|
|
+ String textKey = super.getNullableResult(rs, columnName);
|
|
|
+ return getLongText(textKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
|
|
+ String textKey = super.getNullableResult(rs, columnIndex);
|
|
|
+ return getLongText(textKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
|
|
+ String textKey = super.getNullableResult(cs, columnIndex);
|
|
|
+ return getLongText(textKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存长文本
|
|
|
+ *
|
|
|
+ * @param text 长文本字符串
|
|
|
+ * @return 长文本key
|
|
|
+ */
|
|
|
+ private String saveLongText(String text) {
|
|
|
+ if (StrUtil.isBlank(text) || text.length() <= MAX_TEXT_LENGTH) {
|
|
|
+ return text;
|
|
|
+ }
|
|
|
+
|
|
|
+ try (ByteArrayInputStream bytesArrayInputStream = new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8))) {
|
|
|
+
|
|
|
+ FileInfoService fileService = SpringContextHolder.getBean(FileInfoService.class);
|
|
|
+ FileVo fileVo = fileService.uploadFileWithFileName(bytesArrayInputStream,
|
|
|
+ FILE_PREFIX + DigestUtil.md5Hex(text) + ".txt");
|
|
|
+ return LONG_TEXT_FIELD_PREFIX + fileVo.getBucketUrl();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("保存长文本失败", e);
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取长文本
|
|
|
+ *
|
|
|
+ * @param textKey 长文本key
|
|
|
+ * @return 长文本字符串
|
|
|
+ */
|
|
|
+ public String getLongText(String textKey) {
|
|
|
+ if (StrUtil.isBlank(textKey) || !textKey.startsWith(LONG_TEXT_FIELD_PREFIX)) {
|
|
|
+ return textKey;
|
|
|
+ }
|
|
|
+
|
|
|
+ FileInfoService fileService = SpringContextHolder.getBean(FileInfoService.class);
|
|
|
+ InputStream inputStream = fileService.getStreamByUrl(textKey.substring(LONG_TEXT_FIELD_PREFIX.length() + 1));
|
|
|
+ return IoUtil.read(inputStream, StandardCharsets.UTF_8);
|
|
|
+ }
|
|
|
+}
|