| @@ -132,14 +132,12 @@ public class ImageController { | |||||
| * 镜像上传 | * 镜像上传 | ||||
| * | * | ||||
| * | * | ||||
| * @param files 文件 | |||||
| * @return 上传结果 | * @return 上传结果 | ||||
| */ | */ | ||||
| @PostMapping("/upload") | @PostMapping("/upload") | ||||
| @ApiOperation(value = "上传镜像文件", notes = "上传镜像tar包,返回存储路径") | @ApiOperation(value = "上传镜像文件", notes = "上传镜像tar包,返回存储路径") | ||||
| public AjaxResult uploadImageFiles(@RequestParam("files") MultipartFile[] files) throws Exception { | |||||
| return AjaxResult.success(this.imageService.uploadImageFiles(files)); | |||||
| public AjaxResult uploadImageFiles(@RequestParam("file") MultipartFile file) throws Exception { | |||||
| return AjaxResult.success(this.imageService.uploadImageFiles(file)); | |||||
| } | } | ||||
| } | } | ||||
| @@ -19,6 +19,8 @@ import org.springframework.web.bind.annotation.*; | |||||
| import org.springframework.web.multipart.MultipartFile; | import org.springframework.web.multipart.MultipartFile; | ||||
| import javax.annotation.Resource; | import javax.annotation.Resource; | ||||
| import java.util.ArrayList; | |||||
| import java.util.List; | |||||
| /** | /** | ||||
| * (Models)表控制层 | * (Models)表控制层 | ||||
| @@ -186,9 +188,9 @@ public class ModelsController { | |||||
| *@param models_version_id 模型版本表主键 | *@param models_version_id 模型版本表主键 | ||||
| * @return 单条数据 | * @return 单条数据 | ||||
| */ | */ | ||||
| @GetMapping("/download/{models_version_id}") | |||||
| @GetMapping("/download_model") | |||||
| @ApiOperation(value = "下载模型", notes = "根据模型版本表id下载模型文件。") | @ApiOperation(value = "下载模型", notes = "根据模型版本表id下载模型文件。") | ||||
| public ResponseEntity<InputStreamResource> downloadModels(@PathVariable("models_version_id") Integer models_version_id) { | |||||
| public ResponseEntity<InputStreamResource> downloadModels(@RequestParam("models_version_id") Integer models_version_id) throws Exception { | |||||
| return modelsService.downloadModels(models_version_id); | return modelsService.downloadModels(models_version_id); | ||||
| } | } | ||||
| @@ -203,7 +205,6 @@ public class ModelsController { | |||||
| @ApiOperation(value = "下载模型压缩包", notes = "根据模型ID和版本下载所有模型文件,并打包。") | @ApiOperation(value = "下载模型压缩包", notes = "根据模型ID和版本下载所有模型文件,并打包。") | ||||
| public ResponseEntity<InputStreamResource> downloadAllModelFiles(@RequestParam("models_id") Integer modelsId, @RequestParam("version") String version) { | public ResponseEntity<InputStreamResource> downloadAllModelFiles(@RequestParam("models_id") Integer modelsId, @RequestParam("version") String version) { | ||||
| return modelsService.downloadAllModelFiles(modelsId, version); | return modelsService.downloadAllModelFiles(modelsId, version); | ||||
| } | } | ||||
| @@ -8,6 +8,7 @@ import org.springframework.data.domain.PageRequest; | |||||
| import org.springframework.web.multipart.MultipartFile; | import org.springframework.web.multipart.MultipartFile; | ||||
| import java.util.Collection; | import java.util.Collection; | ||||
| import java.util.Map; | |||||
| /** | /** | ||||
| * (Image)表服务接口 | * (Image)表服务接口 | ||||
| @@ -66,8 +67,15 @@ public interface ImageService { | |||||
| String insertImageAndVersion(ImageVo imageVo) throws Exception; | String insertImageAndVersion(ImageVo imageVo) throws Exception; | ||||
| /** | |||||
| * 本地上传构建镜像 | |||||
| * @param imageName | |||||
| * @param imageTag | |||||
| * @param imageDescription | |||||
| * @return | |||||
| */ | |||||
| String createImageFromLocal(String imageName, String imageTag, String imageDescription); | String createImageFromLocal(String imageName, String imageTag, String imageDescription); | ||||
| String uploadImageFiles(MultipartFile[] files); | |||||
| Map<String, String> uploadImageFiles(MultipartFile file) throws Exception; | |||||
| } | } | ||||
| @@ -2,7 +2,13 @@ package com.ruoyi.platform.service; | |||||
| import org.springframework.core.io.InputStreamResource; | import org.springframework.core.io.InputStreamResource; | ||||
| import org.springframework.http.ResponseEntity; | import org.springframework.http.ResponseEntity; | ||||
| import org.springframework.web.multipart.MultipartFile; | |||||
| import java.util.List; | |||||
| import java.util.Map; | |||||
| public interface MinioService { | public interface MinioService { | ||||
| public ResponseEntity<InputStreamResource> downloadZipFile(String bucketName , String path); | |||||
| ResponseEntity<InputStreamResource> downloadZipFile(String bucketName , String path); | |||||
| Map<String, String> uploadFile(String bucketName, String objectName, MultipartFile file ) throws Exception; | |||||
| } | } | ||||
| @@ -66,7 +66,7 @@ public interface ModelsService { | |||||
| String removeById(Integer id); | String removeById(Integer id); | ||||
| ResponseEntity<InputStreamResource> downloadModels(Integer id); | |||||
| ResponseEntity<InputStreamResource> downloadModels(Integer id) throws Exception; | |||||
| @@ -6,6 +6,7 @@ import com.ruoyi.platform.mapper.ImageDao; | |||||
| import com.ruoyi.platform.mapper.ImageVersionDao; | import com.ruoyi.platform.mapper.ImageVersionDao; | ||||
| import com.ruoyi.platform.service.ImageService; | import com.ruoyi.platform.service.ImageService; | ||||
| import com.ruoyi.platform.service.ImageVersionService; | import com.ruoyi.platform.service.ImageVersionService; | ||||
| import com.ruoyi.platform.service.MinioService; | |||||
| import com.ruoyi.platform.utils.FileUtil; | import com.ruoyi.platform.utils.FileUtil; | ||||
| import com.ruoyi.platform.vo.ImageVo; | import com.ruoyi.platform.vo.ImageVo; | ||||
| import com.ruoyi.system.api.model.LoginUser; | import com.ruoyi.system.api.model.LoginUser; | ||||
| @@ -43,7 +44,10 @@ public class ImageServiceImpl implements ImageService { | |||||
| private ImageVersionDao imageVersionDao; | private ImageVersionDao imageVersionDao; | ||||
| @Resource | |||||
| private MinioService minioService; | |||||
| private String bucketName; | |||||
| /** | /** | ||||
| * 通过ID查询单条数据 | * 通过ID查询单条数据 | ||||
| * | * | ||||
| @@ -174,56 +178,22 @@ public class ImageServiceImpl implements ImageService { | |||||
| } | } | ||||
| @Override | @Override | ||||
| public String createImageFromLocal(String imageName, String imageTag, String imageDescription) { | |||||
| public String createImageFromLocal(String imageName, String imageTag, String fileName) { | |||||
| // TODO 检查环境是否存在,如果不存在,请开启容器 | |||||
| // TODO 在容器的/data/admin 目录下执行命令 docker load -i fileName 得到返回的镜像名字name:tag | |||||
| // TODO 在容器里执行 docker tag name:tag nexus3.kube-system.svc:8083/imageName:imageTag | |||||
| return null; | return null; | ||||
| } | } | ||||
| @Override | @Override | ||||
| public String uploadImageFiles(MultipartFile[] files) { | |||||
| // Map<Integer, Object> results = new HashMap<Integer, Object>(); | |||||
| // | |||||
| // // 验证模型是否存在 | |||||
| // Models models = this.modelsDao.queryById(id); | |||||
| // if (models == null) { | |||||
| // throw new Exception("未找到模型记录"); | |||||
| // } | |||||
| // | |||||
| // for (MultipartFile file:files){ | |||||
| // if (file.isEmpty()) { | |||||
| // throw new Exception("文件为空,无法上传"); | |||||
| // } | |||||
| // // 获取文件大小并转换为KB | |||||
| // long sizeInBytes = file.getSize(); | |||||
| // String formattedSize = FileUtil.formatFileSize(sizeInBytes); // 格式化文件大小 | |||||
| // | |||||
| // // 其余操作基于 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(formattedSize); | |||||
| // //返回插入结果 | |||||
| // ModelsVersion insertedModelsVersion = modelsVersionService.insert(modelsVersion); | |||||
| // Map<String, String> fileResult = new HashMap<>(); | |||||
| // fileResult.put("fileName", file.getOriginalFilename()); | |||||
| // fileResult.put("url", objectName); // objectName根据实际情况定义 | |||||
| // results.put(insertedModelsVersion.getId(), fileResult); | |||||
| // } catch (Exception e) { | |||||
| // throw new Exception("上传到模型失败: " + e.getMessage(), e); | |||||
| // } | |||||
| // } | |||||
| // return results; | |||||
| return null; | |||||
| public Map<String, String> uploadImageFiles(MultipartFile file) throws Exception { | |||||
| LoginUser loginUser = SecurityUtils.getLoginUser(); | |||||
| String path = loginUser.getUsername()+"/"+file.getOriginalFilename(); | |||||
| Map<String, String> stringMap = minioService.uploadFile(bucketName, path, file); | |||||
| return stringMap; | |||||
| } | } | ||||
| } | } | ||||
| @@ -1,6 +1,8 @@ | |||||
| package com.ruoyi.platform.service.impl; | package com.ruoyi.platform.service.impl; | ||||
| import com.ruoyi.common.security.utils.SecurityUtils; | |||||
| import com.ruoyi.platform.service.MinioService; | import com.ruoyi.platform.service.MinioService; | ||||
| import com.ruoyi.platform.utils.FileUtil; | |||||
| import com.ruoyi.platform.utils.MinioUtil; | import com.ruoyi.platform.utils.MinioUtil; | ||||
| import org.springframework.core.io.InputStreamResource; | import org.springframework.core.io.InputStreamResource; | ||||
| import org.springframework.http.HttpHeaders; | import org.springframework.http.HttpHeaders; | ||||
| @@ -8,12 +10,18 @@ import org.springframework.http.HttpStatus; | |||||
| import org.springframework.http.MediaType; | import org.springframework.http.MediaType; | ||||
| import org.springframework.http.ResponseEntity; | import org.springframework.http.ResponseEntity; | ||||
| import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | ||||
| import org.springframework.web.multipart.MultipartFile; | |||||
| import java.io.ByteArrayInputStream; | import java.io.ByteArrayInputStream; | ||||
| import java.io.ByteArrayOutputStream; | import java.io.ByteArrayOutputStream; | ||||
| import java.io.InputStream; | import java.io.InputStream; | ||||
| import java.nio.file.Path; | import java.nio.file.Path; | ||||
| import java.nio.file.Paths; | import java.nio.file.Paths; | ||||
| import java.text.SimpleDateFormat; | |||||
| import java.util.Date; | |||||
| import java.util.HashMap; | |||||
| import java.util.Map; | |||||
| @Service("MinioService") | @Service("MinioService") | ||||
| public class MinioServiceImpl implements MinioService { | public class MinioServiceImpl implements MinioService { | ||||
| @@ -40,4 +48,25 @@ public class MinioServiceImpl implements MinioService { | |||||
| return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); | return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); | ||||
| } | } | ||||
| } | } | ||||
| @Override | |||||
| public Map<String, String> uploadFile(String bucketName, String objectName,MultipartFile file ) throws Exception { | |||||
| if (file.isEmpty()) { | |||||
| throw new Exception("文件为空,无法上传"); | |||||
| } | |||||
| // 获取文件大小并转换为可读形式 | |||||
| long sizeInBytes = file.getSize(); | |||||
| String formattedSize = FileUtil.formatFileSize(sizeInBytes); // 格式化文件大小 | |||||
| Map<String, String> result = new HashMap<>(); | |||||
| // 上传文件到MinIO并将记录新增到数据库中 | |||||
| try (InputStream inputStream = file.getInputStream()){ | |||||
| minioUtil.uploadObject(bucketName, objectName, inputStream); | |||||
| result.put("fileName", file.getOriginalFilename()); | |||||
| result.put("url", objectName); // objectName根据实际情况定义 | |||||
| result.put("fileSize", formattedSize); | |||||
| } catch (Exception e) { | |||||
| throw new Exception("上传数据集失败: " + e.getMessage(), e); | |||||
| } | |||||
| return result; | |||||
| } | |||||
| } | } | ||||
| @@ -176,15 +176,15 @@ public class ModelsServiceImpl implements ModelsService { | |||||
| */ | */ | ||||
| @Override | @Override | ||||
| public ResponseEntity<InputStreamResource> downloadModels(Integer id) { | |||||
| public ResponseEntity<InputStreamResource> downloadModels(Integer id) throws Exception { | |||||
| ModelsVersion modelsVersion = this.modelsVersionDao.queryById(id); | ModelsVersion modelsVersion = this.modelsVersionDao.queryById(id); | ||||
| if (modelsVersion == null) { | if (modelsVersion == null) { | ||||
| return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); | |||||
| throw new Exception("未找到该版本模型"); | |||||
| } | } | ||||
| // 从数据库中获取存储路径(即MinIO中的对象名称) | // 从数据库中获取存储路径(即MinIO中的对象名称) | ||||
| String objectName = modelsVersion.getUrl(); | String objectName = modelsVersion.getUrl(); | ||||
| if(objectName == null || objectName.isEmpty() ){ | if(objectName == null || objectName.isEmpty() ){ | ||||
| return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); | |||||
| throw new Exception("未找到该版本模型文件"); | |||||
| } | } | ||||
| try { | try { | ||||
| @@ -200,7 +200,7 @@ public class ModelsServiceImpl implements ModelsService { | |||||
| .body(resource); | .body(resource); | ||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| e.printStackTrace(); | e.printStackTrace(); | ||||
| return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); | |||||
| throw new Exception("下载模型文件错误"); | |||||
| } | } | ||||
| } | } | ||||