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
indexeddb-files: example using the IndexedDB API to store and retrieve files #171
Conversation
@wbamberg This is an example addon with a collection of small examples (well... as small as possible :-)) related to some common use cases around files:
can you take a look at it and help me to make it as clear and helpful as possible? :-) (as a further step it would be probably even more helpful if we turn it into a howto/tutorial form on MDN, manipulating files efficently is going to be probably a pretty common use case) |
@mixedpuppy This is an example related to how to manipulating files from a WebExtension using the existent WebAPIs (in particular the IndexedDB and the subset of the "File and Directory Entries" APIs already implemented in Firefox) that we talked about during the All Hands, can you take a look when you got some time? as usually any ideas/suggestion/advice is welcome ;-) |
dbRequest.onsuccess = (evt) => { | ||
const file = evt.target.result; | ||
if (!file) { | ||
dbLog("File ${filename} not found."); |
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 needs to be a template literal
dbRequest.onsuccess = (evt) => { | ||
const file = evt.target.result; | ||
if (!file) { | ||
dbLog("File ${filename} not found."); |
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 needs to be a template literal too
I didn't check the code in detail for correctness, but read it to make sure I understood what it's doing. It's very nice to read and easy to understand, as far as that's possible with indexedDB :). And it worked perfectly for me. It's a bit strange that it's doing three different things, especially when one ("reading-local-dirtree") isn't directly related to indexedDB. Since IIUC you're already exercising this API in "indexeddb-save-files", do you need "reading-local-dirtree" at all? It would seem more natural, maybe, to make "tab.html" (which could have a better name perhaps, like "indexeddb-demo" or something) be a browser action popup. Then you click the popup to see the options, that then open new browser tabs. File naming:
For the UI:
|
@@ -0,0 +1,35 @@ | |||
# indexeddb-files | |||
|
|||
This add-on shows how to read and save files using the IndexedDB API and the sub-set of the "File and Directory Entries API" supported on Firefox. |
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.
sub-set -> subset (here and below)
## What it shows ## | ||
|
||
- **Reading files from a local directory tree**: which shows how to use the sub-set of the "File and Directory API" supported on Firefox to read a directory tree instead of single files | ||
- **Save files into IndexedDB**: which shows how to use the IndexedDB files to save and read files from indexedDB (and how to extract of the files as a single zip files) |
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.
"extract of the files as a single zip files" -> "download the files as a single zip file".
You could also say that this shows how to save the contents of a local directory tree into indexedDB.
* https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API | ||
|
||
- IndexedDB MutableFile API: | ||
* https://developer.mozilla.org/en-US/docs/Web/API/IDBMutableFile |
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.
It's a shame the MutableFile docs are so poor :(.
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Files from a WebExtension Examples</title> |
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 is an odd name. Perhaps "IndexedDB/file handling demos"?
<h1>Files from a WebExtension</h1> | ||
<ol> | ||
<li><a href="tabs/reading-local-dirtree.html">Reading files from a local directory tree</a></li> | ||
<li><a href="tabs/indexeddb-save-files.html">Save files into IndexedDB</a></li> |
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.
"Save the contents of a local directory using indexedDB"
<ol> | ||
<li><a href="tabs/reading-local-dirtree.html">Reading files from a local directory tree</a></li> | ||
<li><a href="tabs/indexeddb-save-files.html">Save files into IndexedDB</a></li> | ||
<li><a href="tabs/indexeddb-filehandle.html">Create temporary files</a></li> |
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.
"Create temporary files and save them using IndexedDB"
</style> | ||
</head> | ||
<body> | ||
<h1>Create mutable files using the MutableFile API, and then save and retrieve them using IndexedDB API</h1> |
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 is quite long! Could we make the H1 "Create and save temporary files" and then have a P underneath giving the details, like "Create a temporary file, give it some data and a name, then save it using indexedDB".
|
||
<hr/> | ||
|
||
<h1>1. Create temporary files</h1> |
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.
"Create a temporary file"
|
||
<div> | ||
<button id="create-file">Create a MutableFile</button> | ||
<button id="get-metadata">Get Metadata of the opened MutableFile</button> |
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.
"Get the MutableFile's metadata"
<div> | ||
<textarea id="file-data" placeholder="data to save into the file..." | ||
style="width: 100%;"></textarea> | ||
<button id="save-data">Save Data into the opened MutableFile</button><br/> |
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.
"Save Data into the opened MutableFile" "save" seems strange to me when it hasn't been saved. Could we say "Add data to the MutableFile"?
Or is "saving" (as distinct from "persisting") the correct way to refer to data written to a temp file?
}; | ||
}); | ||
|
||
// Handle the save-data, get-metadata and persist-file buttons. |
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 line (IIUC) refers to the following addEventListener calls. It would be better to split it so each one gets a comment.
It could be a bit more extensive too: "Handle the "save-data" button: fetch the contents of the "file-data" textarea and write it to the currently open temporary file."
6cae815
to
9d19501
Compare
@wbamberg Thanks a lot for the detailed review, it has been very helpful! I just pushed a new commit with a bunch of changes based on the review comments:
Let me know how the new version looks to you. |
<body> | ||
<h4>IndexedDB files handling demos:</h4> | ||
<ol> | ||
<li><a href="#" id="reading-saving-files-example">Read a local directory tree and Save its content into IndexedDB</a></li> |
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.
Save -> save
</details> | ||
|
||
<details id="step-04"> | ||
<summary>2. Retrieve persisted files</summary> |
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.
- -> 4.
I think this looks really great! It's much more intuitive (for me at least). I spotted a couple of typos, but otherwise r+ from me.
I almost asked for this, so I'm very glad you did it anyway :). |
…d fix missing onClickHandler on logged file link
@mi-g This pull request contains the updated version of the indexeddb-files example, would you mind to take a last look at it as a technical review from an Addon Developer point of view? |
@rpl thanks. reply copied from bug#1323414:
|
I do like the way this is going, but its rather complicated and I think we should reduce the amount of copy and paste in an example. Is there are a library we can use for doing this, or can we abstract out some of the heavy lifting into a library that can be updated independent of this example? |
@andymckay I've another example (which I started before this one, but I chose to finalize after this one was completed) which uses BrowserFS to abstract the filesystem access (and I was thinking about trying to use localForage as another simpler abstraction), unfortunately one of the interesting part of this example is the MutableFileHandle and it is not a standard feature, and so I doubt that any of the existent abstraction take it into account (and they will not provide a nicer API to interfact with it). One reason why I chose to write this version first is that it is "library agnostic" and it provides an simple example of how to use the underlying features directly, nevertheless I think that the other example (which will use the abstractions) is going to be needed as well. |
@rpl lets close this in favour of the new library you are building out. |
This add-on shows how to read and save files using the IndexedDB API and the sub-set of the "File and Directory Entries API" supported on Firefox.