diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/BaseDisplayController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/BaseDisplayController.java new file mode 100644 index 0000000000..8b358ca49b --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/BaseDisplayController.java @@ -0,0 +1,47 @@ +package io.metersphere.system.controller; + +import io.metersphere.system.service.BaseDisplayService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.Resource; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.io.IOException; + +@Tag(name = "首页图片") +@RestController +@RequestMapping(value = "/base-display") +public class BaseDisplayController { + + @Resource + private BaseDisplayService displayService; + + + @Operation(summary = "获取icon图片") + @GetMapping("/get/icon") + public ResponseEntity getIcon() throws IOException { + return displayService.getFile("icon"); + } + + + @Operation(summary = "获取loginImage图片") + @GetMapping("/get/login-image") + public ResponseEntity getLoginImage() throws IOException { + return displayService.getFile("loginImage"); + } + + @Operation(summary = "获取loginLogo图片") + @GetMapping("/get/login-logo") + public ResponseEntity getLoginLogo() throws IOException { + return displayService.getFile("loginLogo"); + } + + @Operation(summary = "获取logoPlatform图片") + @GetMapping("/get/logo-platform") + public ResponseEntity getLogoPlatform() throws IOException { + return displayService.getFile("logoPlatform"); + } +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseDisplayService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseDisplayService.java new file mode 100644 index 0000000000..f6c9690cab --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseDisplayService.java @@ -0,0 +1,71 @@ +package io.metersphere.system.service; + +import io.metersphere.sdk.exception.MSException; +import io.metersphere.sdk.file.FileRequest; +import io.metersphere.sdk.file.MinioRepository; +import io.metersphere.sdk.log.constants.OperationLogModule; +import io.metersphere.system.domain.SystemParameter; +import io.metersphere.system.mapper.SystemParameterMapper; +import jakarta.annotation.Resource; +import org.apache.commons.io.IOUtils; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.io.IOException; + +@Service +@Transactional(rollbackFor = Exception.class) +public class BaseDisplayService { + + @Resource + private MinioRepository repository; + @Resource + private SystemParameterMapper systemParameterMapper; + + + public ResponseEntity getFile(String fileName) throws IOException { + byte[] bytes = null; + SystemParameter systemParameter = systemParameterMapper.selectByPrimaryKey("ui." + fileName); + if (systemParameter != null) { + FileRequest request = new FileRequest(); + request.setFileName(systemParameter.getParamValue()); + request.setProjectId("system"); + request.setResourceId(OperationLogModule.SYSTEM_PARAMETER_SETTING); + try { + bytes = repository.getFile(request); + } catch (Exception e) { + throw new MSException("get file error"); + } + } + + MediaType contentType = MediaType.parseMediaType("application/octet-stream"); + if (bytes == null) { + PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(getClass().getClassLoader()); + switch (fileName) { + case "icon": + bytes = IOUtils.toByteArray(resolver.getResource("/static/favicon.ico").getInputStream()); + break; + case "logoPlatform": + bytes = IOUtils.toByteArray(resolver.getResource("/static/svg/MS-full-logo.svg").getInputStream()); + contentType = MediaType.valueOf("image/svg+xml"); + break; + case "loginImage": + bytes = IOUtils.toByteArray(resolver.getResource("/static/images/login-banner.jpg").getInputStream()); + break; + default: + bytes = IOUtils.toByteArray(resolver.getResource("/static/svg/login-logo.svg").getInputStream()); + contentType = MediaType.valueOf("image/svg+xml"); + break; + } + } + return ResponseEntity.ok() + .contentType(contentType) + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"") + .body(bytes); + } + +} diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/BaseDisplayControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/BaseDisplayControllerTests.java new file mode 100644 index 0000000000..39199a4486 --- /dev/null +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/BaseDisplayControllerTests.java @@ -0,0 +1,128 @@ +package io.metersphere.system.controller; + +import io.metersphere.sdk.base.BaseTest; +import io.metersphere.sdk.constants.SessionConstants; +import io.metersphere.sdk.file.FileRequest; +import io.metersphere.sdk.file.MinioRepository; +import io.metersphere.sdk.log.constants.OperationLogModule; +import jakarta.annotation.Resource; +import org.apache.commons.io.IOUtils; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.test.context.jdbc.SqlConfig; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; + +@SpringBootTest +@AutoConfigureMockMvc +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +public class BaseDisplayControllerTests extends BaseTest { + + public static final String DISPLAY_ICON = "/base-display/get/icon"; + public static final String DISPLAY_LOGINIMAGE = "/base-display/get/login-image"; + public static final String DISPLAY_LOGINLOGO = "/base-display/get/login-logo"; + public static final String DISPLAY_LOGOPLATFORM = "/base-display/get/logo-platform"; + + //默认图片 + @Resource + private MinioRepository repository; + + @Test + @Order(1) + public void getIconDefault() throws Exception { + this.requestGet(DISPLAY_ICON); + } + + @Test + @Order(2) + public void getLoginImageDefault() throws Exception { + this.requestGet(DISPLAY_LOGINIMAGE); + } + + @Test + @Order(3) + public void getLoginLogoDefault() throws Exception { + this.requestGet(DISPLAY_LOGINLOGO); + } + + @Test + @Order(4) + public void getLogoPlatformDefault() throws Exception { + this.requestGet(DISPLAY_LOGOPLATFORM); + } + + //文件先放入minio + @Test + @Order(6) + public void saveFile() throws Exception { + + saveImages("/file/favicon.ico","favicon","favicon.ico","ui.icon:favicon.ico"); + saveImages("/file/login-banner.jpg","login-banner","login-banner.jpg","ui.loginImage:login-banner.jpg"); + saveImages("/file/login-logo.svg","login-logo","login-logo.svg","ui.loginLogo:login-logo.svg"); + saveImages("/file/MS-full-logo.svg","MS-full-logo","MS-full-logo.svg","ui.logoPlatform:MS-full-logo.svg"); + + } + + private void saveImages(String url, String name, String originalFileName, String fileName) throws Exception{ + PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(getClass().getClassLoader()); + byte[] bytes = IOUtils.toByteArray(resolver.getResource(url).getInputStream()); + MockMultipartFile mockFile = new MockMultipartFile(name, originalFileName, "application/octet-stream", bytes); + FileRequest request = new FileRequest(); + request.setFileName(fileName); + request.setProjectId("system"); + request.setResourceId(OperationLogModule.SYSTEM_PARAMETER_SETTING); + repository.saveFile(mockFile, request); + } + + + //取minio中的图片 + @Test + @Order(5) + @Sql(scripts = {"/dml/init_display_test.sql"}, + config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED), + executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) + public void getIconError() throws Exception { + this.requestGet(DISPLAY_ICON); + } + + //取minio中的图片 + @Test + @Order(7) + public void getIcon() throws Exception { + this.requestGet(DISPLAY_ICON); + } + + @Test + @Order(7) + public void getLoginImage() throws Exception { + this.requestGet(DISPLAY_LOGINIMAGE); + } + + @Test + @Order(7) + public void getLoginLogo() throws Exception { + this.requestGet(DISPLAY_LOGINLOGO); + } + + @Test + @Order(8) + public void getLogoPlatform() throws Exception { + this.requestGet(DISPLAY_LOGOPLATFORM); + } + + private MvcResult requestGet(String url) throws Exception { + return mockMvc.perform(MockMvcRequestBuilders.get(url) + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken)) + .andDo(print()).andReturn(); + } +} diff --git a/backend/services/system-setting/src/test/resources/dml/init_display_test.sql b/backend/services/system-setting/src/test/resources/dml/init_display_test.sql new file mode 100644 index 0000000000..647eeb328c --- /dev/null +++ b/backend/services/system-setting/src/test/resources/dml/init_display_test.sql @@ -0,0 +1,9 @@ +-- 模拟数据 +INSERT INTO system_parameter(`param_key`, `param_value`, `type`) VALUES ('ui.icon', 'ui.icon:favicon.ico', 'file'); +INSERT INTO system_parameter(`param_key`, `param_value`, `type`) VALUES ('ui.logoPlatform', 'ui.logoPlatform:MS-full-logo.svg', 'file'); +INSERT INTO system_parameter(`param_key`, `param_value`, `type`) VALUES ('ui.loginImage', 'ui.loginImage:login-banner.jpg', 'file'); +INSERT INTO system_parameter(`param_key`, `param_value`, `type`) VALUES ('ui.loginLogo', 'ui.loginLogo:login-logo.svg', 'file'); + + + + diff --git a/backend/services/system-setting/src/test/resources/dml/init_org_project.sql b/backend/services/system-setting/src/test/resources/dml/init_org_project.sql index c4d57320c3..be2ce31fb0 100644 --- a/backend/services/system-setting/src/test/resources/dml/init_org_project.sql +++ b/backend/services/system-setting/src/test/resources/dml/init_org_project.sql @@ -10,4 +10,11 @@ VALUES ('admin1', 'test1', 'admin1@metersphere.io', MD5('admin1@metersphere.io') replace into user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source, last_project_id, create_user, update_user) VALUES ('admin2', 'test2', 'admin2@metersphere.io', MD5('admin2@metersphere.io'), - UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin'); \ No newline at end of file + UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin'); + +replace INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +replace INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId1', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目1', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +replace INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId2', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目2', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +replace INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId3', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目3', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +replace INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId4', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目4', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +replace INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId5', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目5', '系统默认创建的项目', 'test', 'test', unix_timestamp() * 1000, unix_timestamp() * 1000); \ No newline at end of file diff --git a/backend/services/system-setting/src/test/resources/dml/init_project.sql b/backend/services/system-setting/src/test/resources/dml/init_project.sql index 6d691bad1e..b113486407 100644 --- a/backend/services/system-setting/src/test/resources/dml/init_project.sql +++ b/backend/services/system-setting/src/test/resources/dml/init_project.sql @@ -1,20 +1,20 @@ # 插入测试数据 -INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); -INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); -INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId1', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目1', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); -INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId2', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目2', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); -INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId3', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目3', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); -INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId4', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目4', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); -INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId5', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目5', '系统默认创建的项目', 'test', 'test', unix_timestamp() * 1000, unix_timestamp() * 1000); +replace INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +replace INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +replace INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId1', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目1', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +replace INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId2', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目2', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +replace INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId3', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目3', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +replace INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId4', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目4', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +replace INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId5', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目5', '系统默认创建的项目', 'test', 'test', unix_timestamp() * 1000, unix_timestamp() * 1000); -insert into user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source, +replace into user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source, last_project_id, create_user, update_user) VALUES ('admin1', 'test1', 'admin1@metersphere.io', MD5('admin1@metersphere.io'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', 'projectId1', 'admin', 'admin'); -insert into user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source, +replace into user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source, last_project_id, create_user, update_user) VALUES ('admin2', 'test2', 'admin2@metersphere.io', MD5('admin2@metersphere.io'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin'); -INSERT INTO user_role_relation VALUES ('c3bb9b4f-46d8-4952-9681-8889974487w','admin1','project_admin','projectId1','1684747668375','1684747668375'); -INSERT INTO user_role_relation VALUES ('c3bb9b4f-46d8-4952-9681-8889974487q','admin2','project_admin','projectId1','1684747668321','1684747668336'); \ No newline at end of file +replace INTO user_role_relation VALUES ('c3bb9b4f-46d8-4952-9681-8889974487w','admin1','project_admin','projectId1','1684747668375','1684747668375'); +replace INTO user_role_relation VALUES ('c3bb9b4f-46d8-4952-9681-8889974487q','admin2','project_admin','projectId1','1684747668321','1684747668336'); \ No newline at end of file diff --git a/backend/services/system-setting/src/test/resources/file/MS-full-logo.svg b/backend/services/system-setting/src/test/resources/file/MS-full-logo.svg new file mode 100644 index 0000000000..e8482400db --- /dev/null +++ b/backend/services/system-setting/src/test/resources/file/MS-full-logo.svg @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/backend/services/system-setting/src/test/resources/file/favicon.ico b/backend/services/system-setting/src/test/resources/file/favicon.ico new file mode 100644 index 0000000000..45380f4db1 Binary files /dev/null and b/backend/services/system-setting/src/test/resources/file/favicon.ico differ diff --git a/backend/services/system-setting/src/test/resources/file/login-banner.jpg b/backend/services/system-setting/src/test/resources/file/login-banner.jpg new file mode 100644 index 0000000000..a6211737f3 Binary files /dev/null and b/backend/services/system-setting/src/test/resources/file/login-banner.jpg differ diff --git a/backend/services/system-setting/src/test/resources/file/login-logo.svg b/backend/services/system-setting/src/test/resources/file/login-logo.svg new file mode 100644 index 0000000000..14dc9179c8 --- /dev/null +++ b/backend/services/system-setting/src/test/resources/file/login-logo.svg @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file