0

I have this fiddle (please use Google Chrome when running the fiddle).

I would like to know how to properly implement this using HTML 5 file system API.

Use Case: When appending something in the sandbox filesystem API, I would like to check first if the file size of the existing file exceeds a particular threshold level. If so, rename or delete the file.

$(document).ready(function () {
    var messages = "A sample text!";
    navigator.webkitPersistentStorage.requestQuota(1024 * 1024, function (availBytes) {
        window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
        window.requestFileSystem(window.PERSISTENT, availBytes, function (fs) {
            fs.root.getFile("sample.txt", {
                create: true
            }, function (file) {
                console.log("file.name =" + file.name);
                console.log("file.fullPath =" + file.fullPath);

                file.getMetadata(function (md) {
                    //1. check the file size first (md.size)
                    //2. if file size > 100KB
                    //       2.1 delete the existing file or rename it
                    //       2.2 create a new file
                    //   else
                    //       3 create a new file
                }, this.onError);

                file.createWriter(function (fileWriter) {

                    fileWriter.onerror = function (e) {
                        console.log('Write failed: ' + e.toString());
                    };
                    fileWriter.seek(fileWriter.length); // Start write position at EOF.
                    var blob = new Blob([messages], {
                        type: 'plain/text'
                    });
                    fileWriter.write(blob);
                }, this.onError);
            }, this.onError);
        }, this.onError); //end window.requestFileSystem
    }, function (e) {
        console.log('Error', e);
    });

    function onError(e) {
        var msg = '';

        switch (e.code) {
            case FileError.QUOTA_EXCEEDED_ERR:
                msg = 'QUOTA_EXCEEDED_ERR';
                break;
            case FileError.NOT_FOUND_ERR:
                msg = 'NOT_FOUND_ERR';
                break;
            case FileError.SECURITY_ERR:
                msg = 'SECURITY_ERR';
                break;
            case FileError.INVALID_MODIFICATION_ERR:
                msg = 'INVALID_MODIFICATION_ERR';
                break;
            case FileError.INVALID_STATE_ERR:
                msg = 'INVALID_STATE_ERR';
                break;
            default:
                msg = 'Unknown Error';
                break;
        };

        console.log('Error: ' + msg);
    }
});

I used the

 file.getMetadata(function (md) {
            //1. check the file size first (md.size)
            //2. if file size > 100KB
            //       2.1 delete the existing file or rename it
            //       2.2 create a new file
            //   else
            //       3 create a new file
        }, this.onError);

But I am confuse how to prevent the createwriter from running since they are on the same callback function.

I would like to know how to properly sequence my code such that, I will check first the file size of the existing file prior to appending.

There are a lot of callback functions on the FileEntry interface and I am getting confused if I would put them or use them all at once.

  fs.root.getFile('sample.txt', {create: true, exclusive: true}, function(fileEntry) {
    //fileEntry.getMetadata(successCallback, opt_errorCallback);
    //fileEntry.remove(successCallback, opt_errorCallback);
    //fileEntry.moveTo(dirEntry, opt_newName, opt_successCallback, opt_errorCallback);
    //fileEntry.copyTo(dirEntry, opt_newName, opt_successCallback, opt_errorCallback);
    //fileEntry.getParent(successCallback, opt_errorCallback);
    //fileEntry.toURL(opt_mimeType);
    }, errorHandler);

  }

file.createWriter(function (fileWriter) {})
1
  • The Filesystem API seems to be a bunch of callback functions..does this mean that in order to accomplish what I wanted to do, I will have to manipulate everything using those callback function? I just want to confirm this though. Commented Aug 27, 2014 at 7:05

1 Answer 1

1

You can try moving the call to file.createWriter inside the callback which gets the results of the getMetadata call. That way, you will always check the size before proceeding to the call to create the file. The idea is that you have a bunch of async callbacks that you 'chain' together to carry out the stream of processing that you require. If you want to read more, look up 'javascript async patterns' in your search engine of choice. Here is one such page" http://tech.pro/blog/1402/five-patterns-to-help-you-tame-asynchronous-javascript

Sign up to request clarification or add additional context in comments.

1 Comment

The tech.pro website went offline a while back so the article referenced is no longer retrievable. Here's a cached version from the Internet Archive: web.archive.org/web/20150603210740/http://tech.pro/blog/1402/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.