🐛 windows 移动文件失败.

This commit is contained in:
lijiahang
2024-07-22 10:27:21 +08:00
parent a71456b209
commit 0664eff151
6 changed files with 50 additions and 10 deletions

View File

@@ -100,7 +100,7 @@ public abstract class AbstractTerminalHandler<T extends TerminalBasePayload> imp
if (ex == null) {
return null;
}
if (ex instanceof InvalidArgumentException) {
if (ex instanceof InvalidArgumentException || ex instanceof IllegalArgumentException) {
return ex.getMessage();
}
return ErrorMessage.OPERATE_ERROR;

View File

@@ -12,6 +12,7 @@ import com.orion.visor.framework.common.constant.Const;
import com.orion.visor.framework.common.utils.Valid;
import com.orion.visor.module.asset.handler.host.terminal.model.TerminalConfig;
import com.orion.visor.module.asset.handler.host.terminal.model.response.SftpFileVO;
import com.orion.visor.module.asset.utils.SftpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.socket.WebSocketSession;
@@ -82,7 +83,10 @@ public class SftpSession extends TerminalSession implements ISftpSession {
@Override
public void move(String source, String target) {
source = Valid.checkNormalize(source);
executor.move(source, target);
// 移动
SftpUtils.move(executor, source, target);
// FIXME kit
// executor.move(source, target);
}
@Override

View File

@@ -62,7 +62,7 @@ public class TransferUtils {
public static String getErrorMessage(Exception ex) {
if (ex == null) {
return null;
} else if (ex instanceof InvalidArgumentException) {
} else if (ex instanceof InvalidArgumentException || ex instanceof IllegalArgumentException) {
String message = ex.getMessage();
if (Strings.isBlank(message)) {
return ErrorMessage.OPERATE_ERROR;

View File

@@ -126,7 +126,7 @@ public class ExecCommandServiceImpl implements ExecCommandService {
.sourceId(request.getSourceId())
.execSeq(request.getExecSeq())
.description(Strings.ifBlank(request.getDescription(), () -> {
if (command.length() < DESC_OMIT + 3) {
if (command.length() < DESC_OMIT + Const.OMIT.length()) {
return command;
} else {
return command.substring(0, DESC_OMIT) + Const.OMIT;

View File

@@ -422,7 +422,7 @@ public class ExecLogServiceImpl implements ExecLogService {
log.error("ExecLogService.downloadLogFile error id: {}", id, e);
Streams.close(in);
String errorMessage = ErrorMessage.FILE_READ_ERROR_CLEAR;
if (e instanceof InvalidArgumentException) {
if (e instanceof InvalidArgumentException || e instanceof IllegalArgumentException) {
errorMessage = e.getMessage();
}
// 响应错误信息

View File

@@ -1,8 +1,8 @@
package com.orion.visor.module.asset.utils;
import com.alibaba.fastjson.JSON;
import com.jcraft.jsch.SftpException;
import com.orion.lang.utils.Booleans;
import com.orion.lang.utils.Exceptions;
import com.orion.lang.utils.Strings;
import com.orion.lang.utils.io.Files1;
import com.orion.net.host.sftp.SftpExecutor;
@@ -41,13 +41,49 @@ public class SftpUtils {
SftpFileBackupParams backupParams = new SftpFileBackupParams(file.getName(), System.currentTimeMillis());
String target = Strings.format(config.getBackupFileName(), JSON.parseObject(JSON.toJSONString(backupParams)));
// 移动
try {
executor.getChannel().rename(path, Files1.getPath(Files1.normalize(Files1.getPath(path + "/../" + target))));
} catch (SftpException ignored) {
}
// FIXME kit
move(executor, path, target);
// executor.move(path, target);
}
}
/**
* 移动文件
* FIXME kit DELETE
*
* @param executor executor
* @param source source
* @param target target
*/
public static void move(SftpExecutor executor, String source, String target) {
try {
source = Files1.getPath(source);
target = Files1.getPath(target);
if (target.charAt(0) == '/') {
// 检查是否需要创建目标文件目录
if (!isSameParentPath(source, target)) {
executor.makeDirectories(Files1.getParentPath(target));
}
// 绝对路径
executor.getChannel().rename(source, Files1.getPath(Files1.normalize(target)));
} else {
// 相对路径
executor.getChannel().rename(source, Files1.getPath(Files1.normalize(Files1.getPath(source + "/../" + target))));
}
} catch (Exception e) {
throw Exceptions.sftp(e);
}
}
/**
* FIXME kit DELETE
*
* @param source source
* @param target target
* @return res
*/
private static boolean isSameParentPath(String source, String target) {
return Files1.getParentPath(source).equals(Files1.getParentPath(target));
}
}