Merge remote-tracking branch 'origin/main'
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package com.sdm.common.entity.req.pbs;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class HpcTaskFileReq {
|
||||
|
||||
@Schema(description = "任务ID,和targetDir互斥")
|
||||
public String jobId;
|
||||
|
||||
@Schema(description = "指定目录,和jobId互斥")
|
||||
public String targetDir;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.sdm.common.entity.resp.pbs.hpc;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class FileNodeInfo {
|
||||
|
||||
/** 名称 */
|
||||
private String name;
|
||||
|
||||
/** 完整路径 */
|
||||
private String fullPath;
|
||||
|
||||
/** 是否文件夹 1是文件夹 0 文件 */
|
||||
private String directory;
|
||||
|
||||
/** 文件大小(字节,文件夹为 0) */
|
||||
private long size;
|
||||
|
||||
/** 最后修改时间 */
|
||||
private String lastModifiedTime;
|
||||
}
|
||||
@@ -2,29 +2,54 @@ package com.sdm.common.utils;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.TypeReference;
|
||||
import com.sdm.common.entity.resp.pbs.hpc.FileNodeInfo;
|
||||
import com.sdm.common.log.CoreLogger;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class HpcCommandExcuteUtil {
|
||||
|
||||
@Value("${hpc.excuteWay:}")
|
||||
private String hpcExcuteWay;
|
||||
|
||||
@Value("${hpc.remoteCmdUrl:}")
|
||||
private String remoteCmdUrl;
|
||||
|
||||
@Value("${hpc.remoteCreateDirUrl:}")
|
||||
private String remoteCreateDirUrl;
|
||||
|
||||
@Value("${hpc.remoteScanDirUrl:}")
|
||||
private String remoteScanDirUrl;
|
||||
|
||||
@Value("${hpc.remoteDownLoadFileUrl:}")
|
||||
private String remoteDownLoadFileUrl;
|
||||
|
||||
@Autowired
|
||||
private HttpClientUtil httpClientUtil;
|
||||
|
||||
public String excuteCmd(String command,String hpcExcuteWay) {
|
||||
@Autowired
|
||||
private WebClient webClient;
|
||||
|
||||
public String excuteCmd(String command) {
|
||||
if(Objects.equals(hpcExcuteWay,"remote")){
|
||||
return remoteExcuteCmd(command);
|
||||
}
|
||||
@@ -83,4 +108,67 @@ public class HpcCommandExcuteUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public List<FileNodeInfo> scanDir(String targetDir) {
|
||||
List<FileNodeInfo> nodeInfos = new ArrayList<>();
|
||||
if(Objects.equals(hpcExcuteWay,"remote")){
|
||||
com.alibaba.fastjson2.JSONObject paramJson = new com.alibaba.fastjson2.JSONObject();
|
||||
paramJson.put("folderPath", targetDir);
|
||||
String resultString = "";
|
||||
try {
|
||||
resultString = httpClientUtil.doPostJson(remoteScanDirUrl, paramJson.toJSONString());
|
||||
CoreLogger.info("scanDir back:{}", resultString);
|
||||
nodeInfos = JSON.parseObject(
|
||||
resultString,
|
||||
new TypeReference<List<FileNodeInfo>>() {
|
||||
}
|
||||
);
|
||||
return nodeInfos;
|
||||
} catch (Exception e) {
|
||||
CoreLogger.error("scanDir error,targetDir:{},errMsg:{}", targetDir, e.getMessage());
|
||||
return nodeInfos;
|
||||
}
|
||||
}
|
||||
// 非remote,本地的暂时用不到
|
||||
return nodeInfos;
|
||||
}
|
||||
|
||||
public ResponseEntity<StreamingResponseBody> hpcDownloadFile(String path, Long fileSize) {
|
||||
// 从 path 中提取文件名
|
||||
String fileName = extractFileName(path);
|
||||
String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
|
||||
StreamingResponseBody body = outputStream -> {
|
||||
DataBufferUtils.write(
|
||||
webClient.get()
|
||||
.uri(remoteDownLoadFileUrl, path)
|
||||
.retrieve()
|
||||
.bodyToFlux(DataBuffer.class),
|
||||
Channels.newChannel(outputStream)
|
||||
).blockLast(); // 阻塞直到写完
|
||||
};
|
||||
|
||||
// 构建 ResponseEntity
|
||||
ResponseEntity.BodyBuilder builder = ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION,
|
||||
"attachment; filename*=UTF-8''" + encodedFileName)
|
||||
.contentType(MediaType.APPLICATION_OCTET_STREAM);
|
||||
|
||||
// 只有在 fileSize 合法时才设置 Content-Length
|
||||
if (fileSize != null && fileSize > 0) {
|
||||
builder.contentLength(fileSize);
|
||||
}
|
||||
return builder.body(body);
|
||||
}
|
||||
|
||||
private String extractFileName(String path) {
|
||||
if (path == null || path.isBlank()) {
|
||||
return "unknown";
|
||||
}
|
||||
// 同时兼容 / 和 \
|
||||
path = path.replace("\\", "/");
|
||||
|
||||
int lastSlash = path.lastIndexOf('/');
|
||||
return lastSlash >= 0 ? path.substring(lastSlash + 1) : path;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user