From 9dfd9371ec5171a634917398878a87317661bd42 Mon Sep 17 00:00:00 2001 From: Volodymyr Taliar Date: Tue, 12 Oct 2021 13:06:54 +0300 Subject: [PATCH] Fixed compatibility with standard FileTransfer plugin --- package.json | 6 +-- plugin.xml | 8 ++-- scripts/hooks/afterCompile.js | 76 ++++++++++++++++++++++++++++++++ src/android/BBDFileTransfer.java | 18 ++++---- src/ios/CDVFileTransferBD.m | 8 ++-- 5 files changed, 96 insertions(+), 20 deletions(-) create mode 100644 scripts/hooks/afterCompile.js diff --git a/package.json b/package.json index 08e4ff2..87628f2 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cordova-plugin-bbd-file-transfer", - "version": "1.1.0", + "version": "1.2.0", "description": "BlackBerry Dynamics Cordova File Transfer Plugin", "types": "./types/index.d.ts", "license": "SEE LICENSE IN LICENSE", @@ -14,12 +14,12 @@ }, "repository": { "name": "cordova-plugin-bbd-file-transfer", - "version": "1.1.0", + "version": "1.2.0", "private": true, "type": "git", "url": "https://github.com/blackberry/BlackBerry-Dynamics-Cordova-Plugins/tree/file-transfer" }, - "bugs": "https://github.com/blackberry/BlackBerry-Dynamics-Cordova-Plugins/tree/file-transfer/issues", + "bugs": "https://github.com/blackberry/BlackBerry-Dynamics-Cordova-Plugins/issues", "keywords": [ "cordova", "file", diff --git a/plugin.xml b/plugin.xml index a2a06f0..bf58148 100755 --- a/plugin.xml +++ b/plugin.xml @@ -1,7 +1,7 @@ - + cordova-plugin-bbd-file-transfer BlackBerry Dynamics Cordova File Transfer Plugin SEE LICENSE IN LICENSE @@ -35,8 +35,7 @@ Original Cordova FileTransfer plugin link: https://github.com/apache/cordova-plugin-file-transfer - + @@ -81,4 +80,5 @@ + diff --git a/scripts/hooks/afterCompile.js b/scripts/hooks/afterCompile.js new file mode 100644 index 0000000..0b73a4b --- /dev/null +++ b/scripts/hooks/afterCompile.js @@ -0,0 +1,76 @@ +#!/usr/bin/env node + +/* Copyright (c) 2021 BlackBerry Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +module.exports = function(context) { + + var fs = require('fs'), + path = require('path'), + cmdPlatforms = context.opts.platforms, + projectRoot = context.opts.projectRoot, + platformAndroidRoot = path.join(projectRoot, 'platforms', 'android'), + platformiOSRoot = path.join(projectRoot, 'platforms', 'ios'); + + if (cmdPlatforms.includes('android') && fs.existsSync(platformAndroidRoot)) { + handlePlatformCordovaPlugins(path.join(platformAndroidRoot, 'platform_www', 'cordova_plugins.js')); + handlePlatformCordovaPlugins( + path.join(platformAndroidRoot, 'app', 'src', 'main', 'assets', 'www', 'cordova_plugins.js') + ); + } + + if (cmdPlatforms.includes('ios') && fs.existsSync(platformiOSRoot)) { + handlePlatformCordovaPlugins(path.join(platformiOSRoot, 'platform_www', 'cordova_plugins.js')); + handlePlatformCordovaPlugins(path.join(platformiOSRoot, 'www', 'cordova_plugins.js')); + } + + // Removes Window "clobbers", "merges" from cordova-plugin-file-transfer to avoid conflicts with cordova-plugin-bbd-file-transfer + // when both plugins are added + // DEVNOTE: cordova-plugin-file-transfer API's can be accessed using cordova.require('cordova-plugin-file-transfer.someMethod') + function handlePlatformCordovaPlugins(cordovaPluginsPath) { + var cordovaPluginsContent = fs.readFileSync(cordovaPluginsPath, 'utf-8'), + startString = 'module.exports = ', + endString = '];', + startStringIndex = cordovaPluginsContent.indexOf(startString), + endStringIndex = cordovaPluginsContent.indexOf(endString); + + // Get plugins list from cordova_plugin.js + var pluginsListString = cordovaPluginsContent.substring( + startStringIndex + startString.length, endStringIndex + endString.length - 1 + ), + pluginsList = JSON.parse(pluginsListString); + + // Update propertires for cordova-plugin-file-transfer entries + var updatedPluginList = pluginsList.map(function(plugin) { + if (plugin.pluginId === 'cordova-plugin-file-transfer') { + var updatedPlugin = Object.assign({}, plugin); + delete updatedPlugin.clobbers; + delete updatedPlugin.merges; + updatedPlugin.runs = 'true'; + + return updatedPlugin; + } + + return plugin; + }), + updatedPluginListString = JSON.stringify(updatedPluginList, null, 4); + + fs.writeFileSync( + cordovaPluginsPath, cordovaPluginsContent.replace(pluginsListString, updatedPluginListString) + ); + } + +}; diff --git a/src/android/BBDFileTransfer.java b/src/android/BBDFileTransfer.java index 0f3b2c9..d33818a 100755 --- a/src/android/BBDFileTransfer.java +++ b/src/android/BBDFileTransfer.java @@ -1,5 +1,5 @@ /* - Copyright (c) 2020 BlackBerry Limited. All Rights Reserved. + Copyright (c) 2021 BlackBerry Limited. All Rights Reserved. Some modifications to the original Cordova FileTransfer plugin from https://github.com/apache/cordova-plugin-file-transfer/ @@ -168,12 +168,12 @@ public boolean execute(String action, JSONArray args, final CallbackContext call * args[8] headers A map of header name/header values * args[9] id Id of this requested object * args[10] httpMethod HTTP method to use - either PUT or POST - * + * * @return FileUploadResult containing result of upload request */ private void upload(final String source, final String target, JSONArray args, CallbackContext callbackContext) throws JSONException { LOG.d(LOG_TAG, "upload " + source + " to " + target); - + final String objectId = args.getString(9); final RequestContext context = new RequestContext(source, target, callbackContext); @@ -640,8 +640,8 @@ public boolean isAuthenticationRequested( httpRequest.setHeader("Accept-Encoding", "gzip"); synchronized (context) { - context.httpRequest = httpRequest; - } + context.httpRequest = httpRequest; + } final HttpResponse response = httpclient.execute(httpRequest); httpStatus = response.getStatusLine().getStatusCode(); @@ -706,8 +706,8 @@ public boolean isAuthenticationRequested( if (context.aborted) { throw new IOException("download: force aborted"); } - context.targetFile = file; - } + context.targetFile = file; + } LOG.d(LOG_TAG, "Saved file: " + filePath); @@ -722,7 +722,7 @@ public boolean isAuthenticationRequested( progress.setLoaded(bytesLoaded); PluginResult progressResult = new PluginResult(PluginResult.Status.OK, progress.toJSONObject()); progressResult.setKeepCallback(true); - context.sendPluginResult(progressResult); + context.sendPluginResult(progressResult); } final JSONObject fileEntry = new JSONObject(); @@ -886,7 +886,7 @@ private static FileUtils getFilePlugin(CordovaWebView webView) { } } if (pm != null) { - filePlugin = pm.getPlugin("File"); + filePlugin = pm.getPlugin("BBDFile"); if (filePlugin != null && (filePlugin instanceof FileUtils)) { return (FileUtils)filePlugin; } diff --git a/src/ios/CDVFileTransferBD.m b/src/ios/CDVFileTransferBD.m index 5579754..2abefcd 100755 --- a/src/ios/CDVFileTransferBD.m +++ b/src/ios/CDVFileTransferBD.m @@ -300,7 +300,7 @@ - (CDVFileTransferDelegateBD*)delegateForUploadCommand:(CDVInvokedUrlCommand*)co delegate.source = source; delegate.target = server; delegate.trustAllHosts = trustAllHosts; - delegate.filePlugin = [self.commandDelegate getCommandInstance:@"File"]; + delegate.filePlugin = [self.commandDelegate getCommandInstance:@"BBDFile"]; delegate.chunkedMode = chunkedMode; return delegate; @@ -339,7 +339,7 @@ - (void)fileDataForUploadCommand:(CDVInvokedUrlCommand*)command if (sourceURL) { // Try to get a CDVFileSystem which will handle this file. // This requires talking to the current CDVFile plugin. - fs = [[self.commandDelegate getCommandInstance:@"File"] filesystemForURL:sourceURL]; + fs = [[self.commandDelegate getCommandInstance:@"BBDFile"] filesystemForURL:sourceURL]; } if (fs) { __weak CDVFileTransferBD* weakSelf = self; @@ -445,7 +445,7 @@ - (void)download:(CDVInvokedUrlCommand*)command */ target = [target stringByReplacingOccurrencesOfString:@"//" withString:@"/"]; - targetURL = [[self.commandDelegate getCommandInstance:@"File"] fileSystemURLforLocalPath:target].url; + targetURL = [[self.commandDelegate getCommandInstance:@"BBDFile"] fileSystemURLforLocalPath:target].url; } else { targetURL = [NSURL URLWithString:target]; @@ -492,7 +492,7 @@ - (void)download:(CDVInvokedUrlCommand*)command delegate.target = [targetURL absoluteString]; delegate.targetURL = targetURL; delegate.trustAllHosts = trustAllHosts; - delegate.filePlugin = [self.commandDelegate getCommandInstance:@"File"]; + delegate.filePlugin = [self.commandDelegate getCommandInstance:@"BBDFile"]; delegate.backgroundTaskID = [[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler:^{ [delegate cancelSessionTask:delegate.sessionTaskConnection];