mirror of
https://gitee.com/fit2cloud-feizhiyun/MeterSphere.git
synced 2024-12-02 12:09:13 +08:00
refactor(项目管理): 文件预览功能优化
This commit is contained in:
parent
13a5c7ebbc
commit
e584b70676
@ -26,12 +26,6 @@ public class TempFileUtils {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//获取缩略图路径
|
||||
public static String getPreviewImgFilePath(String fileId) {
|
||||
return TEMP_FILE_FOLDER + "preview/" + fileId + ".jpg";
|
||||
}
|
||||
|
||||
//获取临时文件路径
|
||||
public static String getTmpFilePath(String fileId) {
|
||||
return TEMP_FILE_FOLDER + "tmp/" + fileId;
|
||||
@ -39,48 +33,44 @@ public class TempFileUtils {
|
||||
|
||||
public static void deleteTmpFile(String fileId) {
|
||||
try {
|
||||
File file = new File(getPreviewImgFilePath(fileId));
|
||||
FileUtils.forceDelete(file);
|
||||
file = new File(getTmpFilePath(fileId));
|
||||
File file = new File(getTmpFilePath(fileId));
|
||||
FileUtils.forceDelete(file);
|
||||
} catch (Exception ignore) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static void compressPic(byte[] fileBytes, String compressPicAbsolutePath) throws Exception {
|
||||
public static byte[] compressPic(byte[] fileBytes) throws IOException {
|
||||
// 读取原始图像
|
||||
BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(fileBytes));
|
||||
if (originalImage == null) {
|
||||
//如果将一个不是图片的文件强行转换为BufferedImage对象,那么得到的就是null
|
||||
createFile(compressPicAbsolutePath, fileBytes);
|
||||
} else {
|
||||
File file = new File(compressPicAbsolutePath);
|
||||
File dir = file.getParentFile();
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
|
||||
if (originalImage != null) {
|
||||
int width = originalImage.getWidth();
|
||||
int height = originalImage.getHeight();
|
||||
|
||||
|
||||
//计算压缩系数
|
||||
int compressFactor = getCompressFactor(width, height);
|
||||
|
||||
// 指定预览图像的宽度和高度
|
||||
int previewWidth = width / compressFactor;
|
||||
int previewHeight = height / compressFactor;
|
||||
// 创建一个缩小后的图像
|
||||
BufferedImage previewImage = new BufferedImage(previewWidth, previewHeight, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D g2d = previewImage.createGraphics();
|
||||
|
||||
// 绘制缩小后的图像
|
||||
g2d.drawImage(originalImage, 0, 0, previewWidth, previewHeight, null);
|
||||
g2d.dispose();
|
||||
ImageIO.setUseCache(false);
|
||||
// 保存预览图像到文件
|
||||
ImageIO.write(previewImage, "JPEG", new File(compressPicAbsolutePath));
|
||||
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
||||
// 创建一个缩小后的图像
|
||||
BufferedImage previewImage = new BufferedImage(previewWidth, previewHeight, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D g2d = previewImage.createGraphics();
|
||||
|
||||
// 绘制缩小后的图像
|
||||
g2d.drawImage(originalImage, 0, 0, previewWidth, previewHeight, null);
|
||||
g2d.dispose();
|
||||
ImageIO.setUseCache(false);
|
||||
|
||||
ImageIO.write(previewImage, "JPEG", outputStream);
|
||||
return outputStream.toByteArray();
|
||||
} catch (Exception e) {
|
||||
LogUtils.error(e);
|
||||
}
|
||||
}
|
||||
return fileBytes;
|
||||
}
|
||||
|
||||
private static int getCompressFactor(int width, int height) {
|
||||
@ -122,12 +112,6 @@ public class TempFileUtils {
|
||||
return filePath;
|
||||
}
|
||||
|
||||
//图片缩略图是否存在
|
||||
public static boolean isImgPreviewFileExists(String fileId) {
|
||||
File file = new File(getPreviewImgFilePath(fileId));
|
||||
return file.exists();
|
||||
}
|
||||
|
||||
//图片原图是否存在
|
||||
public static boolean isImgTmpFileExists(String fileId) {
|
||||
File file = new File(getTmpFilePath(fileId));
|
||||
|
@ -78,14 +78,14 @@ public class FileManagementService {
|
||||
fileRequest.setFileName(fileMetadata.getId());
|
||||
fileRequest.setStorage(fileMetadata.getStorage());
|
||||
fileRequest.setFolder(DefaultRepositoryDir.getFileManagementDir(fileMetadata.getProjectId()));
|
||||
//删除临时文件
|
||||
TempFileUtils.deleteTmpFile(fileMetadata.getId());
|
||||
try {
|
||||
//删除存储容器中的文件
|
||||
fileService.deleteFile(fileRequest);
|
||||
//删除缓存文件
|
||||
fileRequest.setFolder(DefaultRepositoryDir.getFileManagementPreviewDir(fileMetadata.getProjectId()));
|
||||
fileService.deleteFile(fileRequest);
|
||||
//删除临时文件
|
||||
TempFileUtils.deleteTmpFile(fileMetadata.getId());
|
||||
} catch (Exception e) {
|
||||
LogUtils.error("删除文件失败", e);
|
||||
}
|
||||
@ -201,14 +201,13 @@ public class FileManagementService {
|
||||
if (previewImg == null || previewImg.length == 0) {
|
||||
try {
|
||||
byte[] fileBytes = this.getFile(fileMetadata);
|
||||
TempFileUtils.compressPic(fileBytes, TempFileUtils.getPreviewImgFilePath(fileMetadata.getId()));
|
||||
previewImg = TempFileUtils.getFile(TempFileUtils.getPreviewImgFilePath(fileMetadata.getId()));
|
||||
previewImg = TempFileUtils.compressPic(fileBytes);
|
||||
fileService.upload(previewImg, previewRequest);
|
||||
return previewImg;
|
||||
} catch (Exception e) {
|
||||
LogUtils.error("获取预览图失败", e);
|
||||
}
|
||||
}
|
||||
return new byte[0];
|
||||
return previewImg;
|
||||
}
|
||||
}
|
||||
|
@ -287,8 +287,7 @@ public class FileMetadataService {
|
||||
String filePath = fileService.upload(file, uploadFileRequest);
|
||||
|
||||
if (TempFileUtils.isImage(fileMetadata.getType())) {
|
||||
TempFileUtils.compressPic(file.getBytes(), TempFileUtils.getPreviewImgFilePath(fileMetadata.getId()));
|
||||
byte[] previewImg = TempFileUtils.getFile(TempFileUtils.getPreviewImgFilePath(fileMetadata.getId()));
|
||||
byte[] previewImg = TempFileUtils.compressPic(file.getBytes());
|
||||
uploadFileRequest.setFolder(DefaultRepositoryDir.getFileManagementPreviewDir(fileMetadata.getProjectId()));
|
||||
fileService.upload(previewImg, uploadFileRequest);
|
||||
}
|
||||
@ -478,17 +477,12 @@ public class FileMetadataService {
|
||||
//svg图片不压缩
|
||||
contentType = MediaType.parseMediaType("image/svg+xml");
|
||||
bytes = this.getFileByte(fileMetadata);
|
||||
} else if (TempFileUtils.isImgPreviewFileExists(fileMetadata.getId())) {
|
||||
//获取压缩过的图片
|
||||
bytes = TempFileUtils.getFile(TempFileUtils.getPreviewImgFilePath(fileMetadata.getId()));
|
||||
} else {
|
||||
/**
|
||||
* 从minio中获取临时文件
|
||||
* 如果minio不存在,压缩后上传到minio中,并缓存到文件目录中
|
||||
*/
|
||||
//压缩图片并保存在临时文件夹中
|
||||
bytes = fileManagementService.getPreviewImg(fileMetadata);
|
||||
TempFileUtils.createFile(TempFileUtils.getPreviewImgFilePath(fileMetadata.getId()), bytes);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -905,6 +905,12 @@ public class FileManagementControllerTests extends BaseTest {
|
||||
fileBytes = mvcResult.getResponse().getContentAsByteArray();
|
||||
Assertions.assertTrue(fileBytes.length > 0);
|
||||
|
||||
//删除存储的临时文件,再重新下载
|
||||
FILE_ID_PATH.forEach((k, v) -> TempFileUtils.deleteTmpFile(k));
|
||||
mvcResult = this.requestPostDownloadFile(FileManagementRequestUtils.URL_FILE_BATCH_DOWNLOAD, zipMediaType, batchProcessDTO);
|
||||
fileBytes = mvcResult.getResponse().getContentAsByteArray();
|
||||
Assertions.assertTrue(fileBytes.length > 0);
|
||||
|
||||
//全部文件大小超过默认配置(600M)的限制 事先存储20个大小为50M的数据,过后删除
|
||||
for (int i = 0; i < 20; i++) {
|
||||
String id = "test_" + i;
|
||||
|
Loading…
Reference in New Issue
Block a user