Skip to content

Commit

Permalink
Fixed compatibility with standard FileTransfer plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
volodymyrtaliar committed Oct 12, 2021
1 parent 379f27f commit 9dfd937
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 20 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Copyright (c) 2020 BlackBerry Limited. All Rights Reserved.
Copyright (c) 2021 BlackBerry Limited. All Rights Reserved.
Some modifications to the original cordova-plugin-file-transfer
from https://github.com/apache/cordova-plugin-file-transfer/
Expand All @@ -23,7 +23,7 @@
under the License.
-->

<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" id="cordova-plugin-bbd-file-transfer" version="1.1.0">
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" id="cordova-plugin-bbd-file-transfer" version="1.2.0">
<name>cordova-plugin-bbd-file-transfer</name>
<description>BlackBerry Dynamics Cordova File Transfer Plugin</description>
<license>SEE LICENSE IN LICENSE</license>
Expand All @@ -35,8 +35,7 @@
Original Cordova FileTransfer plugin link: https://github.com/apache/cordova-plugin-file-transfer
</info>

<dependency id="cordova-plugin-bbd-file"
url="git+https://github.com/blackberry/blackberry-dynamics-cordova-plugins#file" version="^1.1.0" />
<dependency id="cordova-plugin-bbd-file" url="git+https://github.com/blackberry/blackberry-dynamics-cordova-plugins#file" version="^1.2.0" />

<js-module src="www/FileTransferError.js" name="FileTransferError">
<clobbers target="window.FileTransferError" />
Expand Down Expand Up @@ -81,4 +80,5 @@
</platform>

<hook type="after_plugin_install" src="scripts/hooks/afterPluginInstall.js" />
<hook type="after_compile" src="scripts/hooks/afterCompile.js" />
</plugin>
76 changes: 76 additions & 0 deletions scripts/hooks/afterCompile.js
Original file line number Diff line number Diff line change
@@ -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)
);
}

};
18 changes: 9 additions & 9 deletions src/android/BBDFileTransfer.java
Original file line number Diff line number Diff line change
@@ -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/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions src/ios/CDVFileTransferBD.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit 9dfd937

Please sign in to comment.