refactor(项目设置): 图片压缩方法增加锁保护

This commit is contained in:
song-tianyang 2023-12-20 10:45:49 +08:00 committed by 建国
parent 7f2b0ca14a
commit 7efe629986
3 changed files with 8 additions and 3 deletions

View File

@ -63,7 +63,6 @@ public class TempFileUtils {
g2d.drawImage(originalImage, 0, 0, previewWidth, previewHeight, null);
g2d.dispose();
ImageIO.setUseCache(false);
ImageIO.write(previewImage, "JPEG", outputStream);
return outputStream.toByteArray();
} catch (Exception e) {

View File

@ -200,8 +200,7 @@ public class FileManagementService {
if (previewImg == null || previewImg.length == 0) {
try {
byte[] fileBytes = this.getFile(fileMetadata);
previewImg = TempFileUtils.compressPic(fileBytes);
previewImg = this.compressPicWithFileMetadata(fileMetadata);
fileService.upload(previewImg, previewRequest);
return previewImg;
} catch (Exception e) {
@ -210,4 +209,10 @@ public class FileManagementService {
}
return previewImg;
}
//获取文件并压缩的方法需要上锁防止并发超过一定数量时内存溢出
private synchronized byte[] compressPicWithFileMetadata(FileMetadata fileMetadata) throws Exception {
byte[] fileBytes = this.getFile(fileMetadata);
return TempFileUtils.compressPic(fileBytes);
}
}

View File

@ -287,6 +287,7 @@ public class FileMetadataService {
String filePath = fileService.upload(file, uploadFileRequest);
if (TempFileUtils.isImage(fileMetadata.getType())) {
//图片文件自动生成预览图
byte[] previewImg = TempFileUtils.compressPic(file.getBytes());
uploadFileRequest.setFolder(DefaultRepositoryDir.getFileManagementPreviewDir(fileMetadata.getProjectId()));
fileService.upload(previewImg, uploadFileRequest);