Skip to content
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

Increase support for BLOBs and add additional VFS API. #35

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/virtualFS.html
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,8 @@ <h2 dir="auto">Functions</h2>
function to tell you if stored data is a file or a folder</li>
<li><code class="notranslate">async readFile(path, fsObject = this.fileSystem, bypass = false)</code><br>
Function to get the contents of a file at a given path</li>
<li><code class="notranslate">async readFileAsBuffer(path, fsObject = this.fileSystem)</code><br>
Same as readFile but for non-text objects (uploaded with non text/ mime type) returns Uint8Array</li>
<li><code class="notranslate">async writeFile(path, contents, fsObject = this.fileSystem)</code><br>
Function to write to a file at a given path</li>
<li><code class="notranslate">async createFolder(path, fsObject = this.fileSystem)</code><br>
Expand Down
2 changes: 2 additions & 0 deletions docs/virtualFS.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Vfs functions are found as below. Some are documented, others are not
function to tell you if stored data is a file or a folder
- `async readFile(path, fsObject = this.fileSystem, bypass = false)`
Function to get the contents of a file at a given path
- `async readFileAsBuffer(path, fsObject = this.fileSystem)`
Same as readFile but for non-text objects (uploaded with non text/ mime type) returns Uint8Array.
- `async writeFile(path, contents, fsObject = this.fileSystem)`
Function to write to a file at a given path
- `async createFolder(path, fsObject = this.fileSystem)`
Expand Down
8 changes: 2 additions & 6 deletions pkgs/apps/FileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export default {
);
let text = await vfs.readFile(selectedItem, undefined);

if (text.startsWith('blob:')) {
if (text.startsWith('blob:') || text.startsWith('data:')) {
var element = document.createElement("a");
element.setAttribute("href", text);
element.setAttribute("download", selectedItem.split("/").pop());
Expand Down Expand Up @@ -164,11 +164,7 @@ export default {
var file = e.target.files[0];
var reader = new FileReader();

if (
file.type.startsWith("image") ||
file.type.startsWith("audio") ||
file.type.startsWith("video")
) {
if (!file.type.startsWith("text")) {
console.log(file);
// read as arraybuffer; store as base64
// reader.readAsDataURL(file);
Expand Down
13 changes: 13 additions & 0 deletions pkgs/lib/VirtualFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,19 @@ const Vfs = {
return resolve(current);
});
},
// Read a file and return it as an uint8array
async readFileAsBuffer(path, fsObject = this.fileSystem) {
return new Promise(async (resolve, reject) => {
let data = await this.readFile(path, fsObject);
if (data === null) return resolve(null);
if (data.startsWith("data:") || data.startsWith("blob:")) {
let blob = await fetch(data).then((r) => r.blob()); // This works and I dont like it.
let buffer = await blob.arrayBuffer();
return resolve(new Uint8Array(buffer));
}
return resolve(null);
})
},
// Function to write to a file at a given path
async writeFile(path, contents, fsObject = this.fileSystem) {
if (typeof contents !== "string")
Expand Down