-
Notifications
You must be signed in to change notification settings - Fork 19
Filesystem Guide Rework #308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
… and Filesystem guide'.
|
Considering to change the "Mounting points overview" callout to normal bullet points. |
epanholz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some comments. Overall it already looks way better but some parts can still be improved,
pnpm-workspace.yaml
Outdated
| - sites/* | ||
| - packages/* | ||
|
|
||
| ignoredBuiltDependencies: | ||
| - esbuild | ||
| - sharp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain why these changes are necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've forced build everything on my machine and it got committed on accident. I've reverted the changes.
| ## Getting a file from the Virtual File System to JavaScript (i.e local file system) | ||
|
|
||
| ## 2\. Getting Files from the Virtual Filesystem to the Local Filesystem (Java to JavaScript) | ||
| 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). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(typically under /files/) -> (defaults to /files/)
I would also rewrite this slightly:
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).
Something like this:
CheerpJ provides APIs that are available through JavaScript to extract files from the virtual file system (link to blob api). To implement a more seamless file interaction you can utilize JavaScript native methods (link to docs) to handle the file after a filer interaction. This can look something like this: (Example flow you already have)
We should also add the cjFileBlob() api here as method 1, and then method 2 is with javascript natives.
2859d5e to
24a37ef
Compare
24a37ef to
7f05140
Compare
| Entries you create under **`/str/`** (from JavaScript) are readable from Java as regular files. Remember: `/str/` is **not persisted** and is **read-only** from Java. | ||
|
|
||
| ```java title="App.java" | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| /* | ||
| Rest of your Java code... | ||
| */ | ||
| String text = Files.readString(Path.of("/str/fileName.txt")); | ||
| System.out.println(text); | ||
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this code look? Do I need to add more structure and details like below;
import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.util.Scanner; // Import the Scanner class to read text files
public class ReadFile {
public static void main(String[] args) {
File myObj = new File("filename.txt");
// try-with-resources: Scanner will be closed automatically
try (Scanner myReader = new Scanner(myObj)) {
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
System.out.println(data);
}
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part looks good now.
| - **`cheerpjRunLibrary("")`**: This creates a library mode object that allows you to use Java's `nio.file` APIs in JavaScript. You can read more about library mode in the [CheerpJ documentation](/docs/reference/cheerpjRunLibrary). | ||
| - **`Files.copy(source, target, [StandardCopyOption.REPLACE_EXISTING])`**: This copies the file from the `/app/` mount point to the `/files/` mount point. The `REPLACE_EXISTING` option ensures that if the target file already exists, it will be overwritten. | ||
| - **Accessing the File in Java**: After copying, your Java application can access the file simply by using `new File("notes_tmp.txt")`, without needing to specify the `/app/` prefix. | ||
| When Java runs under CheerpJ, files are saved in the virtual filesystem (defaults under **`/files/`**), not directly to the user’s disk. Use the JavaScript-accessible [cjFileBlob API](/docs/reference/cjFileBlob) to extract files from the virtual file system. For a more seamless flow, define a [Native Methods](/docs/guides/implementing-native-methods) implemented in JavaScript to process the file. This can look like: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've linked to the docs here and have removed the 'Notes' part in the end where the docs were initially linked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is okay. I originally suggested to add a Method 1 - Using cjFileBlob and a Method 2 - Using native methods to make it more align with the previous section and also a bit easier to read. cjFileBlob can be used to extract files from the virtual file system and having a very short stand alone example here would be nice.
epanholz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requested a few more small changes, after that it should be ready for merging.
| **Mounting points overview** | ||
|
|
||
| CheerpJ's virtual filesystem has different **mounting points**, each with specific purposes: | ||
| - **`/app/`** — Read-only mount mapping to your web server’s root (HTTP assets). Java can read the files using the '/app/' prefix. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Read-only mount mapping to your web server’s root (HTTP assets). Java can read the files using the '/app/' prefix. -> Read-only mount that points to the root of your web server. Java can read the files using the '/app/' prefix.
I think saying HTTP assets might confuse users, since they can pass any files they want to the application like this, like config files or similar.
| To access local files, first import them into the virtual file system (see previous section). To save or download files to the user’s machine from Java, you can utilize a JavaScript native method again. | ||
|
|
||
| // (Retain the native method declaration from Section 2) | ||
| **Java Source Code: Essential `JFileChooser` and Download Logic** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Java Source Code: Essential JFileChooser and Download Logic -> Java Source Code: JFileChooser and download logic
| System.out.println("Triggered direct URL download: " + fileUrl.toString()); | ||
| } catch (Exception ex) { | ||
| ex.printStackTrace(); | ||
| JOptionPane.showMessageDialog(null, "Error downloading from URL: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would remove this line, not need to add the reference to JOptionPane if it does not show in the code example.
I've updated the structure, a bit of text and tried to make the code examples simpler and easy to follow.