Compare commits
3 Commits
9fa40c1722
...
7dcb551b18
| Author | SHA1 | Date |
|---|---|---|
|
|
7dcb551b18 | |
|
|
efbdffeb9b | |
|
|
89a67ac48e |
|
|
@ -2,6 +2,7 @@ package com.ruoyi.platform.scheduling;
|
|||
|
||||
import cn.hutool.core.lang.UUID;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONArray;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
|
|
@ -203,10 +204,8 @@ public class AppScheduleTask {
|
|||
|
||||
String zipFileName = "batch_download_" + taskId + ".zip";
|
||||
String zipFilePath = savePath + "/" + zipFileName;
|
||||
HashMap<String, String> upload = MapUtil.newHashMap();
|
||||
upload.put("input", (String) taskInfo.getByPath("input_file.direct_url"));
|
||||
upload.put("output", saveFilePath);
|
||||
csCollectService.allFileUpload(upload, savePath + "/" + zipFileName);
|
||||
List<String> inputFilePaths = readInputFilePath(taskInfo);
|
||||
csCollectService.allFileUpload(inputFilePaths, saveFilePath, zipFilePath);
|
||||
HashMap<String, String> batch_download = MapUtil.newHashMap();
|
||||
batch_download.put("direct_url", zipFilePath);
|
||||
batch_download.put("filename", zipFileName);
|
||||
|
|
@ -228,4 +227,28 @@ public class AppScheduleTask {
|
|||
// trainingTaskDao.update(trainingTask);
|
||||
// }
|
||||
//}
|
||||
|
||||
/**
|
||||
* 获取输入路径
|
||||
*
|
||||
* @param taskInfo
|
||||
* @return
|
||||
*/
|
||||
private List<String> readInputFilePath(JSONObject taskInfo) {
|
||||
List<String> empty = new ArrayList<>();;
|
||||
taskInfo.forEach((key, value) -> {
|
||||
if (value instanceof JSONObject) {
|
||||
JSONObject valueObj = (JSONObject) value;
|
||||
if (valueObj.containsKey("direct_url")) {
|
||||
|
||||
String directUrl = valueObj.getStr("direct_url");
|
||||
String filename = valueObj.getStr("filename");
|
||||
|
||||
String suffix = StrUtil.removeSuffix(directUrl, filename);
|
||||
empty.add(suffix);
|
||||
}
|
||||
}
|
||||
});
|
||||
return empty;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import cn.hutool.json.JSONObject;
|
|||
import com.ruoyi.platform.domain.TrainingTask;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public interface CSCollectService {
|
||||
|
||||
|
|
@ -23,5 +23,5 @@ public interface CSCollectService {
|
|||
|
||||
JSONObject singleFileDownload(Integer objectId, String savePath,String fileName) throws Exception;
|
||||
|
||||
void allFileUpload(HashMap<String, String> upload, String zipFilePath) throws Exception;
|
||||
void allFileUpload(List<String> inputs, String output, String zipFilePath) throws Exception;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -101,8 +101,7 @@ public class CSCollectServiceImpl implements CSCollectService {
|
|||
.contentType("application/json")
|
||||
.execute()
|
||||
.body();
|
||||
JSONObject resData = returnResDataWithOK(res);
|
||||
return (Integer) getByPath(resData, "object.objectID");
|
||||
return packageId;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -123,7 +122,6 @@ public class CSCollectServiceImpl implements CSCollectService {
|
|||
CSAuthInfo csAuthInfo = getZSAuthInfoFromRedis();
|
||||
List<Map<String, Object>> hpcBindingFiles = buildHpcBindingFiles(taskParam, taskInfo);
|
||||
JSONObject params = convertToJobRequest(trainingTask.getTaskName(),clusterId, jobName, hpcBindingFiles, csAuthInfo.getUserId(), ntasks, nodes);
|
||||
|
||||
String url = csUrl + "/jsm/v2/jobs/submit";
|
||||
String res = HttpUtils.sendBodyPostWithToken(url, params, csAuthInfo.getToken());
|
||||
JSONObject resData = returnResDataWithOK(res);
|
||||
|
|
@ -191,12 +189,13 @@ public class CSCollectServiceImpl implements CSCollectService {
|
|||
if (fileValue instanceof JSONObject) {
|
||||
JSONObject fileObj = (JSONObject) fileValue;
|
||||
// 提取 file_id 和 filename
|
||||
Integer objectId = fileObj.getInt("file_id");
|
||||
Integer packageId = fileObj.getInt("file_id");
|
||||
String filename = fileObj.getStr("filename");
|
||||
|
||||
// 构建 binding 元素
|
||||
Map<String, Object> binding = new HashMap<>();
|
||||
binding.put("paramName", fieldName);
|
||||
binding.put("objectID", objectId);
|
||||
binding.put("filename", filename);
|
||||
binding.put("packageID", packageId);
|
||||
bindingFiles.add(binding);
|
||||
}
|
||||
}
|
||||
|
|
@ -246,11 +245,12 @@ public class CSCollectServiceImpl implements CSCollectService {
|
|||
if (hpcBindingFiles != null) {
|
||||
for (Map<String, Object> fileInfo : hpcBindingFiles) {
|
||||
JSONObject binding = new JSONObject();
|
||||
binding.set("paramName", fileInfo.get("paramName"));
|
||||
binding.set("paramName", "inputFile");
|
||||
|
||||
JSONObject resource = new JSONObject();
|
||||
resource.set("type", "object");
|
||||
resource.set("objectID", fileInfo.get("objectID"));
|
||||
resource.set("type", "path");
|
||||
resource.set("path", "/"+fileInfo.get("filename"));
|
||||
resource.set("packageID", fileInfo.get("packageID"));
|
||||
|
||||
binding.set("resource", resource);
|
||||
bindingArray.set(binding);
|
||||
|
|
@ -419,16 +419,17 @@ public class CSCollectServiceImpl implements CSCollectService {
|
|||
/**
|
||||
* 所有文件打包上传
|
||||
*
|
||||
* @param upload
|
||||
* @param inputs
|
||||
* @param output
|
||||
* @param zipFilePath
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public void allFileUpload(HashMap<String, String> upload, String zipFilePath) throws Exception {
|
||||
public void allFileUpload(List<String> inputs, String output, String zipFilePath) throws Exception {
|
||||
// 异步提交任务
|
||||
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
minioUtil.createZipFromDirectory(bucketName, upload, zipFilePath);
|
||||
minioUtil.createZipFromDirectory(bucketName, inputs,output, zipFilePath);
|
||||
return "执行成功";
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("异步提交任务失败", e);
|
||||
|
|
@ -436,7 +437,6 @@ public class CSCollectServiceImpl implements CSCollectService {
|
|||
});
|
||||
// 处理异步结果
|
||||
future.thenAcceptAsync(result -> {
|
||||
|
||||
log.info("所有文件打包上传成功,result: {}", result);
|
||||
}).exceptionally(ex -> {
|
||||
log.error("异步处理任务失败", ex);
|
||||
|
|
|
|||
|
|
@ -187,11 +187,11 @@ public class CSServiceVersionServiceImpl implements ServiceVersionService {
|
|||
ServiceVersion serviceVersion = csServiceDao.getServiceVersionById(serviceVersionId);
|
||||
com.ruoyi.platform.domain.service.Service service = serviceDao.getServiceById(serviceVersion.getServiceId());
|
||||
ServiceTemp serviceTemp = serviceTempService.getServiceTemp(service.getServiceTempId());
|
||||
Integer objectID = csCollectService.uploadFile(serviceTemp.getName(), file);
|
||||
Integer packageId = csCollectService.uploadFile(serviceTemp.getName(), file);
|
||||
String savePath = "/mini-model-platform-data/cs-data/upload/"+serviceVersionId+ "/" + UUID.fastUUID()+"/"+file.getOriginalFilename();
|
||||
minioService.uploadFile(bucketName, savePath, file);
|
||||
HashMap<String, Object> result = MapUtil.newHashMap();
|
||||
result.put("file_id", objectID);
|
||||
result.put("file_id", packageId);
|
||||
result.put("direct_url", savePath);
|
||||
result.put("filename", file.getOriginalFilename());
|
||||
return result;
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
package com.ruoyi.platform.utils;
|
||||
|
||||
|
||||
import com.ruoyi.common.core.utils.StringUtils;
|
||||
import io.minio.*;
|
||||
import io.minio.errors.MinioException;
|
||||
import io.minio.errors.*;
|
||||
import io.minio.http.Method;
|
||||
import io.minio.messages.DeleteObject;
|
||||
import io.minio.messages.Item;
|
||||
|
|
@ -16,6 +17,7 @@ import org.springframework.web.multipart.MultipartFile;
|
|||
import java.io.*;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.*;
|
||||
|
|
@ -516,43 +518,69 @@ public class MinioUtil {
|
|||
/**
|
||||
* 创建目录的压缩包
|
||||
*/
|
||||
public void createZipFromDirectory(String bucket, Map<String,String> dirMap, String zipPath)
|
||||
public void createZipFromDirectory(String bucket, List<String> inputs,String output, String zipPath)
|
||||
throws Exception {
|
||||
|
||||
// 创建内存中的ZIP流
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
Path tempFile = Files.createTempFile("zip-", ".tmp");
|
||||
try {
|
||||
// 写入临时文件
|
||||
try (FileOutputStream fos = new FileOutputStream(tempFile.toFile());
|
||||
ZipOutputStream zos = new ZipOutputStream(fos)) {
|
||||
|
||||
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
|
||||
for (String input : inputs) {
|
||||
if (StringUtils.isNotBlank(input)) {
|
||||
writeBuffer("input", input, bucket, zos);
|
||||
}
|
||||
}
|
||||
writeBuffer("output", output, bucket, zos);
|
||||
}
|
||||
|
||||
// 遍历每个目录映射
|
||||
for (Map.Entry<String, String> entry : dirMap.entrySet()) {
|
||||
String folderName = entry.getKey(); // 压缩包内的文件夹名
|
||||
String sourcePath = entry.getValue(); // MinIO中的路径
|
||||
// 上传临时文件
|
||||
try (InputStream is = Files.newInputStream(tempFile)) {
|
||||
minioClient.putObject(
|
||||
PutObjectArgs.builder()
|
||||
.bucket(bucket)
|
||||
.object(zipPath)
|
||||
.stream(is, Files.size(tempFile), -1)
|
||||
.contentType("application/zip")
|
||||
.build()
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
// 清理临时文件
|
||||
Files.deleteIfExists(tempFile);
|
||||
}
|
||||
}
|
||||
|
||||
// 1. 自动提取目录路径(去除文件名)
|
||||
String sourceDir = extractDirectoryPath(sourcePath);
|
||||
/**
|
||||
*
|
||||
* @param folderName 压缩包内的文件夹名
|
||||
* @param sourcePath MinIO中的路径
|
||||
*/
|
||||
private void writeBuffer(String folderName,String sourcePath,String bucket,ZipOutputStream zos) {
|
||||
try {
|
||||
|
||||
// 2. 规范化文件夹名
|
||||
// 1. 规范化文件夹名
|
||||
if (!folderName.endsWith("/")) {
|
||||
folderName = folderName + "/";
|
||||
}
|
||||
|
||||
// 3. 获取目录下的所有文件
|
||||
// 2. 获取目录下的所有文件
|
||||
Iterable<Result<Item>> items = minioClient.listObjects(
|
||||
ListObjectsArgs.builder()
|
||||
.bucket(bucket)
|
||||
.prefix(sourceDir)
|
||||
.prefix(sourcePath)
|
||||
.recursive(true)
|
||||
.build()
|
||||
);
|
||||
|
||||
// 4. 添加文件到ZIP
|
||||
// 3. 添加文件到ZIP
|
||||
for (Result<Item> result : items) {
|
||||
Item item = result.get();
|
||||
if (item.isDir()) continue;
|
||||
|
||||
String objectName = item.objectName();
|
||||
String relativePath = objectName.substring(sourceDir.length()-1);
|
||||
String relativePath = objectName.substring(sourcePath.length() - 1);
|
||||
|
||||
// 使用自定义的文件夹名
|
||||
String zipEntryName = folderName + relativePath;
|
||||
|
|
@ -576,48 +604,8 @@ public class MinioUtil {
|
|||
|
||||
zos.closeEntry();
|
||||
}
|
||||
}
|
||||
|
||||
zos.finish();
|
||||
}
|
||||
|
||||
// 上传压缩包到MinIO
|
||||
byte[] zipBytes = baos.toByteArray();
|
||||
|
||||
try (ByteArrayInputStream bais = new ByteArrayInputStream(zipBytes)) {
|
||||
minioClient.putObject(
|
||||
PutObjectArgs.builder()
|
||||
.bucket(bucket)
|
||||
.object(zipPath)
|
||||
.stream(bais, zipBytes.length, -1)
|
||||
.contentType("application/zip")
|
||||
.build()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private String extractDirectoryPath(String path) {
|
||||
path = path.trim();
|
||||
|
||||
// 如果路径包含点号且不以斜杠结尾,可能是文件
|
||||
if (path.contains(".") && !path.endsWith("/")) {
|
||||
int lastSlash = path.lastIndexOf("/");
|
||||
if (lastSlash > 0) {
|
||||
// 返回目录部分
|
||||
return path.substring(0, lastSlash + 1);
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// 如果不是文件路径,直接返回
|
||||
if (!path.endsWith("/")) {
|
||||
return path + "/";
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String objectName = "mini-model-platform-data/cs-data/98/28bbb298-6e21-4361-b00a-ba8dc1643f65/file/log.lammps";
|
||||
String relativePath = objectName.substring("/mini-model-platform-data/cs-data/98/28bbb298-6e21-4361-b00a-ba8dc1643f65/file/".length()-1);
|
||||
System.out.println(relativePath); //输出og.lammps
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue