0

All,

I might have found a bug in Monaca Debugger.

I am using the following code to get a persistent or temporary storage:

window.requestFileSystem(__storageType, _size, 
    function(_fileSystem) { // Success
        logger.info("initFilesystem: _fileSystem.root: " + _fileSystem.root.toURL());
            _deferred.resolve(_fileSystem);
        }, function(_error) { // Failure
            _deferred.reject(_error);
        });

Where the files are correctly managed on both Android and iOS. However, the path is not correct on Android and persistent storage, where I assume that it is a bug in how Monaca Debugger gives paths to the cordova plugin (note that I have not tried yet to deploy to android and verify without the debugger).

See below for the paths I get for Android and iOS, where I only get root directory on Android for persistent (note that on all examples the files are managed correctly, it is only the path for Android and persistent that is not correct).

Is this a bug, or a feature I need to manage in code?

Persistent file paths:

  • iOS: _fileSystem.root: file:///var/mobile/Containers/Data/Application/43F2E2CE-5F06-43F0-AE1C-4DD35EFF723A/Documents/
  • Android: _fileSystem.root: file:///storage/emulated/0/

Temporary file paths:

  • iOS: _fileSystem.root: file:///var/mobile/Containers/Data/Application/43F2E2CE-5F06-43F0-AE1C-4DD35EFF723A/tmp/
  • Android: _fileSystem.root: file:///storage/emulated/0/Android/data/mobi.monaca.debugger/cache/

Versions:

  • iOS version: 8.1.2
  • iOS Monaca Debugger version: 3.1.1
  • Android version: 4.2.2
  • Android Monaca Debugger version: 3.1.1

Update 2015-02-04:

The file object used (simplified code):

function FileIO() {
if (false === (this instanceof FileIO)) { return new FileIO(); } // Enforce that every time a constructor function is called, this function is invoked properly using the new operator.

/** @public @method initFilesystem.
 * @desc Initialize filesystem
 * @param {boolean} Persistent = true, Temporary = false. Must be boolean. Defaults to true.
 * @param {size} Size in bytes requested. Must be number. Defaults to 0 bytes.
 * @return Promise (see examples above). */
FileIO.prototype.initFilesystem = function(_persistent, _size) {
    /** @private */ var _deferred = new jQuery.Deferred(); // Used for object Promise.
    /** @private */ var _err = { code: null };
    if (__status != STATUS_INITIALIZED) { // Only perform if not already initialized.
        if (typeof _persistent !== 'boolean') { _persistent = true; } // Value must be boolean. Default to true if not set correctly.
        if (typeof _size !== 'number') { _size = 0; } // Value must be number. Default to 0 bytes if not set correctly.
        (_persistent == true) ? __storageType = window.PERSISTENT : __storageType = window.TEMPORARY;
        __status = STATUS_INITIALIZING;
        try {
            window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
            window.storageInfo = window.storageInfo || window.webkitStorageInfo;
            window.requestFileSystem(__storageType, _size, 
                function(_fileSystem) { // Success
                    __fileSystem = _fileSystem;
                    __status = STATUS_INITIALIZED;
                    _deferred.resolve(_fileSystem);
                }, function(_error) { // Failure
                    __status = STATUS_NO_FILESYSTEM;
                    _deferred.reject(_error);
                });
        } catch (_error) {
            __status = STATUS_NO_FILESYSTEM;
            _deferred.reject(_error);
        }
    } else {
        _err.code = FILESYSTEM_ALREADY_INITIALIZED;
        _deferred.reject(_err);
    }

    return _deferred.promise();
};

/** @public @method createFile.
 * @desc Create file.
 * @param {string} (path/)filename. Must be string. Defaults to "file.txt".
 * @return Promise (see examples above). */
FileIO.prototype.createFile = function(_fileName) {
    /** @private */ var _err = { code: null };
    /** @private */ var _deferred = new jQuery.Deferred(); // Used for object Promise.
    if (typeof _fileName !== 'string') { _fileName = "file.txt"; } // Default to "file.txt" if not correct.
    if (__status != STATUS_NO_FILESYSTEM) { // Object must be initialized.
        try {
            __fileSystem.root.getFile(_fileName, { create: true, exclusive: true },
                function(_fileEntry) { // Success
                    _deferred.resolve(_fileEntry);
                }, function(_error) { // Failure
                    _deferred.reject(_error);
                });
        } catch (_error) {
            _deferred.reject(_error);
        }
    } else {
        _err.code = FILESYSTEM_NOT_INITIALIZED;
        _deferred.reject(_err);
    }

    return _deferred.promise();
};

/** @public @constructor init.
 * @desc Constructor for the class.
 * @param N/A.
 * @return N/A. */
FileIO.prototype.init = function() {
};

// Construct
this.init();

}

Initializion:

var appFileSystem = new FileIO();
appFileSystem.initFilesystem(true); // Persistent.
appFileSystem.createFile("testfile.txt").done(function(fileEntry) {  console.log("createFile: done. fileEntry: "+JSON.stringify(fileEntry)); }).fail(function(error) { console.log("createFile: fail. error.code: "+error.code); });

With the above, the FileIO object is created, filesystem initialized and file created, where the persistent storage is placed in root (tested and verified for Cordova 4 according to latest Monaca update), while non-persistent is placed in right directory.

Sven
  • 1
  • 1

0 Answers0