Skip to content

Commit cb470ce

Browse files
committed
ANDROID: handle non-.bas file in UI
1 parent af16976 commit cb470ce

File tree

2 files changed

+65
-25
lines changed

2 files changed

+65
-25
lines changed

src/platform/android/app/src/main/assets/main.bas

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const aboutId = "_about"
3434
const backId = "_back"
3535
const scratchId = "_scratch"
3636
const scratch_file = HOME + "/scratch.bas"
37+
const sortEnd = chr(255)
3738

3839
func mk_menu(value, lab, x)
3940
local bn
@@ -230,13 +231,17 @@ end
230231
func fileCmpFunc0(l, r)
231232
local f1 = lower(l.name)
232233
local f2 = lower(r.name)
234+
if (right(f1, 4) != ".bas") then f1 = sortEnd + f1
235+
if (right(f2, 4) != ".bas") then f2 = sortEnd + f2
233236
local n = iff(f1 == f2, 0, iff(f1 > f2, 1, -1))
234237
return iff(l.dir == r.dir, n, iff(l.dir, 1, -1))
235238
end
236239

237240
func fileCmpFunc1(l, r)
238241
local f1 = lower(l.name)
239242
local f2 = lower(r.name)
243+
if (right(f1, 4) != ".bas") then f1 = sortEnd + f1
244+
if (right(f2, 4) != ".bas") then f2 = sortEnd + f2
240245
local n = iff(f1 == f2, 0, iff(f1 > f2, -1, 1))
241246
return iff(l.dir == r.dir, n, iff(l.dir, 1, -1))
242247
end
@@ -285,7 +290,7 @@ sub loadFileList(path, byref basList)
285290
end
286291

287292
func androidWalker(node)
288-
if (node.dir == 0 && lower(right(node.name, 4)) == ".bas") then
293+
if (node.dir == 0) then
289294
basList << node
290295
endif
291296
return node.depth == 0
@@ -392,7 +397,8 @@ sub listFiles(byref frm, path, sortDir, byref basList)
392397
endif
393398
if (abbr) then
394399
bn = mk_bn(path + name, name, txtcol)
395-
bn.type = "link"
400+
bn.type = iff(lower(right(name, 4)) == ".bas", "link", "label")
401+
if (bn.type == "label") then bn.color = colText
396402
if (!node.dir) then bn.isExit = true
397403
else
398404
if (len(name) > 27) then
@@ -401,7 +407,8 @@ sub listFiles(byref frm, path, sortDir, byref basList)
401407
lab = name
402408
endif
403409
bn = mk_bn(path + name, lab, txtcol)
404-
bn.type = "link"
410+
bn.type = iff(lower(right(name, 4)) == ".bas", "link", "label")
411+
if (bn.type == "label") then bn.color = colText
405412
if (!node.dir) then bn.isExit = true
406413
frm.inputs << bn
407414
gap = 12 - len(str(node.size))

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

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.sourceforge.smallbasic;
22

3+
import android.util.Base64;
4+
35
import java.io.BufferedOutputStream;
46
import java.io.ByteArrayInputStream;
57
import java.io.ByteArrayOutputStream;
@@ -14,7 +16,6 @@
1416
import java.text.DateFormat;
1517
import java.text.SimpleDateFormat;
1618
import java.util.ArrayList;
17-
import java.util.Base64;
1819
import java.util.Collection;
1920
import java.util.HashMap;
2021
import java.util.List;
@@ -107,7 +108,6 @@ public abstract static class AbstractRequest {
107108
final String tokenKey;
108109
final List<String> headers;
109110
final InputStream inputStream;
110-
private static final String BASE_64_PREFIX = ";base64,";
111111

112112
public AbstractRequest(Socket socket, String tokenKey) throws IOException {
113113
this.socket = socket;
@@ -158,24 +158,16 @@ protected Map<String, Collection<String>> getParameters(String url) throws IOExc
158158
/**
159159
* Parses HTTP POST data from the given input stream
160160
*/
161-
protected Map<String, Object> getPostData(InputStream inputStream) throws IOException {
161+
protected Map<String, FormField> getPostData(InputStream inputStream) throws IOException {
162162
String postData = getLine(inputStream);
163163
String[] fields = postData.split("&");
164-
Map<String, Object> result = new HashMap<>();
164+
Map<String, FormField> result = new HashMap<>();
165165
for (String nextField : fields) {
166166
int eq = nextField.indexOf("=");
167167
if (eq != -1) {
168168
String key = nextField.substring(0, eq);
169169
String value = nextField.substring(eq + 1);
170-
int index = value.indexOf(BASE_64_PREFIX);
171-
if (index != -1) {
172-
ByteArrayOutputStream data = new ByteArrayOutputStream();
173-
value = value.substring(index + BASE_64_PREFIX.length());
174-
data.write(Base64.getDecoder().decode(value));
175-
result.put(key, data);
176-
} else {
177-
result.put(key, URLDecoder.decode(nextField.substring(eq + 1), UTF_8));
178-
}
170+
result.put(key, new FormField(key, value));
179171
}
180172
}
181173
return result;
@@ -272,6 +264,37 @@ public FileData(File file) {
272264
}
273265
}
274266

267+
/**
268+
* Holder for POST form data
269+
*/
270+
public static class FormField {
271+
private static final String BASE_64_PREFIX = ";base64,";
272+
private final String string;
273+
private final byte[] bytes;
274+
275+
public FormField(String key, String value) throws IOException {
276+
int index = value.indexOf(BASE_64_PREFIX);
277+
if (index != -1 && "data".equals(key)) {
278+
ByteArrayOutputStream data = new ByteArrayOutputStream();
279+
String base64Value = value.substring(index + BASE_64_PREFIX.length());
280+
data.write(Base64.decode(base64Value, Base64.DEFAULT));
281+
this.string = null;
282+
this.bytes = data.toByteArray();
283+
} else {
284+
this.string = URLDecoder.decode(value, UTF_8);
285+
this.bytes = null;
286+
}
287+
}
288+
289+
public String getString() {
290+
return string;
291+
}
292+
293+
public byte[] toByteArray() {
294+
return bytes;
295+
}
296+
}
297+
275298
/**
276299
* Build JSON string
277300
*/
@@ -369,6 +392,16 @@ private Collection<String> getAllFileNames() throws IOException {
369392
return result;
370393
}
371394

395+
private byte[] getData(Map<String, FormField> data) {
396+
FormField field = data.get("data");
397+
return field == null ? null : field.toByteArray();
398+
}
399+
400+
private String getString(Map<String, FormField> data, String key) {
401+
FormField field = data.get(key);
402+
return field == null ? null : field.toString();
403+
}
404+
372405
/**
373406
* Download files button handler
374407
*/
@@ -444,8 +477,8 @@ private void handleGet(Map<String, Collection<String>> parameters) throws IOExce
444477
/**
445478
* Handler for POST requests
446479
*/
447-
private void handlePost(Map<String, Object> data) throws IOException {
448-
String userToken = (String) data.get(TOKEN);
480+
private void handlePost(Map<String, FormField> data) throws IOException {
481+
String userToken = getString(data, TOKEN);
449482
if (userToken == null) {
450483
userToken = requestToken;
451484
}
@@ -476,9 +509,9 @@ private void handlePost(Map<String, Object> data) throws IOException {
476509
/**
477510
* Handler for File rename operations
478511
*/
479-
private Response handleRename(Map<String, Object> data) throws IOException {
480-
String from = (String) data.get("from");
481-
String to = (String) data.get("to");
512+
private Response handleRename(Map<String, FormField> data) throws IOException {
513+
String from = getString(data, "from");
514+
String to = getString(data, "to");
482515
Response result;
483516
try {
484517
renameFile(from, to);
@@ -515,15 +548,15 @@ private Response handleStatus(boolean success, String message) throws IOExceptio
515548
/**
516549
* Handler for file uploads
517550
*/
518-
private Response handleUpload(Map<String, Object> data) throws IOException {
519-
String fileName = (String) data.get("fileName");
520-
ByteArrayOutputStream content = (ByteArrayOutputStream) data.get("data");
551+
private Response handleUpload(Map<String, FormField> data) throws IOException {
552+
String fileName = getString(data, "fileName");
553+
byte[] content = getData(data);
521554
Response result;
522555
try {
523556
if (fileName == null || content == null) {
524557
result = handleStatus(false, "Invalid input");
525558
} else {
526-
saveFile(fileName, content.toByteArray());
559+
saveFile(fileName, content);
527560
result = handleStatus(true, "File saved");
528561
}
529562
} catch (Exception e) {

0 commit comments

Comments
 (0)