Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Apps : How to save blob content to fileSystem in the background?

In Chrome Apps, I'm downloading a blob content from a server using JavaScript XHR (Angular $http GET in particular, with response type 'blob')

How should I save this to chrome application's file system?

Currently using an Angular wrapper on HTML5 filesystem API https://github.com/maciel310/angular-filesystem

I do not want to show user a popup (hence I can't use chrome.fileSystem. chooseEntry )

The chrome.fileSystem.requestFileSystem API is only supported by Kiosk-only apps. Hence I'm using HTML5 FileSystem API instead of chrome's.

I'm using following code to make XHR to fetch blob.

 $http({
          url: SERVER_URL+"/someVideo.mp4",
          method: "GET",
          responseType: "blob"
      }).then(function(response) {
          console.log(response);
          fileSystem.writeBlob(response.name, response).then(function() {
             console.log("file saved");
          }, function(err) {
              console.log(err);
          });
      }, function (response) {

      });

This is my writeBlob method

writeBlob: function(fileName, blob, append) {
    append = (typeof append == 'undefined' ? false : append);

    var def = $q.defer();

    fsDefer.promise.then(function(fs) {

        fs.root.getFile(fileName, {create: true}, function(fileEntry) {

            fileEntry.createWriter(function(fileWriter) {
                if(append) {
                    fileWriter.seek(fileWriter.length);
                }

                var truncated = false;
                fileWriter.onwriteend = function(e) {
                    //truncate all data after current position
                    if (!truncated) {
                        truncated = true;
                        this.truncate(this.position);
                        return;
                    }
                    safeResolve(def, "");
                };

                fileWriter.onerror = function(e) {
                    safeReject(def, {text: 'Write failed', obj: e});
                };

                fileWriter.write(blob);

            }, function(e) {
                safeReject(def, {text: "Error creating file", obj: e});
            });

        }, function(e) {
            safeReject(def, {text: "Error getting file", obj: e});
        });

    }, function(err) {
        def.reject(err);
    });

    return def.promise;
},

This shows SECURITY_ERR as It was determined that certain files are unsafe for access within a Web application, or that too many calls are being made on file resources.

What's the solution for this?

I've tried using --allow-file-access-from-files flag while launching app. It doesn't help.

like image 465
Vishwajeet Vatharkar Avatar asked Oct 18 '22 19:10

Vishwajeet Vatharkar


1 Answers

Chrome Application's sandbox storage doesn't allow files to be stored in root directory (i.e. / )

Modify the code to save it in a specific sub-directory under it. For example -

fileSystem.writeBlob("/new"+response.name, response).then(function() {
             console.log("file saved");
          }, function(err) {
              console.log(err);
          });

This would successfully save the file under /new/ directory.

like image 51
Vishwajeet Vatharkar Avatar answered Nov 15 '22 02:11

Vishwajeet Vatharkar