From 6f26154738fab03024fff4663566d9aa10ff189d Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Tue, 31 Oct 2023 11:22:53 +0800 Subject: [PATCH] =?UTF-8?q?perf(=E9=A1=B9=E7=9B=AE=E8=AE=BE=E7=BD=AE):=20?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=8E=8B=E7=BC=A9=E6=96=B9=E5=BC=8F=E4=BB=8E?= =?UTF-8?q?=E4=B9=8B=E5=89=8D=E7=9A=84=E5=8E=8B=E7=BC=A9=E4=B8=80=E7=99=BE?= =?UTF-8?q?=E5=80=8D=E6=94=B9=E4=B8=BA=E5=8A=A8=E6=80=81=E5=8E=8B=E7=BC=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metersphere/sdk/util/TempFileUtils.java | 20 ++++++++++++++++-- .../project/service/FileMetadataService.java | 4 ++-- .../FileManagementControllerTests.java | 12 +++++++++++ .../test/resources/file/1182937072541700.jpg | Bin 0 -> 3609 bytes 4 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 backend/services/project-management/src/test/resources/file/1182937072541700.jpg 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 733501e265..a3b7b4f377 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 @@ -52,9 +52,15 @@ public class TempFileUtils { dir.mkdirs(); } + int width = originalImage.getWidth(); + int height = originalImage.getHeight(); + + //计算压缩系数 + int compressFactor = getCompressFactor(width, height); + // 指定预览图像的宽度和高度 - int previewWidth = originalImage.getWidth() / 10; - int previewHeight = originalImage.getHeight() / 10; + int previewWidth = width / compressFactor; + int previewHeight = height / compressFactor; // 创建一个缩小后的图像 BufferedImage previewImage = new BufferedImage(previewWidth, previewHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = previewImage.createGraphics(); @@ -68,6 +74,16 @@ public class TempFileUtils { } } + private static int getCompressFactor(int width, int height) { + int compressFactor = 1; + + int maxSize = width > height ? width : height; + if (maxSize > 999) { + compressFactor = maxSize / 999; + } + return compressFactor; + } + private static void createFile(String filePath, byte[] fileBytes) { File file = new File(filePath); 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 8d6c436eb5..b58bd77430 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 @@ -178,7 +178,7 @@ public class FileMetadataService { byte[] bytes = this.getFile(fileMetadata); return ResponseEntity.ok() .contentType(MediaType.parseMediaType("application/octet-stream")) - .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + this.getFileName(fileMetadata.getName(), fileMetadata.getType()) + "\"") + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + this.getFileName(fileMetadata.getId(), fileMetadata.getType()) + "\"") .body(bytes); } @@ -372,7 +372,7 @@ public class FileMetadataService { return ResponseEntity.ok() .contentType(MediaType.parseMediaType("application/octet-stream")) - .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + this.getFileName(fileMetadata.getName(), fileMetadata.getType()) + "\"") + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + this.getFileName(fileMetadata.getId(), fileMetadata.getType()) + "\"") .body(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 80fe4697df..fd17e99d36 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 @@ -415,6 +415,18 @@ public class FileManagementControllerTests extends BaseTest { jarFileId = returnId; uploadedFileTypes.add("jar"); + + //小型图片文件,用于测试预览图下载 + filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/1182937072541700.jpg")).getPath(); + file = new MockMultipartFile("file", "1182937072541700.jpg", MediaType.APPLICATION_OCTET_STREAM_VALUE, FileManagementBaseUtils.getFileBytes(filePath)); + paramMap = new LinkedMultiValueMap<>(); + paramMap.add("file", file); + paramMap.add("request", JSON.toJSONString(fileUploadRequest)); + mvcResult = this.requestMultipartWithOkAndReturn(FileManagementRequestUtils.URL_FILE_UPLOAD, paramMap); + returnId = JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData().toString(); + checkLog(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD); + FILE_ID_PATH.put(returnId, filePath); + //检查文件类型获取接口有没有获取到数据 fileTypes = this.getFileType(); Assertions.assertEquals(fileTypes.size(), uploadedFileTypes.size()); diff --git a/backend/services/project-management/src/test/resources/file/1182937072541700.jpg b/backend/services/project-management/src/test/resources/file/1182937072541700.jpg new file mode 100644 index 0000000000000000000000000000000000000000..7ae44d0f319118056e1fb9c29fb6765d0515fc26 GIT binary patch literal 3609 zcmbW!c{tSly9e;^7>u2YkzT>>R>>z(h{T%E>DzDrsn3>YhK3 z@>dm=^$m^qre?z1cU|2*y~I9J|G+4DYhtvC(lTX*y1KSb+u7aQKlpZd^!>*_ zE)W3zH|zKPZ`l8EL4UiLn3=)Mkbhhtrl{Ws3}t4KLbCE3*+4u(1fvF(G;oL0^9)4-q>$HOi=`JDd9GBnHySH_=O&j6f@aFK7J zR|jp9d7;PGYX`zPH z)`?9t;j`?AL^nj))4PtK6s`;&ktgXbeITQ%lT-D3a%sX(66bca{O!$Ve_(kp@Va(z zi8f6vHN=ReCm%Qs74uJ92sK#wK2%dBj?S_etfpHoY`Q1MWcr5Sm%lf2&qc;Z!`GeX z}KD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