Skip to content

Commit 3974431

Browse files
committed
ANDROID: file manager webui (wip)
1 parent f9db44f commit 3974431

File tree

4 files changed

+93
-53
lines changed

4 files changed

+93
-53
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -943,17 +943,29 @@ protected Collection<FileData> getFileData() throws IOException {
943943
}
944944

945945
@Override
946-
protected Response getResponse(String path, boolean asset) throws IOException {
947-
String name = "webui/" + path;
948-
AssetFileDescriptor fd = getAssets().openFd(name);
949-
return new Response(getAssets().open(name), fd.getLength());
946+
protected Response getFile(String path, boolean asset) throws IOException {
947+
Response result;
948+
if (asset) {
949+
String name = "webui/" + path;
950+
AssetFileDescriptor fd = getAssets().openFd(name);
951+
result = new Response(getAssets().open(name), fd.getLength());
952+
}
953+
else {
954+
result = null;
955+
}
956+
return result;
950957
}
951958

952959
@Override
953960
protected void log(String message, Exception exception) {
954961
Log.i(TAG, message, exception);
955962
}
956963

964+
@Override
965+
protected boolean renameFile(String from, String to) {
966+
return false;
967+
}
968+
957969
@Override
958970
protected void log(String message) {
959971
Log.i(TAG, message);

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,25 @@ public void run() {
5353

5454
protected abstract void execStream(InputStream inputStream) throws IOException;
5555
protected abstract Collection<FileData> getFileData() throws IOException;
56-
protected abstract Response getResponse(String path, boolean asset) throws IOException;
57-
protected abstract void log(String message, Exception exception);
56+
protected abstract Response getFile(String path, boolean asset) throws IOException;
5857
protected abstract void log(String message);
58+
protected abstract void log(String message, Exception exception);
59+
protected abstract boolean renameFile(String from, String to);
5960
protected abstract boolean saveFile(String fileName, String content);
6061

6162
/**
6263
* Download files button handler
6364
*/
64-
private Response handleDownload(Map<String, Collection<String>> parameters) throws IOException {
65-
Collection<String> fileNames = parameters.get("f");
65+
private Response handleDownload(Map<String, Collection<String>> data) throws IOException {
66+
Collection<String> fileNames = data.get("f");
6667
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
6768
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
6869
if (fileNames == null) {
6970
fileNames = Collections.emptyList();
7071
}
7172

7273
for (String fileName : fileNames) {
73-
Response response = getResponse(fileName, false);
74+
Response response = getFile(fileName, false);
7475
ZipEntry entry = new ZipEntry(fileName);
7576
zipOutputStream.putNextEntry(entry);
7677
response.toStream(zipOutputStream);
@@ -106,6 +107,16 @@ private Response handleFileList() throws IOException {
106107
return new Response(new ByteArrayInputStream(json), json.length);
107108
}
108109

110+
/**
111+
* Handler for File rename operations
112+
*/
113+
private Response handleRename(Map<String, String> data) throws IOException {
114+
String from = data.get("from");
115+
String to = data.get("to");
116+
boolean result = renameFile(from, to);
117+
return handleStatus(result, result ? "File renamed" : "File rename error");
118+
}
119+
109120
/**
110121
* Handler to indicate operational status
111122
*/
@@ -124,9 +135,9 @@ private Response handleStatus(boolean success, String message) throws IOExceptio
124135
/**
125136
* Handler for file uploads
126137
*/
127-
private Response handleUpload(Map<String, String> parameters) throws IOException {
128-
String fileName = parameters.get("fileName");
129-
String content = parameters.get("data");
138+
private Response handleUpload(Map<String, String> data) throws IOException {
139+
String fileName = data.get("fileName");
140+
String content = data.get("data");
130141
boolean result = saveFile(fileName, content);
131142
return handleStatus(result, result ? "File saved" : "File save error");
132143
}
@@ -143,7 +154,7 @@ private Response handleWebResponse(String asset) throws IOException {
143154
} else {
144155
path = asset;
145156
}
146-
return getResponse(path, true);
157+
return getFile(path, true);
147158
}
148159

149160
/**
@@ -414,6 +425,8 @@ private void handlePost(Map<String, String> data, String tokenKey) throws IOExce
414425
handleFileList().send(socket, userToken);
415426
} else if (url.startsWith("/api/upload")) {
416427
handleUpload(data).send(socket, null);
428+
} else if (url.startsWith("/api/rename")) {
429+
handleRename(data).send(socket, null);
417430
}
418431
log("Sent POST response");
419432
} else {

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

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

33
import sun.misc.IOUtils;
4-
import sun.rmi.runtime.Log;
54

6-
import java.io.BufferedWriter;
75
import java.io.File;
8-
import java.io.FileWriter;
96
import java.io.IOException;
107
import java.io.InputStream;
118
import java.nio.file.Files;
@@ -62,14 +59,20 @@ protected void log(String message) {
6259
}
6360

6461
@Override
65-
protected boolean saveFile(String fileName, String content) {
62+
protected void log(String message, Exception exception) {
63+
System.err.println(message);
64+
exception.printStackTrace();
65+
}
66+
67+
@Override
68+
protected boolean renameFile(String from, String to) {
69+
log("rename " + from + " to " + to);
6670
return true;
6771
}
6872

6973
@Override
70-
protected void log(String message, Exception exception) {
71-
System.err.println(message);
72-
exception.printStackTrace();
74+
protected boolean saveFile(String fileName, String content) {
75+
return true;
7376
}
7477
};
7578
webServer.run(8080, "ABC123");

src/platform/android/webui/src/App.js

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ import {
3232
const columns = [{
3333
field: 'fileName',
3434
headerName: 'Name',
35+
editable: true,
36+
preProcessEditCellProps: (params) => {
37+
const hasError = !params.props.value.endsWith(".bas");
38+
return {...params.props, error: hasError};
39+
} ,
3540
flex: 1,
3641
}, {
3742
field: 'size',
@@ -41,22 +46,6 @@ const columns = [{
4146
field: 'date',
4247
headerName: 'Modified',
4348
type: 'number',
44-
}, {
45-
field: 'actions',
46-
type: 'actions',
47-
headerName: 'Actions',
48-
cellClassName: 'actions',
49-
getActions: ({ id }) => {
50-
return [
51-
<Button
52-
variant="contained"
53-
size="small"
54-
style={{marginLeft: 16}}
55-
tabIndex={-1}>
56-
Run
57-
</Button>
58-
];
59-
},
6049
}];
6150

6251
function getFetchHeader(body) {
@@ -67,28 +56,35 @@ function getFetchHeader(body) {
6756
};
6857
}
6958

70-
function getFiles(token, success, fail) {
71-
fetch('/api/files', getFetchHeader("token=" + token))
72-
.then(response => response.json())
73-
.then(success)
74-
.catch(fail);
75-
}
76-
77-
function upload(name, data, success, fail) {
78-
let body = "fileName=" + encodeURIComponent(name) + "&data=" + encodeURIComponent(data);
79-
fetch('/api/upload', getFetchHeader(body))
59+
function fetchApi(api, body, success, fail) {
60+
fetch(api, getFetchHeader(body))
8061
.then(response => response.json())
8162
.then((response) => {
8263
if (response.error) {
8364
fail(response.error);
8465
} else {
85-
success();
66+
success(response);
8667
}
8768
})
8869
.catch(fail);
8970
}
9071

91-
function copyFiles(event, token, success, fail) {
72+
function getFiles(token, success, fail) {
73+
let body = token ? ("token=" + token) : "";
74+
fetchApi('/api/files', body, success, fail);
75+
}
76+
77+
function upload(name, data, success, fail) {
78+
let body = "fileName=" + encodeURIComponent(name) + "&data=" + encodeURIComponent(data);
79+
fetchApi('/api/upload', body, success, fail);
80+
}
81+
82+
function renameFile(from, to, success, fail) {
83+
let body = "from=" + encodeURIComponent(from) + "&to=" + encodeURIComponent(to);
84+
fetchApi('/api/rename', body, success, fail);
85+
}
86+
87+
function copyFiles(event, success, fail) {
9288
const fileReader = new FileReader();
9389
const input = event.target;
9490
const files = input.files;
@@ -98,7 +94,7 @@ function copyFiles(event, token, success, fail) {
9894
if (++index < files.length) {
9995
fileReader.readAsText(files[index]);
10096
} else {
101-
getFiles(token, success, fail);
97+
getFiles(null, success, fail);
10298
// reset input control
10399
input.value = input.defaultValue;
104100
}
@@ -131,7 +127,7 @@ function GridToolbarDownload(props) {
131127

132128
function GridToolbarUpload(props) {
133129
const handleUpload = (event) => {
134-
copyFiles(event, props.token, (newRows) => {
130+
copyFiles(event, (newRows) => {
135131
props.setRows(newRows);
136132
}, (error) => {
137133
// show toast message
@@ -159,19 +155,35 @@ function AppToolbar(props) {
159155
);
160156
}
161157

158+
function onCellEditCommit(props, params) {
159+
props.rows.forEach((row) => {
160+
if (row.id === params.id) {
161+
renameFile(row.fileName, params.value, () => {
162+
getFiles(null, (data) => {
163+
props.setRows(data);
164+
}, (error) => {
165+
console.log(error);
166+
});
167+
}, (error) => {
168+
console.log(error);
169+
});
170+
}
171+
});
172+
};
173+
162174
function FileList(props) {
163175
const [selectionModel, setSelectionModel] = useState([]);
164176

165177
const toolbarProps = {
166178
selections: selectionModel,
167179
setRows: props.setRows,
168-
rows: props.rows,
169-
token: props.token,
180+
rows: props.rows
170181
};
171182

172183
return (
173184
<DataGrid rows={props.rows}
174185
columns={columns}
186+
onCellEditCommit={(params) => onCellEditCommit(props, params)}
175187
pageSize={5}
176188
components={{Toolbar: AppToolbar}}
177189
componentsProps={{toolbar: toolbarProps}}
@@ -239,7 +251,7 @@ export default function App() {
239251

240252
let content;
241253
if (token) {
242-
content = <FileList setRows={setRows} rows={rows} token={token} />;
254+
content = <FileList setRows={setRows} rows={rows} />;
243255
} else {
244256
content = <TokenInput setRows={setRows} setToken={setToken} token={token}/>;
245257
}

0 commit comments

Comments
 (0)