From e584b70676da327b4adc6e2b0a961afc6c6d32b0 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Tue, 19 Dec 2023 16:23:45 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E9=A1=B9=E7=9B=AE=E7=AE=A1=E7=90=86):?= =?UTF-8?q?=20=E6=96=87=E4=BB=B6=E9=A2=84=E8=A7=88=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metersphere/sdk/util/TempFileUtils.java | 56 +++++++------------ .../service/FileManagementService.java | 9 ++- .../project/service/FileMetadataService.java | 8 +-- .../FileManagementControllerTests.java | 6 ++ 4 files changed, 31 insertions(+), 48 deletions(-) diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/TempFileUtils.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/TempFileUtils.java index 3db2b474e1..3af27774b0 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/TempFileUtils.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/TempFileUtils.java @@ -26,12 +26,6 @@ public class TempFileUtils { } } - - //获取缩略图路径 - public static String getPreviewImgFilePath(String fileId) { - return TEMP_FILE_FOLDER + "preview/" + fileId + ".jpg"; - } - //获取临时文件路径 public static String getTmpFilePath(String fileId) { return TEMP_FILE_FOLDER + "tmp/" + fileId; @@ -39,48 +33,44 @@ public class TempFileUtils { public static void deleteTmpFile(String fileId) { try { - File file = new File(getPreviewImgFilePath(fileId)); - FileUtils.forceDelete(file); - file = new File(getTmpFilePath(fileId)); + File file = new File(getTmpFilePath(fileId)); FileUtils.forceDelete(file); } catch (Exception ignore) { } } - public static void compressPic(byte[] fileBytes, String compressPicAbsolutePath) throws Exception { + public static byte[] compressPic(byte[] fileBytes) throws IOException { // 读取原始图像 BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(fileBytes)); - if (originalImage == null) { - //如果将一个不是图片的文件强行转换为BufferedImage对象,那么得到的就是null - createFile(compressPicAbsolutePath, fileBytes); - } else { - File file = new File(compressPicAbsolutePath); - File dir = file.getParentFile(); - if (!dir.exists()) { - dir.mkdirs(); - } - + if (originalImage != null) { int width = originalImage.getWidth(); int height = originalImage.getHeight(); - + //计算压缩系数 int compressFactor = getCompressFactor(width, height); // 指定预览图像的宽度和高度 int previewWidth = width / compressFactor; int previewHeight = height / compressFactor; - // 创建一个缩小后的图像 - BufferedImage previewImage = new BufferedImage(previewWidth, previewHeight, BufferedImage.TYPE_INT_RGB); - Graphics2D g2d = previewImage.createGraphics(); - // 绘制缩小后的图像 - g2d.drawImage(originalImage, 0, 0, previewWidth, previewHeight, null); - g2d.dispose(); - ImageIO.setUseCache(false); - // 保存预览图像到文件 - ImageIO.write(previewImage, "JPEG", new File(compressPicAbsolutePath)); + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { + // 创建一个缩小后的图像 + BufferedImage previewImage = new BufferedImage(previewWidth, previewHeight, BufferedImage.TYPE_INT_RGB); + Graphics2D g2d = previewImage.createGraphics(); + + // 绘制缩小后的图像 + g2d.drawImage(originalImage, 0, 0, previewWidth, previewHeight, null); + g2d.dispose(); + ImageIO.setUseCache(false); + + ImageIO.write(previewImage, "JPEG", outputStream); + return outputStream.toByteArray(); + } catch (Exception e) { + LogUtils.error(e); + } } + return fileBytes; } private static int getCompressFactor(int width, int height) { @@ -122,12 +112,6 @@ public class TempFileUtils { return filePath; } - //图片缩略图是否存在 - public static boolean isImgPreviewFileExists(String fileId) { - File file = new File(getPreviewImgFilePath(fileId)); - return file.exists(); - } - //图片原图是否存在 public static boolean isImgTmpFileExists(String fileId) { File file = new File(getTmpFilePath(fileId)); diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/FileManagementService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/FileManagementService.java index a04223fbee..f865ba6c60 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/FileManagementService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/FileManagementService.java @@ -78,14 +78,14 @@ public class FileManagementService { fileRequest.setFileName(fileMetadata.getId()); fileRequest.setStorage(fileMetadata.getStorage()); fileRequest.setFolder(DefaultRepositoryDir.getFileManagementDir(fileMetadata.getProjectId())); + //删除临时文件 + TempFileUtils.deleteTmpFile(fileMetadata.getId()); try { //删除存储容器中的文件 fileService.deleteFile(fileRequest); //删除缓存文件 fileRequest.setFolder(DefaultRepositoryDir.getFileManagementPreviewDir(fileMetadata.getProjectId())); fileService.deleteFile(fileRequest); - //删除临时文件 - TempFileUtils.deleteTmpFile(fileMetadata.getId()); } catch (Exception e) { LogUtils.error("删除文件失败", e); } @@ -201,14 +201,13 @@ public class FileManagementService { if (previewImg == null || previewImg.length == 0) { try { byte[] fileBytes = this.getFile(fileMetadata); - TempFileUtils.compressPic(fileBytes, TempFileUtils.getPreviewImgFilePath(fileMetadata.getId())); - previewImg = TempFileUtils.getFile(TempFileUtils.getPreviewImgFilePath(fileMetadata.getId())); + previewImg = TempFileUtils.compressPic(fileBytes); fileService.upload(previewImg, previewRequest); return previewImg; } catch (Exception e) { LogUtils.error("获取预览图失败", e); } } - return new byte[0]; + return previewImg; } } diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/FileMetadataService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/FileMetadataService.java index 069327fd36..17510ebdac 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/FileMetadataService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/FileMetadataService.java @@ -287,8 +287,7 @@ public class FileMetadataService { String filePath = fileService.upload(file, uploadFileRequest); if (TempFileUtils.isImage(fileMetadata.getType())) { - TempFileUtils.compressPic(file.getBytes(), TempFileUtils.getPreviewImgFilePath(fileMetadata.getId())); - byte[] previewImg = TempFileUtils.getFile(TempFileUtils.getPreviewImgFilePath(fileMetadata.getId())); + byte[] previewImg = TempFileUtils.compressPic(file.getBytes()); uploadFileRequest.setFolder(DefaultRepositoryDir.getFileManagementPreviewDir(fileMetadata.getProjectId())); fileService.upload(previewImg, uploadFileRequest); } @@ -478,17 +477,12 @@ public class FileMetadataService { //svg图片不压缩 contentType = MediaType.parseMediaType("image/svg+xml"); bytes = this.getFileByte(fileMetadata); - } else if (TempFileUtils.isImgPreviewFileExists(fileMetadata.getId())) { - //获取压缩过的图片 - bytes = TempFileUtils.getFile(TempFileUtils.getPreviewImgFilePath(fileMetadata.getId())); } else { /** * 从minio中获取临时文件 * 如果minio不存在,压缩后上传到minio中,并缓存到文件目录中 */ - //压缩图片并保存在临时文件夹中 bytes = fileManagementService.getPreviewImg(fileMetadata); - TempFileUtils.createFile(TempFileUtils.getPreviewImgFilePath(fileMetadata.getId()), bytes); } } diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileManagementControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileManagementControllerTests.java index 269b719ce2..7d52f7af85 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileManagementControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileManagementControllerTests.java @@ -905,6 +905,12 @@ public class FileManagementControllerTests extends BaseTest { fileBytes = mvcResult.getResponse().getContentAsByteArray(); Assertions.assertTrue(fileBytes.length > 0); + //删除存储的临时文件,再重新下载 + FILE_ID_PATH.forEach((k, v) -> TempFileUtils.deleteTmpFile(k)); + mvcResult = this.requestPostDownloadFile(FileManagementRequestUtils.URL_FILE_BATCH_DOWNLOAD, zipMediaType, batchProcessDTO); + fileBytes = mvcResult.getResponse().getContentAsByteArray(); + Assertions.assertTrue(fileBytes.length > 0); + //全部文件大小超过默认配置(600M)的限制 事先存储20个大小为50M的数据,过后删除 for (int i = 0; i < 20; i++) { String id = "test_" + i;