From 781f09c2aaeb5f383852be69f21f195eba2e60f3 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Thu, 16 Nov 2023 15:44:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):?= =?UTF-8?q?=20=E7=B2=BE=E7=AE=80MinIO=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../project/service/FileMetadataService.java | 21 +++++++++--------- .../EnvironmentControllerTests.java | 18 --------------- .../metersphere/system/file/FileRequest.java | 22 +------------------ .../system/file/MinioRepository.java | 12 +++------- 4 files changed, 15 insertions(+), 58 deletions(-) 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 e1d35d1dee..5bd61303c5 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 @@ -216,9 +216,7 @@ public class FileMetadataService { FileRequest uploadFileRequest = new FileRequest(); uploadFileRequest.setFileName(fileMetadata.getId()); - uploadFileRequest.setMainFolder(FileRequest.MAIN_FOLDER_PROJECT); - uploadFileRequest.setAppName(FileRequest.APP_NAME_FILE_MANAGEMENT); - uploadFileRequest.setSourceGroupFolder(fileMetadata.getProjectId()); + uploadFileRequest.setFolder(this.generateMinIOFilePath(projectId)); uploadFileRequest.setStorage(StorageType.MINIO.name()); FileRepository minio = CommonBeanFactory.getBean(MinioRepository.class); @@ -282,9 +280,7 @@ public class FileMetadataService { private String uploadFile(FileMetadata fileMetadata, MultipartFile file) throws Exception { FileRequest uploadFileRequest = new FileRequest(); uploadFileRequest.setFileName(fileMetadata.getId()); - uploadFileRequest.setSourceGroupFolder(fileMetadata.getProjectId()); - uploadFileRequest.setMainFolder(FileRequest.MAIN_FOLDER_PROJECT); - uploadFileRequest.setAppName(FileRequest.APP_NAME_FILE_MANAGEMENT); + uploadFileRequest.setFolder(this.generateMinIOFilePath(fileMetadata.getProjectId())); uploadFileRequest.setStorage(StorageType.MINIO.name()); return fileService.upload(file, uploadFileRequest); } @@ -324,9 +320,7 @@ public class FileMetadataService { } FileRequest fileRequest = new FileRequest(); fileRequest.setFileName(fileMetadata.getId()); - fileRequest.setSourceGroupFolder(fileMetadata.getProjectId()); - fileRequest.setMainFolder(FileRequest.MAIN_FOLDER_PROJECT); - fileRequest.setAppName(FileRequest.APP_NAME_FILE_MANAGEMENT); + fileRequest.setFolder(this.generateMinIOFilePath(fileMetadata.getProjectId())); fileRequest.setStorage(fileMetadata.getStorage()); //获取git文件下载 @@ -666,4 +660,11 @@ public class FileMetadataService { }); return fileVersionResponseList; } -} + + private static final String MAIN_FOLDER_PROJECT = "project"; + private static final String APP_NAME_FILE_MANAGEMENT = "fileManagement"; + + private String generateMinIOFilePath(String projectId) { + return StringUtils.join(MAIN_FOLDER_PROJECT, "/", projectId, "/", APP_NAME_FILE_MANAGEMENT); + } +} \ No newline at end of file diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/EnvironmentControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/EnvironmentControllerTests.java index 2637f52ea7..0a52d483a0 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/EnvironmentControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/EnvironmentControllerTests.java @@ -649,15 +649,6 @@ public class EnvironmentControllerTests extends BaseTest { Assertions.assertNotNull(response); environment = environmentMapper.selectByPrimaryKey(response.getId()); Assertions.assertEquals(response.getName(), environment.getName()); - //查询文件 - FileRequest fileRequest = new FileRequest(); - fileRequest.setProjectId(StringUtils.join(DIR_PATH, environment.getProjectId())); - fileRequest.setResourceId(environment.getId()); - MinioRepository minioRepository = CommonBeanFactory.getBean(MinioRepository.class); - assert minioRepository != null; - List fileNames = minioRepository.getFolderFileNames(fileRequest); - //校验文件 - Assertions.assertEquals(2, fileNames.size()); //校验权限 request.setProjectId(DEFAULT_PROJECT_ID); @@ -843,15 +834,6 @@ public class EnvironmentControllerTests extends BaseTest { Assertions.assertNotNull(response); environment = environmentMapper.selectByPrimaryKey(response.getId()); Assertions.assertEquals(response.getName(), environment.getName()); - //查询文件 - FileRequest fileRequest = new FileRequest(); - fileRequest.setProjectId(StringUtils.join(DIR_PATH, environment.getProjectId())); - fileRequest.setResourceId(environment.getId()); - MinioRepository minioRepository = CommonBeanFactory.getBean(MinioRepository.class); - assert minioRepository != null; - List fileNames = minioRepository.getFolderFileNames(fileRequest); - //校验文件 - Assertions.assertEquals(2, fileNames.size()); //校验权限 example = new EnvironmentExample(); example.createCriteria().andProjectIdEqualTo(DEFAULT_PROJECT_ID).andNameEqualTo("校验权限"); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/file/FileRequest.java b/backend/services/system-setting/src/main/java/io/metersphere/system/file/FileRequest.java index 82c651122b..2974d7c2e3 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/file/FileRequest.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/file/FileRequest.java @@ -7,28 +7,8 @@ import lombok.Data; @Data public class FileRequest { - public static final String MAIN_FOLDER_SYSTEM = "system"; - public static final String MAIN_FOLDER_PROJECT = "project"; - public static final String MAIN_FOLDER_ORGANIZATION = "organization"; - public static final String APP_NAME_FILE_MANAGEMENT = "fileManagement"; - - //主文件夹 取值:system、project、organization - private String mainFolder; - - /* - 资源所属组目录。 - 当mainFolder为system时,这里可以是plugin(存放插件) - 当mainFolder为project时,这里可以是项目ID - 可为空 - */ - private String sourceGroupFolder; - - /* - 可以为空 - 取值参照:fileManagement、api - */ - private String appName; + private String folder; //项目ID private String projectId; diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/file/MinioRepository.java b/backend/services/system-setting/src/main/java/io/metersphere/system/file/MinioRepository.java index 4783efb095..b525fa5bcc 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/file/MinioRepository.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/file/MinioRepository.java @@ -24,15 +24,9 @@ public class MinioRepository implements FileRepository { private static final int BUFFER_SIZE = 8192; private String getPath(FileRequest request) { - String mainFolder = (StringUtils.isBlank(request.getMainFolder()) ? "default" : request.getMainFolder()) + "/"; - String appName = StringUtils.isBlank(request.getAppName()) ? StringUtils.EMPTY : (request.getAppName() + "/"); - //如果sourceGroupFolder为空, 取projectId - String sourceGroupFolder = StringUtils.isNotBlank(request.getSourceGroupFolder()) ? (request.getSourceGroupFolder() + "/") : - (StringUtils.isNotBlank(request.getProjectId()) ? (request.getProjectId() + "/") : StringUtils.EMPTY); - String resourceId = StringUtils.isBlank(request.getResourceId()) ? StringUtils.EMPTY : (request.getResourceId() + "/"); - - // 文件存储路径 - return StringUtils.join(mainFolder, sourceGroupFolder, appName, resourceId, request.getFileName()); + String folder = StringUtils.isNotEmpty(request.getFolder()) ? request.getFolder() : request.getProjectId(); + //todo 后续要增加对folder起始路径的校验: system / project / organization + return StringUtils.join(folder, "/", request.getFileName()); } @Override