|
|
|
@@ -1,6 +1,8 @@ |
|
|
|
package com.ruoyi.platform.service.impl; |
|
|
|
|
|
|
|
import com.ruoyi.common.security.utils.SecurityUtils; |
|
|
|
import com.ruoyi.platform.domain.Dataset; |
|
|
|
import com.ruoyi.platform.domain.DatasetVersion; |
|
|
|
import com.ruoyi.platform.domain.Models; |
|
|
|
import com.ruoyi.platform.domain.ModelsVersion; |
|
|
|
import com.ruoyi.platform.mapper.ModelsDao; |
|
|
|
@@ -201,53 +203,60 @@ public class ModelsServiceImpl implements ModelsService { |
|
|
|
* @param id 注意是models_version表的主键 |
|
|
|
* @return 是否成功 |
|
|
|
*/ |
|
|
|
|
|
|
|
@Override |
|
|
|
public String uploadModels(MultipartFile file, Integer id) throws Exception{ |
|
|
|
if(file.isEmpty()){ |
|
|
|
public String uploadModels(MultipartFile file, Integer id) throws Exception { |
|
|
|
if (file.isEmpty()) { |
|
|
|
throw new Exception("文件为空,无法上传"); |
|
|
|
} |
|
|
|
// 获取文件大小并转换为KB |
|
|
|
long sizeInBytes = file.getSize(); |
|
|
|
double sizeInKB = sizeInBytes / 1024.0; |
|
|
|
|
|
|
|
//拿到models表的id去查模型名字 |
|
|
|
ModelsVersion modelsVersion = this.modelsVersionDao.queryById(id); |
|
|
|
if (modelsVersion == null){ |
|
|
|
// 检查并处理现有的数据集版本记录 |
|
|
|
ModelsVersion currentModelsVersion = this.modelsVersionDao.queryById(id); |
|
|
|
if (currentModelsVersion == null) { |
|
|
|
throw new Exception("未找到模型版本记录"); |
|
|
|
} |
|
|
|
Integer modelsId = modelsVersion.getModelsId(); |
|
|
|
|
|
|
|
ModelsVersion modelsVersionToUse = currentModelsVersion; |
|
|
|
//检查是否存在URL记录 |
|
|
|
String url = currentModelsVersion.getUrl(); |
|
|
|
if (url != null && !url.isEmpty()) { |
|
|
|
// 逻辑删除当前版本 |
|
|
|
currentModelsVersion.setState(0); |
|
|
|
modelsVersionDao.update(currentModelsVersion); |
|
|
|
|
|
|
|
// 复制原有版本并在数据库中插入新版本 |
|
|
|
modelsVersionToUse = this.modelsVersionService.duplicateModelsVersion(currentModelsVersion); |
|
|
|
} |
|
|
|
|
|
|
|
//查询模型名称 |
|
|
|
Integer modelsId = modelsVersionToUse.getModelsId(); |
|
|
|
Models models = this.modelsDao.queryById(modelsId); |
|
|
|
if (models == null) { |
|
|
|
throw new Exception("未找到模型记录"); |
|
|
|
} |
|
|
|
|
|
|
|
//得到用户名 |
|
|
|
LoginUser loginUser = SecurityUtils.getLoginUser(); |
|
|
|
String version = modelsVersion.getVersion(); |
|
|
|
String username = loginUser.getUsername(); |
|
|
|
Date createTime = models.getCreateTime(); |
|
|
|
// 其余操作基于 datasetVersionToUse |
|
|
|
String username = SecurityUtils.getLoginUser().getUsername(); |
|
|
|
String version = modelsVersionToUse.getVersion(); |
|
|
|
Date createTime = modelsVersionToUse.getCreateTime(); |
|
|
|
String fileName = file.getOriginalFilename(); |
|
|
|
//格式化日期为时间戳字符串 |
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss"); |
|
|
|
String timestamp = sdf.format(createTime); |
|
|
|
//拼接objectName |
|
|
|
String objectName = "models/" + username + "/" + models.getName() + "-" + timestamp + "/" + version + "/" + fileName; |
|
|
|
String timestamp = new SimpleDateFormat("yyyyMMdd-HHmmss").format(createTime); |
|
|
|
String objectName = "models/" + username + "/" + models.getName() + "-" + timestamp + "/" + version + "/" + fileName; |
|
|
|
|
|
|
|
// 上传文件到MinIO |
|
|
|
try (InputStream inputStream = file.getInputStream()) { |
|
|
|
minioUtil.uploadObject(bucketName, objectName, inputStream); |
|
|
|
// 更新数据库url |
|
|
|
modelsVersion.setUrl(objectName); |
|
|
|
modelsVersion.setFileName(fileName); |
|
|
|
modelsVersion.setFileSize(String.valueOf(sizeInKB)); |
|
|
|
modelsVersionDao.update(modelsVersion); |
|
|
|
modelsVersionToUse.setUrl(objectName); |
|
|
|
modelsVersionToUse.setFileName(fileName); |
|
|
|
modelsVersionToUse.setFileSize(String.valueOf(sizeInKB)); |
|
|
|
modelsVersionDao.update(modelsVersionToUse); |
|
|
|
return "模型成功上传到: " + objectName; |
|
|
|
} catch (Exception e) { |
|
|
|
throw new Exception("上传到MinIO失败: " + e.getMessage(), e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private String extractFileName(String urlStr) { |
|
|
|
return urlStr.substring(urlStr.lastIndexOf('/') + 1); |
|
|
|
} |
|
|
|
|