disusered / cordova-safe

File encryption for Cordova.
MIT License
35 stars 22 forks source link

No callback called with encrypt #8

Closed marc-ed-raffalli closed 8 years ago

marc-ed-raffalli commented 8 years ago

Neither of the callbacks provided to safe.encrypt are getting called.

Info:

OS: Windows 7
Device:
    OnePlus One
    Android version: 5.0.2

$ cordova --version
    5.1.1
$ cordova plugins list
    cordova-plugin-file 2.1.0 "File"
    cordova-plugin-whitelist 1.0.0 "Whitelist"
    cordova-safe 1.1.1 "Safe"

Code to reproduce the issue:

var
    onDeviceReady = function () {
        // create a file in the persistent storage
        window.requestFileSystem(window.PERSISTENT, 5 * 1024 * 1024, onInitFs, onError);
    },
    onError = function (e) {
        console.error('Error:', e, e.code);
    },
    onInitFs = function (fs) {
        console.log('Opened file system: ' + fs.name);

        fs.root.getFile('userData.json', {create: true, exclusive: false}, function (fileEntry) {
            fileEntry.createWriter(function (fileWriter) {
                fileWriter.onwriteend = onFileWriteComplete;
                fileWriter.onerror = onError;

                // Write a file containing some test data
                fileWriter.write(new Blob(
                        [JSON.stringify({some: 'data'})],
                        {type: 'application/json'})
                );
            }, onError);
        }, onError);
    },
    onFileWriteComplete = function (file) {
        // here the file should be created and available on the device
        var safe = cordova.plugins.disusered.safe,
            key = 'someKey',
            onEncryptSuccess = function (encryptedFile) {
                console.log('Encrypted file: ' + encryptedFile);
            };
        // file.target.localURL value is "cdvfile://localhost/persistent/userData.json"
        safe.encrypt(file.target.localURL, key, onEncryptSuccess, onError);
    };

document.addEventListener('deviceready', onDeviceReady, false);

In the code above onEncryptSuccess or onError should be called. The function onFileWriteComplete is called and the file exists on the device with the rights: -rw-rw----.

disusered commented 8 years ago

On a stock device and cordova install on 5.0 with your sample code I get:

/persistent/userData.json: open failed: ENOENT (No such file or directory)

Cordova's Android file system layout is much more complex than the Java implementation. This is also a reason for #7. I'm working on it but it will probably be another day or two!

disusered commented 8 years ago

This seems to be an issue with convention introduced in 3.x. Thanks much for the debug information, it was super helpful! Your example code is now running as-is, I pushed the update a few minutes ago so let me know if it works.

References:

marc-ed-raffalli commented 8 years ago

Glad it helped, thanks a lot for the fix :) Just tried on the device, it seems all good.

disusered commented 8 years ago

Awesome, great to hear!

manojbhardwaj commented 6 years ago

plugin is not working in ionic 3 already installed but still showing i have to installed the plugin