LongTextHandler.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package com.github.jfcloud.gene.handler;
  2. import cn.hutool.core.io.IoUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import cn.hutool.crypto.digest.DigestUtil;
  5. import com.github.jfcloud.common.core.util.SpringContextHolder;
  6. import com.github.jfcloud.gene.file.service.FileInfoService;
  7. import com.github.jfcloud.gene.file.vo.FileVo;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.apache.ibatis.type.JdbcType;
  10. import org.apache.ibatis.type.StringTypeHandler;
  11. import java.io.ByteArrayInputStream;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.nio.charset.StandardCharsets;
  15. import java.sql.CallableStatement;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.sql.SQLException;
  19. /**
  20. * 自定义长文本类型处理器,将长文本存储在minIO上
  21. */
  22. @Slf4j
  23. public class LongTextHandler extends StringTypeHandler {
  24. /**
  25. * 长文本字段前缀
  26. */
  27. public static final String LONG_TEXT_FIELD_PREFIX = "@SampleLongText@";
  28. public static final String FILE_PREFIX = "SampleLongText_Md5_";
  29. /**
  30. * 长文本字段最大长度,过短的文本没必要使用minIO
  31. */
  32. public static final int MAX_TEXT_LENGTH = 200;
  33. @Override
  34. public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
  35. ps.setString(i, saveLongText(parameter));
  36. }
  37. @Override
  38. public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
  39. String textKey = super.getNullableResult(rs, columnName);
  40. return getLongText(textKey);
  41. }
  42. @Override
  43. public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
  44. String textKey = super.getNullableResult(rs, columnIndex);
  45. return getLongText(textKey);
  46. }
  47. @Override
  48. public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
  49. String textKey = super.getNullableResult(cs, columnIndex);
  50. return getLongText(textKey);
  51. }
  52. /**
  53. * 保存长文本
  54. *
  55. * @param text 长文本字符串
  56. * @return 长文本key
  57. */
  58. private String saveLongText(String text) {
  59. if (StrUtil.isBlank(text) || text.length() <= MAX_TEXT_LENGTH) {
  60. return text;
  61. }
  62. try (ByteArrayInputStream bytesArrayInputStream = new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8))) {
  63. FileInfoService fileService = SpringContextHolder.getBean(FileInfoService.class);
  64. FileVo fileVo = fileService.uploadFileWithFileName(bytesArrayInputStream,
  65. FILE_PREFIX + DigestUtil.md5Hex(text) + ".txt");
  66. return LONG_TEXT_FIELD_PREFIX + fileVo.getBucketUrl();
  67. } catch (IOException e) {
  68. log.error("保存长文本失败", e);
  69. }
  70. return "";
  71. }
  72. /**
  73. * 获取长文本
  74. *
  75. * @param textKey 长文本key
  76. * @return 长文本字符串
  77. */
  78. public String getLongText(String textKey) {
  79. if (StrUtil.isBlank(textKey) || !textKey.startsWith(LONG_TEXT_FIELD_PREFIX)) {
  80. return textKey;
  81. }
  82. FileInfoService fileService = SpringContextHolder.getBean(FileInfoService.class);
  83. InputStream inputStream = fileService.getStreamByUrl(textKey.substring(LONG_TEXT_FIELD_PREFIX.length() + 1));
  84. if (inputStream == null) {
  85. log.warn("获取长文本失败,文件不存在:{}", textKey);
  86. return null;
  87. }
  88. return IoUtil.read(inputStream, StandardCharsets.UTF_8);
  89. }
  90. }