apache / cordova-plugin-file-transfer

Apache Cordova File Transfer Plugin
https://cordova.apache.org/
Apache License 2.0
595 stars 888 forks source link

Cordova 11 > Android incompatible #345

Closed michaelpeterlee closed 1 year ago

michaelpeterlee commented 1 year ago

Does not work with Cordova 11 Android; cordova build android fails.

Errors due to cordova-plugin-whitelist missing.

Include cordova-plugin-whitelist and cordova build android fails due to an incompatibility with cordova-plugin-whitelist.

michaelpeterlee commented 1 year ago

Contrary to the README.MD, to install you must reference the repo not the Cordova plugin registry:

cordova plugin add https://github.com/apache/cordova-plugin-file-transfer.git.

Can you please update the readme or the Cordova plugin registry.

michaelpeterlee commented 1 year ago

Additional to this, we can no-longer download() due to error, below.

{
  "cordova-plugin-file": "^7.0.0",
  "cordova-plugin-file-transfer": "^1.7.1",
  "cordova-plugin-ionic-webview": "^5.0.0",
  ...
}

We downgraded dependency cordova-plugin-file@6.0.2 to no-avail also.

const oFileEntry = _oFileSystem.root.getFile('myfile.png',{create:true,exclusive:false} ...
const sFullPath = oFileEntry.toUrl();
const sURL = 'https://mydomain.com/myfile.png';

oFileTransfer = new FileTransfer();

oFileTransfer.download(
  sURL,
  sFullPath,
  function success(oFile) {
    alert("That's all folks!");
  },
  fFail,
  false,
  null
);

// fFail() presents the following error object:

FileTransferError =
{
  body: null,
  code: 3,
  exception: null,
  http_status: 401,
  source: "https://mydomain.com/myfile.png",
  target: "file:///data/user/0/com.mydomain.myapp/files/myfile.png"
}
Mister-CK commented 1 year ago

I just remove the code that refers to the whitelist plugin with a custom hook. Cordova-anrdoid 10 doesn't use the whitelist plugin anymore so I figure that code has been made redundant. Only problem I am having is that the hook is messing up the file. So I might just make a fork and remove the unwanted code myself. Although I prefer a hook. Sections to be removed: const firstSectionOfCodeToRemove = "import org.apache.cordova.Whitelist;" second section: const secondSectionOfCodeToRemove = "if (shouldAllowRequest == null) { try { Method gwl = webView.getClass().getMethod("getWhitelist"); Whitelist whitelist = (Whitelist)gwl.invoke(webView); shouldAllowRequest = whitelist.isUrlWhiteListed(source); } catch (NoSuchMethodException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } }"

opindia123 commented 1 year ago

@Mister-CK can you share your hook , it will be very helpful, thanks in advance.

pieshop commented 1 year ago

Here is a hook I did:

config.xml: <hook type="after_prepare" src="hooks/hook-android-fix-file-transfer.js" info="Comment out whitelist code" />

hook-android-fix-file-transfer.js:

const fs = require('fs-extra');
const path = require('path');

const DIR = path.join(__dirname, '../');

/**
 * This hook removes whitelist from FileTransfer.java
 * see : https://github.com/apache/cordova-plugin-file-transfer/issues/345
 */
module.exports = function main(ctx) {
    if (!ctx.opts.platforms.includes('android')) return;

    console.log('[hook] Fix file transfer on android');

    const file = `${DIR}/platforms/android/app/src/main/java/org/apache/cordova/filetransfer/FileTransfer.java`;
    let code = `import org.apache.cordova.Whitelist;`
    let commentedOut = `//  Commented out by hook-android-fix-file-transfer.js
//  import org.apache.cordova.Whitelist;`

    let str = fs.readFileSync(file, 'utf8');
    str = str.replace(code, commentedOut);

    code = `        if (shouldAllowRequest == null) {
            try {
                Method gwl = webView.getClass().getMethod("getWhitelist");
                Whitelist whitelist = (Whitelist)gwl.invoke(webView);
                shouldAllowRequest = whitelist.isUrlWhiteListed(source);
            } catch (NoSuchMethodException e) {
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
        }`
    commentedOut = `//  Commented out by hook-android-fix-file-transfer.js
//          if (shouldAllowRequest == null) {
//            try {
//                Method gwl = webView.getClass().getMethod("getWhitelist");
//                Whitelist whitelist = (Whitelist)gwl.invoke(webView);
//                shouldAllowRequest = whitelist.isUrlWhiteListed(source);
//            } catch (NoSuchMethodException e) {
//            } catch (IllegalAccessException e) {
//            } catch (InvocationTargetException e) {
//            }
//        }`

    str = str.replace(code, commentedOut);

    fs.writeFileSync(file, str, 'utf8');
};
pixellet14 commented 1 year ago

Here is a hook I did:

config.xml: <hook type="after_prepare" src="hooks/hook-android-fix-file-transfer.js" info="Comment out whitelist code" />

hook-android-fix-file-transfer.js:

const fs = require('fs-extra');
const path = require('path');

const DIR = path.join(__dirname, '../');

/**
 * This hook removes whitelist from FileTransfer.java
 * see : https://github.com/apache/cordova-plugin-file-transfer/issues/345
 */
module.exports = function main(ctx) {
    if (!ctx.opts.platforms.includes('android')) return;

    console.log('[hook] Fix file transfer on android');

    const file = `${DIR}/platforms/android/app/src/main/java/org/apache/cordova/filetransfer/FileTransfer.java`;
    let code = `import org.apache.cordova.Whitelist;`
    let commentedOut = `//  Commented out by hook-android-fix-file-transfer.js
//  import org.apache.cordova.Whitelist;`

    let str = fs.readFileSync(file, 'utf8');
    str = str.replace(code, commentedOut);

    code = `        if (shouldAllowRequest == null) {
            try {
                Method gwl = webView.getClass().getMethod("getWhitelist");
                Whitelist whitelist = (Whitelist)gwl.invoke(webView);
                shouldAllowRequest = whitelist.isUrlWhiteListed(source);
            } catch (NoSuchMethodException e) {
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
        }`
    commentedOut = `//  Commented out by hook-android-fix-file-transfer.js
//          if (shouldAllowRequest == null) {
//            try {
//                Method gwl = webView.getClass().getMethod("getWhitelist");
//                Whitelist whitelist = (Whitelist)gwl.invoke(webView);
//                shouldAllowRequest = whitelist.isUrlWhiteListed(source);
//            } catch (NoSuchMethodException e) {
//            } catch (IllegalAccessException e) {
//            } catch (InvocationTargetException e) {
//            }
//        }`

    str = str.replace(code, commentedOut);

    fs.writeFileSync(file, str, 'utf8');
};

Hi! thanks for your solution but it isn't working for me :'(

pixellet14 commented 1 year ago

Does not work with Cordova 11 Android; cordova build android fails.

Errors due to cordova-plugin-whitelist missing.

Include cordova-plugin-whitelist and cordova build android fails due to an incompatibility with cordova-plugin-whitelist.

Have you found a solution yet? if yes then please can you share with me? my project is stuck :'(

michaelpeterlee commented 1 year ago

We do not use Cordova 11 to publish Android due to this issue.

Nice to see some engagement. I will be hands-on here in ~3mths in light of no solution.

pixellet14 commented 1 year ago

finally found a plugin for file transfer and it's working!!! https://www.npmjs.com/package/cordova-plugin-ns0m-file-transfer

tried and tested

michaelpeterlee commented 1 year ago

finally found a plugin for file transfer and it's working!!! https://www.npmjs.com/package/cordova-plugin-ns0m-file-transfer

tried and tested

I still receive HTTP 401 error with that fork.

michaelpeterlee commented 1 year ago

@pieshop Hook does not work; Is not relevant to current version of FileTransfer.java on /master.

michaelpeterlee commented 1 year ago

HTTP 401 is a red herring and an incorrect response; FileTransfer.download() destination parameter must change from oFile.toURL() to oFile.nativeURL.

Protocol/path returned from toURL() has changed as inherited from cordova-file plugin, as referenced: https://github.com/apache/cordova-plugin-file-transfer/issues/339

Thanks to https://github.com/sameteyuboglu/cordova-plugin-file-transfer/commit/0c0bd2d65e520674b3ee6210feaab1c373a07f2d as I took that for a spin.

Mugunthan93 commented 1 year ago

Myself working in Ionic 6 & Capacitor 4,

Give me any solution for this, Because of this, I cannot able to publish my application.

rolinger commented 1 year ago

@pixellet14

finally found a plugin for file transfer and it's working!!! https://www.npmjs.com/package/cordova-plugin-ns0m-file-transfer

tried and tested

I found that in installed it, it installed clean, but its not working with cordova-plugin-file-downloader

Did you find it to be an equal replacement for the master file-transfer or there changes you needed to make in your code?

rolinger commented 1 year ago

For anyone still needing a solution, see this post:

https://github.com/apache/cordova-plugin-file-transfer/issues/339#issuecomment-1450747708