sftp 文件操作实现.

This commit is contained in:
lijiahang
2024-02-19 18:14:16 +08:00
parent a7c9d0b468
commit a2ff7b0076
11 changed files with 155 additions and 4 deletions

View File

@@ -1,6 +1,10 @@
package com.orion.ops.module.asset.handler.host.terminal.handler;
import com.orion.ops.framework.common.enums.BooleanBit;
import com.orion.ops.module.asset.handler.host.terminal.enums.OutputTypeEnum;
import com.orion.ops.module.asset.handler.host.terminal.model.request.SftpChangeModRequest;
import com.orion.ops.module.asset.handler.host.terminal.model.response.SftpBaseResponse;
import com.orion.ops.module.asset.handler.host.terminal.session.ISftpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;
@@ -18,6 +22,27 @@ public class SftpChangeModHandler extends AbstractTerminalHandler<SftpChangeModR
@Override
public void handle(WebSocketSession channel, SftpChangeModRequest payload) {
// 获取会话
ISftpSession session = terminalManager.getSession(channel.getId(), payload.getSessionId());
String path = payload.getPath();
Integer mod = payload.getMod();
log.info("SftpChangeModHandler-handle session: {}, path: {}, mod: {}", payload.getSessionId(), path, mod);
Exception ex = null;
// 修改权限
try {
session.chmod(path, mod);
} catch (Exception e) {
log.error("SftpChangeModHandler-handle error", e);
ex = e;
}
// 返回
this.send(channel,
OutputTypeEnum.SFTP_CHMOD,
SftpBaseResponse.builder()
.sessionId(payload.getSessionId())
.result(BooleanBit.of(ex == null).getValue())
.msg(ex == null ? null : ex.getMessage())
.build());
}
}

View File

@@ -1,6 +1,10 @@
package com.orion.ops.module.asset.handler.host.terminal.handler;
import com.orion.ops.framework.common.enums.BooleanBit;
import com.orion.ops.module.asset.handler.host.terminal.enums.OutputTypeEnum;
import com.orion.ops.module.asset.handler.host.terminal.model.request.SftpBaseRequest;
import com.orion.ops.module.asset.handler.host.terminal.model.response.SftpGetContentResponse;
import com.orion.ops.module.asset.handler.host.terminal.session.ISftpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;
@@ -18,6 +22,28 @@ public class SftpGetContentHandler extends AbstractTerminalHandler<SftpBaseReque
@Override
public void handle(WebSocketSession channel, SftpBaseRequest payload) {
// 获取会话
ISftpSession session = terminalManager.getSession(channel.getId(), payload.getSessionId());
String path = payload.getPath();
log.info("SftpGetContentHandler-handle session: {}, path: {}", payload.getSessionId(), path);
String content = null;
Exception ex = null;
// 获取内容
try {
content = session.getContent(path);
} catch (Exception e) {
log.error("SftpGetContentHandler-handle error", e);
ex = e;
}
// 返回
this.send(channel,
OutputTypeEnum.SFTP_GET_CONTENT,
SftpGetContentResponse.builder()
.sessionId(payload.getSessionId())
.result(BooleanBit.of(ex == null).getValue())
.content(content)
.msg(ex == null ? null : ex.getMessage())
.build());
}
}

View File

@@ -27,6 +27,7 @@ public class SftpMakeDirectoryHandler extends AbstractTerminalHandler<SftpBaseRe
String path = payload.getPath();
log.info("SftpMakeDirectoryHandler-handle session: {}, path: {}", payload.getSessionId(), path);
Exception ex = null;
// 创建文件夹
try {
session.mkdir(path);
} catch (Exception e) {

View File

@@ -1,6 +1,10 @@
package com.orion.ops.module.asset.handler.host.terminal.handler;
import com.orion.ops.framework.common.enums.BooleanBit;
import com.orion.ops.module.asset.handler.host.terminal.enums.OutputTypeEnum;
import com.orion.ops.module.asset.handler.host.terminal.model.request.SftpMoveRequest;
import com.orion.ops.module.asset.handler.host.terminal.model.response.SftpBaseResponse;
import com.orion.ops.module.asset.handler.host.terminal.session.ISftpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;
@@ -18,6 +22,27 @@ public class SftpMoveHandler extends AbstractTerminalHandler<SftpMoveRequest> {
@Override
public void handle(WebSocketSession channel, SftpMoveRequest payload) {
// 获取会话
ISftpSession session = terminalManager.getSession(channel.getId(), payload.getSessionId());
String path = payload.getPath();
String target = payload.getTarget();
log.info("SftpMoveHandler-handle session: {}, path: {}, target: {}", payload.getSessionId(), path, target);
Exception ex = null;
// 移动
try {
session.move(path, target);
} catch (Exception e) {
log.error("SftpMoveHandler-handle error", e);
ex = e;
}
// 返回
this.send(channel,
OutputTypeEnum.SFTP_MOVE,
SftpBaseResponse.builder()
.sessionId(payload.getSessionId())
.result(BooleanBit.of(ex == null).getValue())
.msg(ex == null ? null : ex.getMessage())
.build());
}
}

View File

@@ -1,6 +1,10 @@
package com.orion.ops.module.asset.handler.host.terminal.handler;
import com.orion.ops.framework.common.enums.BooleanBit;
import com.orion.ops.module.asset.handler.host.terminal.enums.OutputTypeEnum;
import com.orion.ops.module.asset.handler.host.terminal.model.request.SftpRemoveRequest;
import com.orion.ops.module.asset.handler.host.terminal.model.response.SftpBaseResponse;
import com.orion.ops.module.asset.handler.host.terminal.session.ISftpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;
@@ -18,6 +22,26 @@ public class SftpRemoveHandler extends AbstractTerminalHandler<SftpRemoveRequest
@Override
public void handle(WebSocketSession channel, SftpRemoveRequest payload) {
// 获取会话
ISftpSession session = terminalManager.getSession(channel.getId(), payload.getSessionId());
String[] paths = payload.getPaths().split("\\|");
log.info("SftpRemoveHandler-handle session: {}, paths: {}", payload.getSessionId(), paths);
Exception ex = null;
// 删除
try {
session.remove(paths);
} catch (Exception e) {
log.error("SftpRemoveHandler-handle error", e);
ex = e;
}
// 返回
this.send(channel,
OutputTypeEnum.SFTP_REMOVE,
SftpBaseResponse.builder()
.sessionId(payload.getSessionId())
.result(BooleanBit.of(ex == null).getValue())
.msg(ex == null ? null : ex.getMessage())
.build());
}
}

