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.CMTables;
import org.bdware.sc.db.KeyValueDBUtil; import org.bdware.sc.db.KeyValueDBUtil;
import org.bdware.sc.db.TimeDBUtil; import org.bdware.sc.db.TimeDBUtil;
import org.bdware.sc.util.FileUtil;
import org.bdware.sc.util.JsonUtil; import org.bdware.sc.util.JsonUtil;
import org.bdware.server.action.Action; import org.bdware.server.action.Action;
import org.bdware.server.http.HttpMethod; import org.bdware.server.http.HttpMethod;
@ -32,15 +33,15 @@ import static io.netty.handler.codec.http.HttpResponseStatus.OK;
public class FileActions { public class FileActions {
private static final Logger LOGGER = LogManager.getLogger(FileActions.class); private static final Logger LOGGER = LogManager.getLogger(FileActions.class);
private static final Set<String> TEXT_FILE_SUFFIXES = new HashSet<>(); private static final Set<String> TEXT_FILE_SUFFIXES = new HashSet<>();
public NodeCenterFrameHandler controller;
static Map<String, FileOutputStream> fileMap = new HashMap<>(); static Map<String, FileOutputStream> fileMap = new HashMap<>();
static Map<String, Long> updateTime = new HashMap<>(); static Map<String, Long> updateTime = new HashMap<>();
public FileActions(NodeCenterFrameHandler nodeCenterFrameHandler) { static NodeCenterWSFrameHandler handler;
controller = nodeCenterFrameHandler;
public FileActions(NodeCenterWSFrameHandler nodeCenterWSFrameHandler) {
handler = nodeCenterWSFrameHandler;
} }
static NodeCenterWSFrameHandler handler;
public static boolean isLocked(String pubKey) { public static boolean isLocked(String pubKey) {
String ret = KeyValueDBUtil.instance.getValue(CMTables.LockedUser.toString(), pubKey); String ret = KeyValueDBUtil.instance.getValue(CMTables.LockedUser.toString(), pubKey);
@ -392,4 +393,92 @@ public class FileActions {
private static boolean isTextFile(String path) { private static boolean isTextFile(String path) {
return TEXT_FILE_SUFFIXES.contains(path.substring(path.lastIndexOf("."))); 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

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

View File

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

View File

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