Skip to content

Commit cd6df0e

Browse files
committed
ANDROID: file manager webui (wip)
1 parent 40b80de commit cd6df0e

File tree

4 files changed

+160
-65
lines changed

4 files changed

+160
-65
lines changed

src/platform/android/app/src/main/java/net/sourceforge/smallbasic/MainActivity.java

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import android.content.DialogInterface.OnCancelListener;
1313
import android.content.Intent;
1414
import android.content.pm.PackageManager;
15-
import android.content.res.AssetFileDescriptor;
1615
import android.content.res.Configuration;
1716
import android.content.res.Resources;
1817
import android.graphics.Rect;
@@ -99,6 +98,7 @@ public class MainActivity extends NativeActivity {
9998
private static final int REQUEST_STORAGE_PERMISSION = 1;
10099
private static final int REQUEST_LOCATION_PERMISSION = 2;
101100
private static final String FOLDER_NAME = "SmallBASIC";
101+
private static final int COPY_BUFFER_SIZE = 1024;
102102
private String _startupBas = null;
103103
private boolean _untrusted = false;
104104
private final ExecutorService _audioExecutor = Executors.newSingleThreadExecutor();
@@ -694,24 +694,23 @@ public void run() {
694694
});
695695
}
696696

697-
private void copy(File src, File dst) throws IOException {
698-
InputStream in = new FileInputStream(src);
697+
private void copy(InputStream in, OutputStream out) throws IOException {
699698
try {
700-
OutputStream out = new FileOutputStream(dst);
701-
try {
702-
byte[] buf = new byte[1024];
703-
int len;
704-
while ((len = in.read(buf)) > 0) {
705-
out.write(buf, 0, len);
706-
}
707-
} finally {
708-
out.close();
699+
byte[] buf = new byte[COPY_BUFFER_SIZE];
700+
int len;
701+
while ((len = in.read(buf)) > 0) {
702+
out.write(buf, 0, len);
709703
}
710704
} finally {
705+
out.close();
711706
in.close();
712707
}
713708
}
714709

710+
private void copy(File src, File dst) throws IOException {
711+
copy(new FileInputStream(src), new FileOutputStream(dst));
712+
}
713+
715714
private String execBuffer(final String buffer, final String name, boolean run) throws IOException {
716715
File outputFile = new File(getInternalStorage(), name);
717716
BufferedWriter output = new BufferedWriter(new FileWriter(outputFile));
@@ -952,7 +951,15 @@ protected Response getFile(String path, boolean asset) throws IOException {
952951
log("Opened " + name + " " + length + " bytes");
953952
result = new Response(getAssets().open(name), length);
954953
} else {
955-
result = null;
954+
File file = getFile(getExternalStorage(), path);
955+
if (file == null) {
956+
file = getFile(getInternalStorage(), path);
957+
}
958+
if (file != null) {
959+
result = new Response(new FileInputStream(file), file.length());
960+
} else {
961+
throw new IOException("file not found");
962+
}
956963
}
957964
return result;
958965
}
@@ -976,13 +983,46 @@ protected void log(String message) {
976983
}
977984

978985
@Override
979-
protected boolean renameFile(String from, String to) {
980-
return false;
986+
protected void renameFile(String from, String to) throws IOException {
987+
if (to == null || !to.endsWith(".bas")) {
988+
throw new IOException("Invalid New File Name: " + to);
989+
}
990+
File toFile = getFile(getInternalStorage(), to);
991+
if (toFile == null) {
992+
toFile = getFile(getExternalStorage(), to);
993+
}
994+
if (toFile != null) {
995+
throw new IOException("New File Name already exists");
996+
}
997+
File fromFile = getFile(getInternalStorage(), from);
998+
if (fromFile == null) {
999+
fromFile = getFile(getExternalStorage(), from);
1000+
}
1001+
if (fromFile == null) {
1002+
throw new IOException("Old File Name does not exist");
1003+
}
1004+
if (!fromFile.renameTo(new File(getExternalStorage(), to))) {
1005+
throw new IOException("File rename failed");
1006+
}
9811007
}
9821008

9831009
@Override
984-
protected boolean saveFile(String fileName, String content) {
985-
return false;
1010+
protected void saveFile(String fileName, byte[] content) throws IOException {
1011+
File file = new File(getExternalStorage(), fileName);
1012+
if (file.exists()) {
1013+
throw new IOException("File already exists");
1014+
} else if (file.isDirectory()) {
1015+
throw new IOException("Invalid File Name: " + fileName);
1016+
}
1017+
copy(new ByteArrayInputStream(content), new FileOutputStream(file));
1018+
}
1019+
1020+
private File getFile(String parent, String path) {
1021+
File result = new File(parent, path);
1022+
if (!result.exists() || !result.canRead() || result.isDirectory()) {
1023+
result = null;
1024+
}
1025+
return result;
9861026
}
9871027

9881028
private long getFileLength(String name) throws IOException {

src/platform/android/app/src/main/java/net/sourceforge/smallbasic/WebServer.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public void run() {
6161
protected abstract Collection<FileData> getFileData() throws IOException;
6262
protected abstract void log(String message);
6363
protected abstract void log(String message, Exception exception);
64-
protected abstract boolean renameFile(String from, String to);
65-
protected abstract boolean saveFile(String fileName, String content);
64+
protected abstract void renameFile(String from, String to) throws IOException;
65+
protected abstract void saveFile(String fileName, byte[] content) throws IOException;
6666

6767
/**
6868
* Download files button handler
@@ -118,8 +118,14 @@ private Response handleFileList() throws IOException {
118118
private Response handleRename(Map<String, String> data) throws IOException {
119119
String from = data.get("from");
120120
String to = data.get("to");
121-
boolean result = renameFile(from, to);
122-
return handleStatus(result, result ? "File renamed" : "File rename error");
121+
Response result;
122+
try {
123+
renameFile(from, to);
124+
result = handleStatus(true, "File renamed");
125+
} catch (IOException e) {
126+
result = handleStatus(false, e.getMessage());
127+
}
128+
return result;
123129
}
124130

125131
/**
@@ -143,8 +149,18 @@ private Response handleStatus(boolean success, String message) throws IOExceptio
143149
private Response handleUpload(Map<String, String> data) throws IOException {
144150
String fileName = data.get("fileName");
145151
String content = data.get("data");
146-
boolean result = saveFile(fileName, content);
147-
return handleStatus(result, result ? "File saved" : "File save error");
152+
Response result;
153+
try {
154+
if (fileName == null || content == null) {
155+
result = handleStatus(false, "Invalid input");
156+
} else {
157+
saveFile(fileName, content.getBytes(UTF_8));
158+
result = handleStatus(true, "File saved");
159+
}
160+
} catch (Exception e) {
161+
result = handleStatus(false, e.getMessage());
162+
}
163+
return result;
148164
}
149165

150166
/**

src/platform/android/webui/server/src/main/java/net/sourceforge/smallbasic/Server.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
import java.util.Objects;
1515

1616
public class Server {
17-
public static void main( String[] args ) {
17+
private static final String BASIC_HOME = "../basic";
18+
19+
public static void main(String[] args ) {
1820
// ln -s ../../../../../../../../app/src/main/java/net/sourceforge/smallbasic/WebServer.java .
1921
WebServer webServer = new WebServer() {
2022
@Override
@@ -30,7 +32,7 @@ protected void execStream(InputStream inputStream) {
3032

3133
@Override
3234
protected Collection<FileData> getFileData() throws IOException {
33-
final File folder = new File("../basic");
35+
final File folder = new File(BASIC_HOME);
3436
Collection<FileData> result = new ArrayList<>();
3537
for (final File fileEntry : Objects.requireNonNull(folder.listFiles())) {
3638
BasicFileAttributes attr = Files.readAttributes(fileEntry.toPath(), BasicFileAttributes.class);
@@ -48,7 +50,7 @@ protected Collection<FileData> getFileData() throws IOException {
4850

4951
@Override
5052
protected Response getFile(String path, boolean asset) throws IOException {
51-
String prefix = asset ? "../build/" : "../basic/";
53+
String prefix = asset ? "../build/" : BASIC_HOME;
5254
File file = new File(prefix + path);
5355
return new Response(Files.newInputStream(file.toPath()), file.length());
5456
}
@@ -65,15 +67,28 @@ protected void log(String message, Exception exception) {
6567
}
6668

6769
@Override
68-
protected boolean renameFile(String from, String to) {
69-
log("rename " + from + " to " + to);
70-
return true;
70+
protected void renameFile(String from, String to) throws IOException {
71+
if (to == null || !to.endsWith(".bas")) {
72+
throw new IOException("Invalid New File Name: " + to);
73+
}
74+
File toFile = new File(BASIC_HOME, to);
75+
if (toFile.exists()) {
76+
throw new IOException("New File Name already exists");
77+
}
78+
File fromFile = new File(BASIC_HOME, from);
79+
if (!fromFile.exists()) {
80+
throw new IOException("Old File Name does not exist");
81+
}
82+
if (!fromFile.renameTo(new File(BASIC_HOME, to))) {
83+
throw new IOException("File rename failed");
84+
}
7185
}
7286

7387
@Override
74-
protected boolean saveFile(String fileName, String content) {
75-
return true;
88+
protected void saveFile(String fileName, byte[] content) throws IOException {
89+
throw new IOException("Failed to save file: " + fileName);
7690
}
91+
7792
};
7893
webServer.run(8080, "ABC123");
7994
}

0 commit comments

Comments
 (0)