Browse Source

数据集模型上传代码复用重写,删除冗余代码

pull/19/head
西大锐 1 year ago
parent
commit
1951c21355
4 changed files with 129 additions and 61 deletions
  1. +8
    -26
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/DatasetServiceImpl.java
  2. +2
    -2
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/MinioServiceImpl.java
  3. +9
    -32
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java
  4. +110
    -1
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/MinioUtil.java

+ 8
- 26
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/DatasetServiceImpl.java View File

@@ -9,6 +9,7 @@ import com.ruoyi.platform.mapper.DatasetDao;
import com.ruoyi.platform.mapper.DatasetVersionDao;
import com.ruoyi.platform.service.DatasetService;
import com.ruoyi.platform.service.DatasetVersionService;
import com.ruoyi.platform.service.MinioService;
import com.ruoyi.platform.utils.BeansUtils;
import com.ruoyi.platform.utils.FileUtil;
import com.ruoyi.platform.utils.MinioUtil;
@@ -56,6 +57,9 @@ public class DatasetServiceImpl implements DatasetService {
@Resource
private DatasetVersionService datasetVersionService;

@Resource
private MinioService minioService;

// 固定存储桶名
private final String bucketName = "platform-data";

@@ -209,39 +213,16 @@ public class DatasetServiceImpl implements DatasetService {
*/
@Override
public List<Map<String, String>> uploadDataset(MultipartFile[] files, String uuid) throws Exception {
List<Map<String, String>> results = new ArrayList<>();
// //时间戳统一定在外面,一次上传就定好
// Date createTime = new Date();
// String timestamp = new SimpleDateFormat("yyyyMMdd-HHmmss").format(createTime);

List<Map<String, String>> results = new ArrayList<>();
for (MultipartFile file:files){
if (file.isEmpty()) {
throw new Exception("文件为空,无法上传");
}
// 获取文件大小并转换为可读形式
long sizeInBytes = file.getSize();
String formattedSize = FileUtil.formatFileSize(sizeInBytes); // 格式化文件大小

// 其余操作基于 modelsVersionToUse
// 构建objectName
String username = SecurityUtils.getLoginUser().getUsername();
String fileName = file.getOriginalFilename();
String objectName = "datasets/" + username + "/" + uuid + "/" + fileName;

// 上传文件到MinIO并将记录新增到数据库中
try (InputStream inputStream = file.getInputStream()) {
minioUtil.uploadObject(bucketName, objectName, inputStream);

Map<String, String> fileResult = new HashMap<>();
fileResult.put("fileName", file.getOriginalFilename());
fileResult.put("url", objectName); // objectName根据实际情况定义
fileResult.put("fileSize", formattedSize);
results.add(fileResult);
} catch (Exception e) {
throw new Exception("上传数据集失败: " + e.getMessage(), e);
}
results.add(minioService.uploadFile(bucketName, objectName, file));
}
return results;

}


@@ -308,6 +289,7 @@ public class DatasetServiceImpl implements DatasetService {
List<VersionVo> datasetVersionVos = datasetVo.getDatasetVersionVos();
if (datasetVersionVos==null || datasetVersionVos.isEmpty()){
throw new Exception("数据集版本信息错误");

}

Dataset dataset = new Dataset();


+ 2
- 2
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/MinioServiceImpl.java View File

@@ -50,7 +50,7 @@ public class MinioServiceImpl implements MinioService {
}

@Override
public Map<String, String> uploadFile(String bucketName, String objectName,MultipartFile file ) throws Exception {
public Map<String, String> uploadFile(String bucketName, String objectName, MultipartFile file ) throws Exception {
if (file.isEmpty()) {
throw new Exception("文件为空,无法上传");
}
@@ -65,7 +65,7 @@ public class MinioServiceImpl implements MinioService {
result.put("url", objectName); // objectName根据实际情况定义
result.put("fileSize", formattedSize);
} catch (Exception e) {
throw new Exception("上传数据集失败: " + e.getMessage(), e);
throw new Exception("上传文件失败: " + e.getMessage(), e);
}
return result;
}


+ 9
- 32
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java View File

@@ -5,6 +5,7 @@ import com.ruoyi.platform.domain.Models;
import com.ruoyi.platform.domain.ModelsVersion;
import com.ruoyi.platform.mapper.ModelsDao;
import com.ruoyi.platform.mapper.ModelsVersionDao;
import com.ruoyi.platform.service.MinioService;
import com.ruoyi.platform.service.ModelsService;
import com.ruoyi.platform.service.ModelsVersionService;
import com.ruoyi.platform.utils.BeansUtils;
@@ -52,6 +53,9 @@ public class ModelsServiceImpl implements ModelsService {
@Resource
private ModelsVersionService modelsVersionService;

@Resource
private MinioService minioService;


// 固定存储桶名
private final String bucketName = "platform-data";
@@ -194,50 +198,23 @@ public class ModelsServiceImpl implements ModelsService {
}

/**
* 上传模型
* 上传模型文件
*
* @param files 文件
* @param uuid
* @param uuid 唯一标识
* @return 是否成功
*/
@Override
public List<Map<String, String>> uploadModels(MultipartFile[] files, String uuid) throws Exception {

List<Map<String, String>> results = new ArrayList<>();
// //时间戳统一定在外面,一次上传就定好
// Date createTime = new Date();
// String timestamp = new SimpleDateFormat("yyyyMMdd-HHmmss").format(createTime);

for (MultipartFile file:files){
if (file.isEmpty()) {
throw new Exception("文件为空,无法上传");
}
// 获取文件大小并转换为KB
long sizeInBytes = file.getSize();
String formattedSize = FileUtil.formatFileSize(sizeInBytes); // 格式化文件大小




// 其余操作基于 modelsVersionToUse
for (MultipartFile file:files) {
// 构建objectName
String username = SecurityUtils.getLoginUser().getUsername();
String fileName = file.getOriginalFilename();
String objectName = "models/" + username + "/" + uuid + "/" + fileName;

// 上传文件到MinIO并将记录新增到数据库中
try (InputStream inputStream = file.getInputStream()) {
minioUtil.uploadObject(bucketName, objectName, inputStream);
//
Map<String, String> fileResult = new HashMap<>();
fileResult.put("fileName", file.getOriginalFilename());
fileResult.put("url", objectName); // objectName根据实际情况定义
fileResult.put("fileSize", formattedSize);
results.add(fileResult);
} catch (Exception e) {
throw new Exception("上传模型失败: " + e.getMessage(), e);
}
results.add(minioService.uploadFile(bucketName, objectName, file));
}

return results;
}



+ 110
- 1
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/MinioUtil.java View File

@@ -8,6 +8,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.StandardCharsets;
@@ -23,7 +24,7 @@ import java.util.zip.ZipOutputStream;
@Component
public class MinioUtil {

private MinioClient minioClient;
private static MinioClient minioClient;

@Autowired
public MinioUtil(@Value("${minio.endpoint}")String minioEndpoint,@Value("${minio.accessKey}")String minioAccessKey,@Value("${minio.secretKey}") String minioSecretKey) {
@@ -43,6 +44,84 @@ public class MinioUtil {
minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
}

/**
* 验证bucketName是否存在
*
* @return boolean true:存在
*/
public boolean bucketExists(String bucketName)
throws Exception {
return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
}

/**
* 判断文件是否存在
*
* @param bucketName 存储桶
* @param objectName 对象
* @return true:存在
*/
public static boolean doesObjectExist(String bucketName, String objectName) {
boolean exist = true;
try {
minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
} catch (Exception e) {
exist = false;
}
return exist;
}

/**
* 判断文件夹是否存在
*
* @param bucketName 存储桶
* @param objectName 文件夹名称(去掉/)
* @return true:存在
*/
public static boolean doesFolderExist(String bucketName, String objectName) {
boolean exist = false;
try {
Iterable<Result<Item>> results = minioClient.listObjects(
ListObjectsArgs.builder().bucket(bucketName).prefix(objectName).recursive(false).build());
for (Result<Item> result : results) {
Item item = result.get();
if (item.isDir() && objectName.equals(item.objectName())) {
exist = true;
}
}
} catch (Exception e) {
exist = false;
}
return exist;
}

/**
* 根据文件前置查询文件
*
* @param bucketName bucket名称
* @param prefix 前缀
* @param recursive 是否递归查询
* @return MinioItem 列表
*/
public static List<Item> getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) throws Exception {
List<Item> list = new ArrayList<>();
Iterable<Result<Item>> objectsIterator = minioClient.listObjects(
ListObjectsArgs.
builder().
bucket(bucketName).
prefix(prefix).
recursive(recursive).
build());
if (objectsIterator != null) {
for (Result<Item> o : objectsIterator) {
Item item = o.get();
list.add(item);
}
}
return list;
}


public void uploadObject(String bucketName, String objectName, InputStream stream) throws Exception {
long size = stream.available();
minioClient.putObject(
@@ -54,6 +133,36 @@ public class MinioUtil {
);
}

/**
* 通过MultipartFile,上传文件
*
* @param bucketName 存储桶
* @param file 文件
* @param objectName 对象名
* @param contentType 文件类型
*/
public static ObjectWriteResponse putObject(String bucketName, MultipartFile file, String objectName, String contentType) throws Exception {
InputStream inputStream = file.getInputStream();
return minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object(objectName).contentType(contentType)
.stream(inputStream, inputStream.available(), -1).build());
}

/**
* 通过MultipartFile,上传文件
*
* @param bucketName 存储桶
* @param file 文件
* @param objectName 对象名
*/
public static ObjectWriteResponse putObject(String bucketName, MultipartFile file, String objectName) throws Exception {
InputStream inputStream = file.getInputStream();
return minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object(objectName)
.stream(inputStream, inputStream.available(), -1).build());
}


public void downloadObject(String bucketName, String objectName, OutputStream stream) throws Exception {
try (InputStream inStream = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build())) {
byte[] buffer = new byte[1024];


Loading…
Cancel
Save