Commit 06cb5707 authored by Administrator's avatar Administrator

add FileExplorer

parent bee54ef3
package com.virjar.ratel.api.extension.superappium.sekiro;
import android.util.Log;
import com.virjar.ratel.api.RatelToolKit;
import com.virjar.ratel.api.extension.superappium.SuperAppium;
import com.virjar.sekiro.api.SekiroRequest;
import com.virjar.sekiro.api.SekiroRequestHandler;
import com.virjar.sekiro.api.SekiroResponse;
import com.virjar.sekiro.api.databind.AutoBind;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import external.org.apache.commons.lang3.StringUtils;
public class FileExploreHandler implements SekiroRequestHandler {
@AutoBind(defaultStringValue = "/")
private String path;
@AutoBind(defaultStringValue = "get")
private String op;
@Override
public void handleRequest(SekiroRequest sekiroRequest, SekiroResponse sekiroResponse) {
if (StringUtils.isBlank(path)) {
path = "/";
}
String base = RatelToolKit.sContext.getFilesDir().getParentFile().getAbsolutePath();
if (path.startsWith(base)) {
path = path.substring(base.length());
}
File targetFile = new File(RatelToolKit.sContext.getFilesDir().getParent());
if (!"/".equals(path)) {
targetFile = new File(RatelToolKit.sContext.getFilesDir().getParentFile(), path);
}
if ("get".equalsIgnoreCase(op)) {
if (!targetFile.exists()) {
sekiroResponse.failed(404, "the file :" + targetFile.getAbsolutePath() + " not exist");
return;
}
handleGet(sekiroRequest, sekiroResponse, targetFile);
} else if ("post".equalsIgnoreCase(op)) {
if (!targetFile.exists()) {
sekiroResponse.failed(404, "the file :" + targetFile.getAbsolutePath() + " not exist");
return;
}
handlePost(sekiroRequest, sekiroResponse, targetFile);
} else if ("put".equalsIgnoreCase(op)) {
handlePut(sekiroRequest, sekiroResponse, targetFile);
}
}
private void handleGet(SekiroRequest sekiroRequest, SekiroResponse sekiroResponse, File targetFile) {
if (targetFile.isDirectory()) {
List<String> ret = new ArrayList<>();
String[] list = targetFile.list();
if (list == null) {
sekiroResponse.failed("can not read dir file: " + targetFile.getAbsolutePath());
return;
}
String base = RatelToolKit.sContext.getFilesDir().getParentFile().getAbsolutePath();
for (String str : list) {
if (str.startsWith(base)) {
ret.add(str.substring(base.length()));
} else {
ret.add(str);
}
}
sekiroResponse.success(ret);
return;
}
if (!targetFile.canRead()) {
sekiroResponse.failed("can not read file: " + targetFile.getAbsolutePath());
return;
}
try {
sekiroResponse.sendFile(targetFile);
} catch (IOException e) {
Log.e(SuperAppium.TAG, "read filed failed", e);
sekiroResponse.failed("can not read file: " + targetFile.getAbsolutePath());
}
}
private void handlePost(SekiroRequest sekiroRequest, SekiroResponse sekiroResponse, File targetFile) {
sekiroResponse.failed("not implement now");
}
private void handlePut(SekiroRequest sekiroRequest, SekiroResponse sekiroResponse, File targetFile) {
sekiroResponse.failed("not implement now");
}
}
......@@ -14,20 +14,25 @@ public class SekiroStarter {
private static final String dumpTopFragment = "dumpFragment";
private static final String screenShot = "screenShot";
private static final String executeJsOnWebView = "ExecuteJsOnWebView";
private static final String fileExplore = "fileExplore";
private static boolean isStarted = false;
private static SekiroClient defaultSekiroClient = null;
public static void startService(String host, int port, String token) {
if (isStarted) {
return;
}
Log.i(SuperAppium.TAG, "start a supperAppium client: " + token);
PageTriggerManager.getTopFragment("insureComponentStarted");
SekiroClient.start(host, port, token, sekiroGroup)
defaultSekiroClient = SekiroClient.start(host, port, token, sekiroGroup)
.registerHandler(dumpTopActivity, new DumpTopActivityHandler())
.registerHandler(dumpTopFragment, new DumpTopFragmentHandler())
.registerHandler(screenShot, new ScreenShotHandler())
.registerHandler(executeJsOnWebView, new ExecuteJsOnWebViewHandler())
.registerHandler(fileExplore, new FileExploreHandler())
;
isStarted = true;
......@@ -36,4 +41,8 @@ public class SekiroStarter {
public static void startService(String host, int port) {
startService(host, port, UUID.randomUUID().toString());
}
public static SekiroClient getDefaultSekiroClient() {
return defaultSekiroClient;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment