Skip to content

Commit af16976

Browse files
committed
ANDROID: fix binary data upload in webui
1 parent 5857efb commit af16976

File tree

14 files changed

+49
-39
lines changed

14 files changed

+49
-39
lines changed

src/lib/lodepng

src/lib/miniaudio

src/lib/stb

src/platform/android/app/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ android {
99
applicationId 'net.sourceforge.smallbasic'
1010
minSdkVersion 16
1111
targetSdkVersion 33
12-
versionCode 53
12+
versionCode 54
1313
versionName '12.25'
1414
resConfigs 'en'
1515
}
@@ -47,6 +47,7 @@ android {
4747
path '../jni/Android.mk'
4848
}
4949
}
50+
namespace 'net.sourceforge.smallbasic'
5051
}
5152

5253
dependencies {

src/platform/android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="net.sourceforge.smallbasic">
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
43
<!-- support large + xlarge screens to avoid compatibility mode -->
54
<supports-screens android:largeScreens="true" />
65
<supports-screens android:xlargeScreens="true" />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ private long getFileLength(String name) throws IOException {
10471047
private Collection<FileData> getFiles(File path) {
10481048
Collection<FileData> result = new ArrayList<>();
10491049
if (path.isDirectory() && path.canRead()) {
1050-
File[] files = path.listFiles(new BasFileFilter());
1050+
File[] files = path.listFiles((FilenameFilter)null);
10511051
if (files != null) {
10521052
for (File file : files) {
10531053
result.add(new FileData(file));

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

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@
1111
import java.net.ServerSocket;
1212
import java.net.Socket;
1313
import java.net.URLDecoder;
14-
import java.time.Instant;
15-
import java.time.ZoneId;
16-
import java.time.ZonedDateTime;
17-
import java.time.format.DateTimeFormatter;
14+
import java.text.DateFormat;
15+
import java.text.SimpleDateFormat;
1816
import java.util.ArrayList;
17+
import java.util.Base64;
1918
import java.util.Collection;
2019
import java.util.HashMap;
2120
import java.util.List;
21+
import java.util.Locale;
2222
import java.util.Map;
23+
import java.util.TimeZone;
2324
import java.util.concurrent.ExecutorService;
2425
import java.util.concurrent.Executors;
2526
import java.util.zip.ZipEntry;
@@ -32,7 +33,7 @@
3233
* @author chrisws
3334
*/
3435
public abstract class WebServer {
35-
private static final int BUFFER_SIZE = 32768;
36+
private static final int BUFFER_SIZE = 32768 / 2;
3637
private static final int SEND_SIZE = BUFFER_SIZE / 4;
3738
private static final int LINE_SIZE = 128;
3839
private static final String UTF_8 = "utf-8";
@@ -106,6 +107,7 @@ public abstract static class AbstractRequest {
106107
final String tokenKey;
107108
final List<String> headers;
108109
final InputStream inputStream;
110+
private static final String BASE_64_PREFIX = ";base64,";
109111

110112
public AbstractRequest(Socket socket, String tokenKey) throws IOException {
111113
this.socket = socket;
@@ -156,16 +158,24 @@ protected Map<String, Collection<String>> getParameters(String url) throws IOExc
156158
/**
157159
* Parses HTTP POST data from the given input stream
158160
*/
159-
protected Map<String, String> getPostData(InputStream inputStream) throws IOException {
161+
protected Map<String, Object> getPostData(InputStream inputStream) throws IOException {
160162
String postData = getLine(inputStream);
161163
String[] fields = postData.split("&");
162-
Map<String, String> result = new HashMap<>();
164+
Map<String, Object> result = new HashMap<>();
163165
for (String nextField : fields) {
164166
int eq = nextField.indexOf("=");
165167
if (eq != -1) {
166168
String key = nextField.substring(0, eq);
167-
String value = URLDecoder.decode(nextField.substring(eq + 1), UTF_8);
168-
result.put(key, value);
169+
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+
}
169179
}
170180
}
171181
return result;
@@ -253,9 +263,11 @@ public FileData(String fileName, String date, long size) {
253263
}
254264

255265
public FileData(File file) {
256-
ZonedDateTime zonedDateTime = Instant.ofEpochMilli(file.lastModified()).atZone(ZoneId.of("UTC"));
266+
final DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
267+
format.setLenient(false);
268+
format.setTimeZone(TimeZone.getTimeZone("UTC"));
257269
this.fileName = file.getName();
258-
this.date = DateTimeFormatter.RFC_1123_DATE_TIME.format(zonedDateTime);
270+
this.date = format.format(file.lastModified());
259271
this.size = file.length();
260272
}
261273
}
@@ -432,8 +444,8 @@ private void handleGet(Map<String, Collection<String>> parameters) throws IOExce
432444
/**
433445
* Handler for POST requests
434446
*/
435-
private void handlePost(Map<String, String> data) throws IOException {
436-
String userToken = data.get(TOKEN);
447+
private void handlePost(Map<String, Object> data) throws IOException {
448+
String userToken = (String) data.get(TOKEN);
437449
if (userToken == null) {
438450
userToken = requestToken;
439451
}
@@ -464,9 +476,9 @@ private void handlePost(Map<String, String> data) throws IOException {
464476
/**
465477
* Handler for File rename operations
466478
*/
467-
private Response handleRename(Map<String, String> data) throws IOException {
468-
String from = data.get("from");
469-
String to = data.get("to");
479+
private Response handleRename(Map<String, Object> data) throws IOException {
480+
String from = (String) data.get("from");
481+
String to = (String) data.get("to");
470482
Response result;
471483
try {
472484
renameFile(from, to);
@@ -503,15 +515,15 @@ private Response handleStatus(boolean success, String message) throws IOExceptio
503515
/**
504516
* Handler for file uploads
505517
*/
506-
private Response handleUpload(Map<String, String> data) throws IOException {
507-
String fileName = data.get("fileName");
508-
String content = data.get("data");
518+
private Response handleUpload(Map<String, Object> data) throws IOException {
519+
String fileName = (String) data.get("fileName");
520+
ByteArrayOutputStream content = (ByteArrayOutputStream) data.get("data");
509521
Response result;
510522
try {
511523
if (fileName == null || content == null) {
512524
result = handleStatus(false, "Invalid input");
513525
} else {
514-
saveFile(fileName, content.getBytes(UTF_8));
526+
saveFile(fileName, content.toByteArray());
515527
result = handleStatus(true, "File saved");
516528
}
517529
} catch (Exception e) {

src/platform/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
mavenCentral()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:7.3.0'
8+
classpath 'com.android.tools.build:gradle:7.3.1'
99
}
1010
}
1111

src/platform/android/jni/runtime.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ bool Runtime::loadSettings(Properties<String *> &settings) {
572572
String buffer;
573573
struct stat st{};
574574
if (stat(path.c_str(), &st) == 0) {
575-
int len = st.st_size;
575+
long len = st.st_size;
576576
buffer.append(fp, len);
577577
settings.load(buffer.c_str(), buffer.length());
578578
result = true;
@@ -959,7 +959,7 @@ bool System::getPen3() {
959959
return result;
960960
}
961961

962-
void System::completeKeyword(int index) {
962+
void System::completeKeyword(int index) const {
963963
if (get_focus_edit() && isEditing()) {
964964
const char *help = get_focus_edit()->completeKeyword(index);
965965
if (help) {

src/platform/android/jni/runtime.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct Runtime : public System {
5454
void pushEvent(MAEvent *event);
5555
void readSensorEvents();
5656
void setFloat(const char *methodName, float value);
57-
void setLocationData(var_t *retval);
57+
static void setLocationData(var_t *retval);
5858
void setSensorData(var_t *retval);
5959
void setString(const char *methodName, const char *value);
6060
void speak(const char *text) { setString("speak", text); }
@@ -72,7 +72,7 @@ struct Runtime : public System {
7272
void onRunCompleted();
7373
void onUnicodeChar(int ch);
7474
void loadConfig();
75-
void loadEnvConfig(Properties<String *> &settings, const char *key);
75+
static void loadEnvConfig(Properties<String *> &settings, const char *key);
7676
bool loadSettings(Properties<String *> &settings);
7777
void saveConfig();
7878
void runPath(const char *path);

0 commit comments

Comments
 (0)