From cc4d4ffa5b24226d5e52521d633950acefc9faef Mon Sep 17 00:00:00 2001 From: CaptainB Date: Tue, 7 Jun 2022 19:24:55 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E4=BF=9D=E5=AD=98=E6=B5=8B=E8=AF=95=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E6=97=B6=E4=BD=BF=E7=94=A8zip=E5=8E=8B?= =?UTF-8?q?=E7=BC=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/utils/CompressUtils.java | 31 +++++++++++++++++++ .../io/metersphere/config/DatabaseConfig.java | 6 ++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/backend/src/main/java/io/metersphere/commons/utils/CompressUtils.java b/backend/src/main/java/io/metersphere/commons/utils/CompressUtils.java index 9bda4f6fff..749d8d8ebe 100644 --- a/backend/src/main/java/io/metersphere/commons/utils/CompressUtils.java +++ b/backend/src/main/java/io/metersphere/commons/utils/CompressUtils.java @@ -1,6 +1,9 @@ package io.metersphere.commons.utils; +import org.apache.commons.codec.binary.Base64; + import java.io.*; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.zip.*; @@ -229,4 +232,32 @@ public class CompressUtils { return data; } } + + public static Object zipString(Object data) { + if (!(data instanceof String)) { + return data; + } + try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { + try (DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(out)) { + deflaterOutputStream.write(((String) data).getBytes(StandardCharsets.UTF_8)); + } + return Base64.encodeBase64String(out.toByteArray()); + } catch (Exception e) { + return data; + } + } + + public static Object unzipString(Object data) { + if (!(data instanceof String)) { + return data; + } + try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { + try (OutputStream outputStream = new InflaterOutputStream(os)) { + outputStream.write(Base64.decodeBase64((String) data)); + } + return os.toString(StandardCharsets.UTF_8); + } catch (Exception e) { + return data; + } + } } diff --git a/backend/src/main/java/io/metersphere/config/DatabaseConfig.java b/backend/src/main/java/io/metersphere/config/DatabaseConfig.java index 77c5290017..903dfd16f4 100644 --- a/backend/src/main/java/io/metersphere/config/DatabaseConfig.java +++ b/backend/src/main/java/io/metersphere/config/DatabaseConfig.java @@ -3,10 +3,7 @@ package io.metersphere.config; import com.fit2cloud.quartz.anno.QuartzDataSource; import com.github.pagehelper.PageInterceptor; import com.zaxxer.hikari.HikariDataSource; -import io.metersphere.base.domain.ApiTestReportDetail; -import io.metersphere.base.domain.AuthSource; -import io.metersphere.base.domain.FileContent; -import io.metersphere.base.domain.TestResource; +import io.metersphere.base.domain.*; import io.metersphere.commons.utils.CompressUtils; import io.metersphere.commons.utils.MybatisInterceptorConfig; import io.metersphere.interceptor.MybatisInterceptor; @@ -54,6 +51,7 @@ public class DatabaseConfig { configList.add(new MybatisInterceptorConfig(ApiTestReportDetail.class, "content", CompressUtils.class, "compress", "decompress")); configList.add(new MybatisInterceptorConfig(TestResource.class, "configuration")); configList.add(new MybatisInterceptorConfig(AuthSource.class, "configuration")); + configList.add(new MybatisInterceptorConfig(LoadTestReportLog.class, "content", CompressUtils.class, "zipString", "unzipString")); interceptor.setInterceptorConfigList(configList); return interceptor; }