refactor(项目管理): 优化文件上传的方式

This commit is contained in:
song-tianyang 2024-02-04 20:37:58 +08:00 committed by 刘瑞斌
parent f7d32ef2f6
commit 77015d2127
3 changed files with 28 additions and 11 deletions

View File

@ -103,7 +103,13 @@ public class MinioRepository implements FileRepository {
@Override
public String saveFile(InputStream inputStream, FileRequest request) throws Exception {
return null;
String filePath = getPath(request);
client.putObject(PutObjectArgs.builder()
.bucket(BUCKET)
.object(filePath)
.stream(inputStream, -1, 5242880) // 文件内容
.build());
return filePath;
}
@Override

View File

@ -45,6 +45,7 @@ import org.springframework.util.unit.DataSize;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -281,18 +282,22 @@ public class FileMetadataService {
}
private String uploadFile(FileMetadata fileMetadata, MultipartFile file) throws Exception {
String filePath;
FileRequest uploadFileRequest = new FileRequest();
uploadFileRequest.setFileName(fileMetadata.getId());
uploadFileRequest.setFolder(this.generateMinIOFilePath(fileMetadata.getProjectId()));
uploadFileRequest.setStorage(StorageType.MINIO.name());
String filePath = fileService.upload(file, uploadFileRequest);
try (InputStream inputStream = file.getInputStream()) {
uploadFileRequest.setFileName(fileMetadata.getId());
uploadFileRequest.setFolder(this.generateMinIOFilePath(fileMetadata.getProjectId()));
uploadFileRequest.setStorage(StorageType.MINIO.name());
filePath = fileService.upload(inputStream, uploadFileRequest);
}
if (TempFileUtils.isImage(fileMetadata.getType())) {
//图片文件自动生成预览图
byte[] previewImg = TempFileUtils.compressPic(file.getInputStream());
if (previewImg.length > 0) {
uploadFileRequest.setFolder(DefaultRepositoryDir.getFileManagementPreviewDir(fileMetadata.getProjectId()));
fileService.upload(previewImg, uploadFileRequest);
try (InputStream inputStream = file.getInputStream()) {
//图片文件自动生成预览图
byte[] previewImg = TempFileUtils.compressPic(inputStream);
if (previewImg.length > 0) {
uploadFileRequest.setFolder(DefaultRepositoryDir.getFileManagementPreviewDir(fileMetadata.getProjectId()));
fileService.upload(previewImg, uploadFileRequest);
}
}
}
return filePath;

View File

@ -5,12 +5,18 @@ import io.metersphere.sdk.file.FileRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
@Service
public class FileService {
public String upload(MultipartFile file, FileRequest request) throws Exception {
return FileCenter.getRepository(request.getStorage()).saveFile(file, request);
}
public String upload(InputStream inputStream, FileRequest request) throws Exception {
return FileCenter.getRepository(request.getStorage()).saveFile(inputStream, request);
}
public String upload(byte[] file, FileRequest request) throws Exception {
return FileCenter.getRepository(request.getStorage()).saveFile(file, request);
}