perf(项目设置): 图片压缩方式从之前的压缩一百倍改为动态压缩

This commit is contained in:
song-tianyang 2023-10-31 11:22:53 +08:00 committed by f2c-ci-robot[bot]
parent e41e9f6c65
commit 6f26154738
4 changed files with 32 additions and 4 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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());

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB