Browse Source

模型多文件上传接口实现

pull/7/head
西大锐 1 year ago
parent
commit
931420c107
3 changed files with 53 additions and 55 deletions
  1. +6
    -4
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/model/ModelsController.java
  2. +1
    -1
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ModelsService.java
  3. +46
    -50
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java

+ 6
- 4
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/model/ModelsController.java View File

@@ -104,10 +104,10 @@ public class ModelsController {
* @return 上传结果
*/

@PostMapping("/upload/{models_version_id}")
@ApiOperation(value = "上传模型", notes = "根据模型版本表id上传模型文件,并将信息存入数据库。")
public AjaxResult uploadModels(@RequestParam("file") MultipartFile file, @PathVariable("models_version_id") Integer models_version_id) throws Exception {
return AjaxResult.success(this.modelsService.uploadModels(file,models_version_id));
@PostMapping("/upload")
@ApiOperation(value = "上传模型", notes = "根据模型id和版本号上传模型文件,并将信息存入数据库。")
public AjaxResult uploadModels(@RequestParam("files") MultipartFile[] files, @RequestParam("models_id") Integer modelsId, @RequestParam("version") String version) throws Exception {
return AjaxResult.success(this.modelsService.uploadModels(files,modelsId,version));

}

@@ -144,6 +144,8 @@ public class ModelsController {
@ApiOperation(value = "下载模型压缩包", notes = "根据模型ID和版本下载所有模型文件,并打包。")
public ResponseEntity<InputStreamResource> downloadAllModelFiles(@RequestParam("models_id") Integer modelsId, @RequestParam("version") String version) {
return modelsService.downloadAllModelFiles(modelsId, version);


}




+ 1
- 1
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ModelsService.java View File

@@ -68,7 +68,7 @@ public interface ModelsService {



Map uploadModels(MultipartFile file, Integer id) throws Exception;
Map uploadModels(MultipartFile[] files, Integer id, String version) throws Exception;

Map uploadModelsPipeline(ModelsVersion modelsVersion) throws Exception;



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

@@ -204,65 +204,56 @@ public class ModelsServiceImpl implements ModelsService {
/**
* 上传模型
*
* @param file 文件
* @param id 注意是models_version表的主键
* @param files 文件
* @param id 模型id
* @param version 模型版本
* @return 是否成功
*/
@Override
public Map uploadModels(MultipartFile file, Integer id) throws Exception {
if (file.isEmpty()) {
throw new Exception("文件为空,无法上传");
}
// 获取文件大小并转换为KB
long sizeInBytes = file.getSize();
double sizeInKB = sizeInBytes / 1024.0;

// 检查并处理现有的数据集版本记录
ModelsVersion currentModelsVersion = this.modelsVersionDao.queryById(id);
if (currentModelsVersion == null) {
throw new Exception("未找到模型版本记录");
}
public Map uploadModels(MultipartFile[] files, Integer id, String version) throws Exception {

ModelsVersion modelsVersionToUse = currentModelsVersion;
//检查是否存在URL记录
String url = currentModelsVersion.getUrl();
if (url != null && !url.isEmpty()) {
// 逻辑删除当前版本
currentModelsVersion.setState(0);
modelsVersionDao.update(currentModelsVersion);

// 复制原有版本并在数据库中插入新版本
modelsVersionToUse = this.modelsVersionService.duplicateModelsVersion(currentModelsVersion);
}
Map<String, Object> results = new HashMap<String, Object>();

//查询模型名称
Integer modelsId = modelsVersionToUse.getModelsId();
Models models = this.modelsDao.queryById(modelsId);
// 验证模型是否存在
Models models = this.modelsDao.queryById(id);
if (models == null) {
throw new Exception("未找到模型记录");
}

// 其余操作基于 datasetVersionToUse
String username = SecurityUtils.getLoginUser().getUsername();
String version = modelsVersionToUse.getVersion();
Date createTime = modelsVersionToUse.getCreateTime();
String fileName = file.getOriginalFilename();
// String timestamp = new SimpleDateFormat("yyyyMMdd-HHmmss").format(createTime);
String objectName = "models/" + username + "/" + models.getName() + "/" + version + "/" + fileName;

// 上传文件到MinIO
try (InputStream inputStream = file.getInputStream()) {
minioUtil.uploadObject(bucketName, objectName, inputStream);
modelsVersionToUse.setUrl(objectName);
modelsVersionToUse.setFileName(fileName);
modelsVersionToUse.setFileSize(String.valueOf(sizeInKB));
modelsVersionDao.update(modelsVersionToUse);
} catch (Exception e) {
throw new Exception("上传到模型失败: " + e.getMessage(), e);
for (MultipartFile file:files){
if (file.isEmpty()) {
throw new Exception("文件为空,无法上传");
}
// 获取文件大小并转换为KB
long sizeInBytes = file.getSize();
double sizeInKB = sizeInBytes / 1024.0;

// 其余操作基于 modelsVersionToUse
String username = SecurityUtils.getLoginUser().getUsername();
String fileName = file.getOriginalFilename();
// String timestamp = new SimpleDateFormat("yyyyMMdd-HHmmss").format(createTime);
String objectName = "models/" + username + "/" + models.getName() + "/" + version + "/" + fileName;

// 上传文件到MinIO并将记录新增到数据库中
try (InputStream inputStream = file.getInputStream()) {
minioUtil.uploadObject(bucketName, objectName, inputStream);
ModelsVersion modelsVersion = new ModelsVersion();
modelsVersion.setModelsId(id);
modelsVersion.setVersion(version);
modelsVersion.setUrl(objectName);
modelsVersion.setFileName(fileName);
modelsVersion.setFileSize(String.valueOf(sizeInKB));
modelsVersionService.insert(modelsVersion);
} catch (Exception e) {
throw new Exception("上传到模型失败: " + e.getMessage(), e);
}
Map<String, String> fileResult = new HashMap<>();
fileResult.put("fileName", file.getOriginalFilename());
fileResult.put("url", objectName); // objectName根据实际情况定义
results.put(file.getOriginalFilename(), fileResult);

}
Map<String, String> result = new HashMap<String, String>();
result.put("url",objectName);
return result;
return results;
}


@@ -277,6 +268,8 @@ public class ModelsServiceImpl implements ModelsService {
}

ModelsVersion version = modelsVersionService.queryByModelsVersion(modelsVersion);


String url = "";
if (version == null) {
//插表,因为这里是一次直接插表所以这里定掉date,然后用DAO插入
@@ -313,6 +306,9 @@ public class ModelsServiceImpl implements ModelsService {
*/
@Override
public ResponseEntity<InputStreamResource> downloadAllModelFiles(Integer modelsId, String version) {
// 根据模型id查模型名
Models model = this.modelsDao.queryById(modelsId);
String modelName = model.getName();
// 查询特定模型和版本对应的所有文件
List<ModelsVersion> modelsVersionList = this.modelsVersionDao.queryAllByModelsVersion(modelsId, version);
if (modelsVersionList == null || modelsVersionList.isEmpty()) {
@@ -343,7 +339,7 @@ public class ModelsServiceImpl implements ModelsService {

// 设置响应
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"models_" + modelsId + "_version_" + version + ".zip\"")
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + modelName + "_" + version + ".zip\"")
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(resource);
} catch (Exception e) {


Loading…
Cancel
Save