unlimited file storage in chrome app

2020-02-26 12:17发布

问题:

I want to save an unlimited number of files to the users hard drive, without making the user click through a dialog box.

The only documentation I have seen on using unlimited storage is here: https://developers.google.com/chrome/whitepapers/storage, which says that it is only available to Chrome apps.

All of the examples I have seen of the Chrome fileSystem API suggest that in order to create a new file and write to it, the user needs to obtain an entry object from the chrome.fileSystem.chooseEntry function, which means the user needs to go through a dialog box.

The regular fileSystem API looks like it can save a file to the user's extension sandbox without making the user go through a dialog box, but I'm wondering, does it have the "unlimitedStorage" permission that is only given to apps? It wouldn't shock me if only the Chrome API has that permission.

If I use the regular fileSystem API, I need to request the filesystem using window.webkitRequestFileSystem (I'm a little surprised window.requestFileSystem isn't working for me in Chrome), but the method takes type ("persistent" or "temporary"), and size (the bytes the app will require for storage) arguments.

You obviously don't do that in the Chrome API. However, the normal fileSystem API lets you request more storage when you need it using the Quota Management API, which is also documented on the page I linked above

And on https://developers.google.com/chrome/apps/docs/developers_guide#manifest, it says:

The "permissions" field lets you specify HTML5 permissions that the app requires. By specifying "unlimitedStorage" and "notifications", this app is able to use those HTML5 features without having to repeatedly ask the user for permission. During app installation, the user is told which permissions the app needs; installing the app implicitly grants those permissions for all pages whose URLs match those in the "apps" field of the manifest.

So I'm hoping that the "unlimitedStorage" permission simply means that I can just call window.webkitRequestFileSystem(window.PERSISTENT, anySize, successCallback), and instead of asking the user to raise the quota when it needs to, Chrome automatically does it.

Question 1: Does anyone know if that's the way it works?

However, if only the the chrome API has that "unlimitedStorage" permission, I could try creating a file entry with the regular API, and passing that entry into the Chrome API functions, therefore skipping the need for a dialog box, and gaining access to the "unlimitedStorage" permission.

Question 2: Is it possible for me to create the entry in one API and pass it to the other? I'm afraid the different API's might have different sandboxes.

The documentation on the fileSystem API's isn't great, so I figured I'd ask to see if anybody knew from previous experience. The fewer rabbit holes the better.

回答1:

First of all, to avoid confusion, what here is being called a "packaged app" should be called a "Chrome App".

If the blob is to be used by any other app, you must obtain a FileEntry via chrome.fileSystem.chooseEntry so the data will be written to the computer's file system where the user can access it. (You can obtain a directory that way from the user and save the retained entry, and use that without asking the user to make a choice every time.)

If the blob is only for use by that Chrome App, you can use the file API without calling chrome.fileSystem.chooseEntry.

As for limits, you're correct that unlimitedStorage allows unlimited storage. In my experiments, despite the documentation, I have never been able to get a Chrome App to ask the user to authorize a specific amount. I use that permission, and there seems to be no limit. The Google documentation I've read on this appears to be inaccurate.

If you're writing to the external file system, after you've called chrome.fileSystem.chooseEntry, there are no limits and you don't need unlimitedStorage permission.



回答2:

My solution.

main.js

var chooseDirectory = function() {
  chrome.app.window.create("choose_directory.html");
};

chrome.runtime.onInstalled.addListener(function(details) {
  if (details.reason == 'install') {
    chooseDirectory();
  }
};  

chrome.runtime.onMessageExternal.addListener(function(message, sender, sendResponse) {

  switch (message.type) {
  case 'saveBlob':
    var blob = new Blob([message.blob])

    chrome.storage.local.get('filesystemKey', function(items) {
      var fileSystemRef = items.fileSystemKey;

      chrome.fileSystem.restoreEntry(fileSystemRef, function(fileSystem) {

        fileSystem.getFile('test.txt', { create: true }, function(fileEntry) {

          fileEntry.createWriter(function(writer) {
            writer.write(blob);
          });   
        });             
      });           
    });  
    break;
  } 
});

after using the chrome filesystem APIs, you use the regular html5 filesystem API to create the file. As explained here you cannot call the chooseDirectory function from the background script, so you need to create an app window that can call it, which I do below.

choose_directory.js

window.addEventListener('load', function() {

  document.getElementById('input').addEventListener('click', function() {

    chrome.fileSystem.chooseEntry({type: 'openDirectory'}, function(entry) {
      var homeDirectory = chrome.fileSystem.retainEntry(entry);

      chrome.storage.local.set({'filesystemKey': homeDirectory}, function() {
        window.close();
      });           
    });     
  });   
}); 

choose_directory.html

<script src="choose_directory.js"></script>

You need to choose a directory where you will store your files.

<button id="input"> Chose a place to save your files</button>