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 @Override
public String saveFile(InputStream inputStream, FileRequest request) throws Exception { 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 @Override

View File

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

View File

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