Skip to content

Commit 7f05140

Browse files
committed
Prettier check implemented
1 parent e890689 commit 7f05140

File tree

1 file changed

+54
-52
lines changed

1 file changed

+54
-52
lines changed

sites/cheerpj/src/content/docs/11-guides/filesystem.mdx

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ description: Interacting with the virtual filesystem in CheerpJ
66
CheerpJ provides a **virtual filesystem** that lets your Java application read and write files inside a secure, browser-sandboxed environment. For browser security reasons, it cannot access the user’s actual local disk. All file operations happen within this virtual space.
77

88
> [!info] Mounting points overview
9+
>
910
> - **`/app/`** — HTTP-based filesystem for loading files from your web server; **Write**: No, **Read**: Yes.
1011
> - **`/files/`** — Persistent read–write filesystem (browser-scoped, e.g., IndexedDB); **Write**: Java only, **Read**: Yes.
1112
> - **`/str/`** — Filesystem for passing JavaScript strings/binary data to Java; **Write**: JS only, **Read**: Yes.
@@ -26,17 +27,17 @@ The quickest way to place a file into CheerpJ’s virtual filesystem from JavaSc
2627

2728
```js
2829
// Call this only after cheerpjInit() has resolved.
29-
async function addStringFileToCheerpJ() {
30-
const fileName = "default.txt";
31-
const content = "Default content.";
32-
33-
try {
34-
await cheerpOSAddStringFile(`/str/${fileName}`, content);
35-
console.log(`File "${fileName}" added to /str/.`);
36-
} catch (e) {
37-
console.error("Error writing file to /str/:", e);
38-
}
30+
async function addStringFileToCheerpJ() {
31+
const fileName = "default.txt";
32+
const content = "Default content.";
33+
34+
try {
35+
await cheerpOSAddStringFile(`/str/${fileName}`, content);
36+
console.log(`File "${fileName}" added to /str/.`);
37+
} catch (e) {
38+
console.error("Error writing file to /str/:", e);
3939
}
40+
}
4041
```
4142

4243
### Method 2: Using library mode
@@ -68,6 +69,7 @@ async function copyFileToFilesMountPoint() {
6869
When Java runs under CheerpJ, files are saved in the virtual filesystem (typically under **`/files/`**), not directly to the user’s disk. To make a file available to the page (for download, upload, or preview), expose it via a **Java native method** implemented in JavaScript. The JavaScript side reads the bytes from the VFS and then uses standard browser APIs (e.g., create a `Blob`, trigger a download, or upload it).
6970

7071
**Example flow**
72+
7173
1. Java writes/generates the file (e.g., under **`/files/`**).
7274
2. Java invokes a JavaScript **native method**.
7375
3. JavaScript reads the file from the VFS and processes it (e.g., create a `Blob` and trigger a download).
@@ -105,33 +107,33 @@ Now we’ll implement the JavaScript for `downloadFileFromCheerpJ`. The function
105107
// Native method implementation called from Java
106108
// Naming: Java_<fully-qualified-class>_<method>
107109
async function Java_App_downloadFileFromCheerpJ(lib, fileName) {
108-
try {
109-
// Read the file from CheerpJ's virtual filesystem as a Blob
110-
const blob = await cjFileBlob("/files/"+fileName);
111-
112-
// Create an invisible link
113-
const link = document.createElement('a');
114-
link.href = URL.createObjectURL(blob);
115-
link.download = fileName;
116-
117-
// Simulate a click on the link and clean up objects
118-
link.click();
119-
link.remove();
120-
URL.revokeObjectURL(url);
121-
122-
console.log(`Successfully downloaded ${fileName}`);
123-
} catch (e) {
124-
console.error("Error downloading file:", e);
125-
}
110+
try {
111+
// Read the file from CheerpJ's virtual filesystem as a Blob
112+
const blob = await cjFileBlob("/files/" + fileName);
113+
114+
// Create an invisible link
115+
const link = document.createElement("a");
116+
link.href = URL.createObjectURL(blob);
117+
link.download = fileName;
118+
119+
// Simulate a click on the link and clean up objects
120+
link.click();
121+
link.remove();
122+
URL.revokeObjectURL(url);
123+
124+
console.log(`Successfully downloaded ${fileName}`);
125+
} catch (e) {
126+
console.error("Error downloading file:", e);
127+
}
126128
}
127129

128130
// Initialize CheerpJ and register the native method
129131
async function cj3init() {
130-
await cheerpjInit({
131-
// Register native method
132-
natives: { Java_App_downloadFileFromCheerpJ },
133-
});
134-
// cheerpjCreateDisplay(...) and cheerpjRunMain(...) go here
132+
await cheerpjInit({
133+
// Register native method
134+
natives: { Java_App_downloadFileFromCheerpJ },
135+
});
136+
// cheerpjCreateDisplay(...) and cheerpjRunMain(...) go here
135137
}
136138
cj3init();
137139
```
@@ -225,24 +227,24 @@ And in your HTML, you'd have the `downloadFromJava` JavaScript function:
225227
```javascript
226228
// JavaScript in applet.html
227229
function downloadFromJava(encodedContent) {
228-
try {
229-
const content = decodeURIComponent(encodedContent);
230-
const blob = new Blob([content], { type: "text/plain" });
231-
const url = URL.createObjectURL(blob);
232-
233-
// Create an invisible link
234-
const link = document.createElement("a");
235-
link.href = url;
236-
link.download = "applet_showdocument_dynamic.txt";
237-
238-
// Simulate a click on the link and clean up objects
239-
link.click();
240-
link.remove()
241-
URL.revokeObjectURL(url);
242-
243-
console.log("File downloaded via showDocument");
244-
} catch (e) {
245-
console.error("Error downloading file:", e);
246-
}
230+
try {
231+
const content = decodeURIComponent(encodedContent);
232+
const blob = new Blob([content], { type: "text/plain" });
233+
const url = URL.createObjectURL(blob);
234+
235+
// Create an invisible link
236+
const link = document.createElement("a");
237+
link.href = url;
238+
link.download = "applet_showdocument_dynamic.txt";
239+
240+
// Simulate a click on the link and clean up objects
241+
link.click();
242+
link.remove();
243+
URL.revokeObjectURL(url);
244+
245+
console.log("File downloaded via showDocument");
246+
} catch (e) {
247+
console.error("Error downloading file:", e);
248+
}
247249
}
248250
```

0 commit comments

Comments
 (0)