From 0ff439f776ed066ba69c141c2e310a84283dfdc3 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Thu, 7 Dec 2023 17:26:30 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E9=A1=B9=E7=9B=AE=E7=AE=A1=E7=90=86):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../project/service/EnvironmentService.java | 2 +- .../project/service/FileMetadataService.java | 31 +++++++++++----- .../project/service/FileModuleService.java | 1 + .../project/utils/FileDownloadUtils.java | 36 ++++++++++++++++++- .../FileManagementControllerTests.java | 25 +++++++++++++ 5 files changed, 85 insertions(+), 10 deletions(-) diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/EnvironmentService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/EnvironmentService.java index 432386633b..148bfaf81f 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/EnvironmentService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/EnvironmentService.java @@ -227,7 +227,7 @@ public class EnvironmentService { } } } - return FileDownloadUtils.listBytesToZip(files); + return FileDownloadUtils.listFileBytesToZip(files); } public List exportEnv(EnvironmentExportRequest environmentExportRequest) { 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 85197aa2d8..50a910ddb8 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 @@ -44,7 +44,11 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.util.unit.DataSize; import org.springframework.web.multipart.MultipartFile; -import java.util.*; +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.concurrent.atomic.AtomicLong; import java.util.function.Function; import java.util.stream.Collectors; @@ -235,7 +239,7 @@ public class FileMetadataService { return fileMetadata.getId(); } - private String genTransferFileName(String fullFileName, String projectId) { + public String genTransferFileName(String fullFileName, String projectId) { if (StringUtils.containsAny(fullFileName, "/")) { throw new MSException(Translator.get("file.name.error")); } @@ -293,6 +297,20 @@ public class FileMetadataService { return fileService.upload(file, uploadFileRequest); } + public File getTmpFile(FileMetadata fileMetadata) { + File file = null; + if (TempFileUtils.isImgTmpFileExists(fileMetadata.getId())) { + file = new File(TempFileUtils.getTmpFilePath(fileMetadata.getId())); + } else { + try { + String filePath = TempFileUtils.createFile(TempFileUtils.getTmpFilePath(fileMetadata.getId()), this.getFile(fileMetadata)); + file = new File(filePath); + } catch (Exception ignore) { + } + } + return file; + } + public byte[] getFileByte(FileMetadata fileMetadata) { String filePath = null; if (TempFileUtils.isImgTmpFileExists(fileMetadata.getId())) { @@ -400,12 +418,9 @@ public class FileMetadataService { } public byte[] batchDownload(List fileMetadataList) { - Map files = new LinkedHashMap<>(); - fileMetadataList.forEach(fileMetadata -> { - byte[] bytes = this.getFileByte(fileMetadata); - files.put(this.getFileName(fileMetadata.getName(), fileMetadata.getType()), bytes); - }); - return FileDownloadUtils.listBytesToZip(files); + Map fileMap = new HashMap<>(); + fileMetadataList.forEach(fileMetadata -> fileMap.put(this.getFileName(fileMetadata.getName(), fileMetadata.getType()), this.getTmpFile(fileMetadata))); + return FileDownloadUtils.listBytesToZip(fileMap); } //检查下载的文件的大小 diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/FileModuleService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/FileModuleService.java index 72824d08af..01e4625bb2 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/FileModuleService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/FileModuleService.java @@ -73,6 +73,7 @@ public class FileModuleService extends ModuleTreeService implements CleanupProje fileModule.setCreateUser(operator); fileModule.setUpdateUser(operator); fileModule.setModuleType(ModuleConstants.NODE_TYPE_DEFAULT); + fileModuleMapper.insert(fileModule); //记录日志 diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/utils/FileDownloadUtils.java b/backend/services/project-management/src/main/java/io/metersphere/project/utils/FileDownloadUtils.java index a07f46addc..9e097b5432 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/utils/FileDownloadUtils.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/utils/FileDownloadUtils.java @@ -1,12 +1,46 @@ package io.metersphere.project.utils; +import io.metersphere.sdk.util.LogUtils; + +import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class FileDownloadUtils { - public static byte[] listBytesToZip(Map mapReport) { + + public static byte[] listBytesToZip(Map fileMap) { + try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream)) { + for (Map.Entry fileEntry : fileMap.entrySet()) { + String fileName = fileEntry.getKey(); + File file = fileEntry.getValue(); + if (file.exists()) { + try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) { + ZipEntry zipEntry = new ZipEntry(fileName); + zipOutputStream.putNextEntry(zipEntry); + byte[] buffer = new byte[1024]; + int num; + while ((num = bis.read(buffer)) > 0) { + zipOutputStream.write(buffer, 0, num); + } + zipOutputStream.closeEntry(); + } catch (Exception ignore) { + } + } + } + zipOutputStream.close(); + return byteArrayOutputStream.toByteArray(); + } catch (Exception e) { + LogUtils.error(e); + return new byte[0]; + } + } + + public static byte[] listFileBytesToZip(Map mapReport) { try { if (!mapReport.isEmpty()) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); 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 c13d771c19..1b6ee71d25 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 @@ -11,6 +11,7 @@ import io.metersphere.project.mapper.FileAssociationMapper; import io.metersphere.project.mapper.FileMetadataMapper; import io.metersphere.project.mapper.FileModuleMapper; import io.metersphere.project.service.FileAssociationService; +import io.metersphere.project.service.FileMetadataService; import io.metersphere.project.service.FileModuleService; import io.metersphere.project.service.FileService; import io.metersphere.project.utils.FileManagementBaseUtils; @@ -90,6 +91,8 @@ public class FileManagementControllerTests extends BaseTest { @Resource private FileMetadataMapper fileMetadataMapper; @Resource + private FileMetadataService fileMetadataService; + @Resource private CommonProjectService commonProjectService; @BeforeEach @@ -860,6 +863,11 @@ public class FileManagementControllerTests extends BaseTest { fileBytes = mvcResult.getResponse().getContentAsByteArray(); Assertions.assertTrue(fileBytes.length > 0); + //重新下载全部文件 + mvcResult = this.requestPostDownloadFile(FileManagementRequestUtils.URL_FILE_BATCH_DOWNLOAD, null, batchProcessDTO); + fileBytes = mvcResult.getResponse().getContentAsByteArray(); + Assertions.assertTrue(fileBytes.length > 0); + //全部文件大小超过默认配置(600M)的限制 事先存储20个大小为50M的数据,过后删除 for (int i = 0; i < 20; i++) { String id = "test_" + i; @@ -1632,6 +1640,23 @@ public class FileManagementControllerTests extends BaseTest { error = true; } Assertions.assertTrue(error); + + //文件名称非法 + error = false; + try { + fileMetadataService.transferFile("", null, null, null, null); + } catch (Exception e) { + error = true; + } + Assertions.assertTrue(error); + + error = false; + try { + fileMetadataService.genTransferFileName("testTransfer/File.jpg", null); + } catch (Exception e) { + error = true; + } + Assertions.assertTrue(error); } public void associationDelete() {