add listYPKs & deleteFile

This commit is contained in:
yanghuanyu 2021-11-17 12:59:20 +08:00
parent 91080096ce
commit c69bfa6620
4 changed files with 105 additions and 13 deletions

View File

@ -15,6 +15,7 @@ import org.bdware.sc.conn.ResultCallback;
import org.bdware.sc.db.CMTables;
import org.bdware.sc.db.KeyValueDBUtil;
import org.bdware.sc.db.TimeDBUtil;
import org.bdware.sc.util.FileUtil;
import org.bdware.sc.util.JsonUtil;
import org.bdware.server.action.Action;
import org.bdware.server.http.HttpMethod;
@ -32,15 +33,15 @@ import static io.netty.handler.codec.http.HttpResponseStatus.OK;
public class FileActions {
private static final Logger LOGGER = LogManager.getLogger(FileActions.class);
private static final Set<String> TEXT_FILE_SUFFIXES = new HashSet<>();
public NodeCenterFrameHandler controller;
static Map<String, FileOutputStream> fileMap = new HashMap<>();
static Map<String, Long> updateTime = new HashMap<>();
public FileActions(NodeCenterFrameHandler nodeCenterFrameHandler) {
controller = nodeCenterFrameHandler;
static NodeCenterWSFrameHandler handler;
public FileActions(NodeCenterWSFrameHandler nodeCenterWSFrameHandler) {
handler = nodeCenterWSFrameHandler;
}
static NodeCenterWSFrameHandler handler;
public static boolean isLocked(String pubKey) {
String ret = KeyValueDBUtil.instance.getValue(CMTables.LockedUser.toString(), pubKey);
@ -102,7 +103,7 @@ public class FileActions {
// logger.info("toVerify " + str);
boolean verify = SM2Util.plainStrVerify(pubkey, str, sign);
LOGGER.info("[CMHttpHandler] upload http请求验签结果 : " + verify);
LOGGER.info("[CMHttpHandler] upload http请求验签结果 : " + verify);
if (verify) {
// 查permission
@ -392,4 +393,92 @@ public class FileActions {
private static boolean isTextFile(String path) {
return TEXT_FILE_SUFFIXES.contains(path.substring(path.lastIndexOf(".")));
}
@Action(userPermission = 0)
public void listYPKs(JsonObject args, ResultCallback resultCallback) {
Response response = new Response();
response.action = "onListYPKs";
response.data = "[]";
response.responseID = args.get("requestID").getAsString();
ListYPKsResp resp = new ListYPKsResp();
String dbDir = "./NodeCenterDB";
String project = "NC_YPKs";
File f = new File(dbDir, project);
if (!project.contains("..") && f.exists()) {
resp.isDir = f.isDirectory();
if (resp.isDir) {
resp.subFiles = new ArrayList<>();
for (File subFile : f.listFiles()) {
resp.subFiles.add(createFileItem(subFile));
}
resp.subFiles.sort(Comparator.comparing(o -> o.name));
} else {
if (isTextFile(f.getName())) {
resp.val = FileUtil.getFileContent(f.getAbsolutePath());
} else {
resp.val = "unknown file type, length:" + f.length();
}
}
response.data = JsonUtil.toJson(resp);
}
resultCallback.onResult(JsonUtil.toJson(response));
}
@Action(userPermission = 0)
public void deleteFile(JsonObject args, ResultCallback resultCallback) {
Response response = new Response();
response.action = "onDeleteFile";
response.data = "failed";
response.responseID = args.get("requestID").getAsString();
boolean compiled = false, isPrivate = false;
String parPath = "./NodeCenterDB/NC_YPKs";
try {
String oldFile = args.get("file").getAsString();
File f = new File(parPath + "/" + oldFile);
if (!oldFile.contains("..") && f.exists()) {
LOGGER.debug(
"[FileController] delete:"
+ f.getAbsolutePath()
+ " exists:"
+ f.exists());
f.delete();
}
response.data = "success";
} catch (Exception e) {
e.printStackTrace();
}
resultCallback.onResult(JsonUtil.toJson(response));
}
private FileItem createFileItem(File subFile) {
FileItem item = new FileItem();
item.name = subFile.getName();
if (subFile.isDirectory()) {
item.subFiles = new ArrayList<>();
for (File f : subFile.listFiles()) {
item.subFiles.add(createFileItem(f));
}
}
return item;
}
static class ListYPKsResp {
public String path;
boolean isDir;
String val;
List<FileItem> subFiles;
}
static class FileItem {
String name;
List<FileItem> subFiles;
}
}

View File

@ -317,7 +317,7 @@ public class NodeCenterActions {
return Role.Node.getValue();
*/
// TODO fix permission bugs.
return Role.compoundValue(new String[] {"NodeManager"});
return Role.compoundValue(new String[]{"NodeManager"});
} else {
return (Role.compoundValue(ret.split(",")));
@ -477,7 +477,8 @@ public class NodeCenterActions {
List<ContractDesp> contracts =
JsonUtil.fromJson(
json.get("contracts"),
new TypeToken<List<ContractDesp>>() {}.getType());
new TypeToken<List<ContractDesp>>() {
}.getType());
MetaIndexAction.updateContractsIndex(contracts, rc);
LOGGER.debug("update contracts: " + json.get("contracts"));
int version = -1;
@ -730,7 +731,8 @@ public class NodeCenterActions {
JsonUtil.fromJson(
ret.result,
new TypeToken<
List<Map<String, String>>>() {}.getType());
List<Map<String, String>>>() {
}.getType());
converted.put("result", je);
} catch (Exception e) {
converted.put("result", ret.result);
@ -1126,7 +1128,7 @@ public class NodeCenterActions {
File project = new File(filePath);
String projectName = project.getName().split("_NC")[0];
LOGGER.debug("[JJJJJJJ] " + projectName);
LOGGER.debug("[sendProject] " + projectName);
Map<String, String> req = new HashMap<>();
req.put("action", "receiveProject");
req.put("isPrivate", isPrivate);

View File

@ -27,18 +27,16 @@ public class NodeCenterFrameHandler extends SimpleChannelInboundHandler<Object>
public String pubKey;
NodeCenterActions actions;
MasterActions masterActions;
FileActions fileActions;
ActionExecutor<ResultCallback, JsonObject> ae;
ChannelHandlerContext ctx;
public NodeCenterFrameHandler() {
actions = new NodeCenterActions(this);
fileActions = new FileActions(this);
MetaIndexAction.controller = this;
// TODO 添加那个UnitAction.
ae =
new ActionExecutor<ResultCallback, JsonObject>(
executorService, actions, fileActions, new MetaIndexAction()) {
executorService, actions, new MetaIndexAction()) {
@Override
public boolean checkPermission(
Action a, final JsonObject args, long permission) {

View File

@ -29,14 +29,17 @@ public class NodeCenterWSFrameHandler extends SimpleChannelInboundHandler<WebSoc
private NCManagerAction managerAction;
private LogActions logActions;
private UnitActions unitActions;
public FileActions fileActions;
public NodeCenterWSFrameHandler() {
managerAction = new NCManagerAction(this);
logActions = new LogActions(managerAction);
unitActions = new UnitActions(managerAction);
fileActions = new FileActions(this);
ae =
new ActionExecutor<ResultCallback, JsonObject>(
executorService, managerAction, logActions, unitActions, new MetaIndexAction(), new TracingAction()) {
executorService, managerAction, logActions, unitActions, fileActions, new MetaIndexAction(), new TracingAction()) {
@Override
public boolean checkPermission(Action a, JsonObject arg, long permission) {
// Permission userPermission = a.userPermission();