View File

@@ -1,6 +1,10 @@
package com.orion.ops.module.asset.handler.host.terminal.handler;
import com.orion.ops.framework.common.enums.BooleanBit;
import com.orion.ops.module.asset.handler.host.terminal.enums.OutputTypeEnum;
import com.orion.ops.module.asset.handler.host.terminal.model.request.SftpSetContentRequest;
import com.orion.ops.module.asset.handler.host.terminal.model.response.SftpBaseResponse;
import com.orion.ops.module.asset.handler.host.terminal.session.ISftpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;
@@ -18,6 +22,26 @@ public class SftpSetContentHandler extends AbstractTerminalHandler<SftpSetConten
@Override
public void handle(WebSocketSession channel, SftpSetContentRequest payload) {
// 获取会话
ISftpSession session = terminalManager.getSession(channel.getId(), payload.getSessionId());
String path = payload.getPath();
log.info("SftpSetContentHandler-handle session: {}, path: {}", payload.getSessionId(), path);
Exception ex = null;
// 修改权限
try {
session.setContent(path, payload.getContent());
} catch (Exception e) {
log.error("SftpSetContentHandler-handle error", e);
ex = e;
}
// 返回
this.send(channel,
OutputTypeEnum.SFTP_SET_CONTENT,
SftpBaseResponse.builder()
.sessionId(payload.getSessionId())
.result(BooleanBit.of(ex == null).getValue())
.msg(ex == null ? null : ex.getMessage())
.build());
}
}

View File

@@ -27,6 +27,7 @@ public class SftpTouchHandler extends AbstractTerminalHandler<SftpBaseRequest> {
String path = payload.getPath();
log.info("SftpTouchHandler-handle session: {}, path: {}", payload.getSessionId(), path);
Exception ex = null;
// 创建文件
try {
session.touch(path);
} catch (Exception e) {

View File

@@ -1,6 +1,10 @@
package com.orion.ops.module.asset.handler.host.terminal.handler;
import com.orion.ops.framework.common.enums.BooleanBit;
import com.orion.ops.module.asset.handler.host.terminal.enums.OutputTypeEnum;
import com.orion.ops.module.asset.handler.host.terminal.model.request.SftpBaseRequest;
import com.orion.ops.module.asset.handler.host.terminal.model.response.SftpBaseResponse;
import com.orion.ops.module.asset.handler.host.terminal.session.ISftpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;
@@ -18,6 +22,26 @@ public class SftpTruncateHandler extends AbstractTerminalHandler<SftpBaseRequest
@Override
public void handle(WebSocketSession channel, SftpBaseRequest payload) {
// 获取会话
ISftpSession session = terminalManager.getSession(channel.getId(), payload.getSessionId());
String path = payload.getPath();
log.info("SftpTruncateHandler-handle session: {}, path: {}", payload.getSessionId(), path);
Exception ex = null;
// 截断文件
try {
session.truncate(path);
} catch (Exception e) {
log.error("SftpTruncateHandler-handle error", e);
ex = e;
}
// 返回
this.send(channel,
OutputTypeEnum.SFTP_TRUNCATE,
SftpBaseResponse.builder()
.sessionId(payload.getSessionId())
.result(BooleanBit.of(ex == null).getValue())
.msg(ex == null ? null : ex.getMessage())
.build());
}
}

View File

@@ -27,6 +27,6 @@ import java.util.List;
public class SftpRemoveRequest extends SftpBaseRequest {
@Schema(description = "paths 多个用|分割")
private List<String> paths;
private String paths;
}

View File

@@ -61,7 +61,7 @@ public interface ISftpSession extends ITerminalSession {
*
* @param paths paths
*/
void remove(List<String> paths);
void remove(String[] paths);
/**
* 截断文件

View File

@@ -16,6 +16,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.web.socket.WebSocketSession;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@@ -89,8 +90,8 @@ public class SftpSession extends TerminalSession implements ISftpSession {
}
@Override
public void remove(List<String> paths) {
paths.stream()
public void remove(String[] paths) {
Arrays.stream(paths)
.map(Valid::checkNormalize)
.forEach(executor::remove);
}