Skip to content
This repository was archived by the owner on Sep 15, 2021. It is now read-only.

Commit 11717fc

Browse files
committed
feat(plugin:fileTransfer): refractor out fileTransfer plugin from file plugin into its own module
1 parent 8d9e7fb commit 11717fc

File tree

15 files changed

+337
-97
lines changed

15 files changed

+337
-97
lines changed

demo/config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<author email="[email protected]" href="http://paolobernasconi.com">
1111
Paolo Bernasconi
1212
</author>
13-
<content src="http://140.233.187.163:8100" original-src="index.html"/>
13+
<content src="index.html"/>
1414
<access origin="*"/>
1515
<preference name="fullscreen" value="true"/>
1616
<preference name="webviewbounce" value="false"/>

demo/www/app/app.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ angular.module('demo', [
2020
'demo.facebook.ctrl',
2121
'demo.file.ctrl',
2222
'demo.fileOpener2.ctrl',
23+
'demo.fileTransfer.ctrl',
2324
'demo.flashlight.ctrl',
2425
'demo.geolocation.ctrl',
2526
'demo.globalization.ctrl',
@@ -220,6 +221,12 @@ angular.module('demo', [
220221
controller: "FileCtrl"
221222
})
222223

224+
.state('fileTransfer', {
225+
url: '/fileTransfer',
226+
templateUrl: 'app/fileTransfer/fileTransfer.html',
227+
controller: "FileTransferCtrl"
228+
})
229+
223230
.state('fileOpener2', {
224231
url: '/fileOpener2',
225232
templateUrl: 'app/fileOpener2/fileOpener2.html',
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
angular.module('demo.fileTransfer.ctrl', [])
2+
3+
.controller('FileTransferCtrl', function ($scope, $rootScope, $cordovaFileTransfer, $timeout) {
4+
5+
$scope.downloadFile = function () {
6+
document.addEventListener('deviceready', function () {
7+
var url = "http://cdn.wall-pix.net/albums/art-space/00030109.jpg";
8+
var fileDir = cordova.file.documentsDirectory + "testImage.png";
9+
10+
var download = $cordovaFileTransfer.download(url, fileDir).then(function (success) {
11+
console.log("success " + JSON.stringify(success));
12+
$timeout(function () {
13+
$scope.downloadProgress = 100
14+
}, 1000);
15+
}, function (error) {
16+
console.log("Error " + JSON.stringify(error));
17+
}, function (progress) {
18+
$timeout(function () {
19+
$scope.downloadProgress = (progress.loaded / progress.total) * 100;
20+
21+
});
22+
});
23+
24+
25+
if ($scope.downloadProgress > 0.1) {
26+
download.abort();
27+
}
28+
})
29+
};
30+
31+
32+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<ion-view title="File Transfer">
2+
<ion-content padding="true">
3+
4+
<button class="button button-block button-positive" ng-click="downloadFile()">Download File</button>
5+
6+
<div class="card" ng-if="downloadProgress">
7+
<div class="item item-text-wrap">
8+
Progress : {{downloadProgress}}%
9+
</div>
10+
</div>
11+
</ion-content>
12+
</ion-view>

demo/www/app/inAppBrowser/inAppBrowser.ctrl.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ angular.module('demo.inAppBrowser.ctrl', [])
88
location: "no"
99
};
1010

11-
$cordovaInAppBrowser.open('http://ngcordova.com', '_blank', options);
11+
$cordovaInAppBrowser.open('http://ngcordova.com', '_blank', options).then(function () {
12+
console.log("InAppBrowser opened http://ngcordova.com successfully");
13+
}, function (error) {
14+
console.log("Error: " + error);
15+
});
1216

1317
}, false);
1418
};

demo/www/app/menu.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@
7777
File
7878
</a>
7979

80+
<a class="item item-icon-right" ui-sref="fileTransfer">
81+
<i class="icon ion-ios7-cloud-download positive"></i>
82+
File Transfer
83+
</a>
84+
8085
<a class="item item-icon-right" ui-sref="fileOpener2">
8186
<i class="icon ion-folder"></i>
8287
FileOpener2

demo/www/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<script src="app/facebook/facebook.ctrl.js"></script>
5353
<script src="app/file/file.ctrl.js"></script>
5454
<script src="app/fileOpener2/fileOpener2.ctrl.js"></script>
55+
<script src="app/fileTransfer/fileTransfer.ctrl.js"></script>
5556
<script src="app/flashlight/flashlight.ctrl.js"></script>
5657
<script src="app/geolocation/geolocation.ctrl.js"></script>
5758
<script src="app/globalization/globalization.ctrl.js"></script>

demo/www/lib/ngCordova/dist/ng-cordova.js

Lines changed: 89 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,33 +1929,49 @@ angular.module('ngCordova.plugins.file', [])
19291929
return getAbsoluteFile(filePath);
19301930
},
19311931

1932-
downloadFile: function (source, filePath, trustAllHosts, options) {
1932+
download: function (source, filePath, options, trustAllHosts) {
19331933
var q = $q.defer();
1934-
var fileTransfer = new FileTransfer();
1934+
var ft = new FileTransfer();
19351935
var uri = encodeURI(source);
19361936

1937-
fileTransfer.onprogress = q.notify;
1938-
fileTransfer.download(uri, filePath, q.resolve, q.reject, trustAllHosts, options);
1937+
if (options && options.timeout !== undefined && options.timeout !== null) {
1938+
$timeout(function () {
1939+
ft.abort();
1940+
}, options.timeout);
1941+
options.timeout = null;
1942+
}
1943+
1944+
ft.onprogress = function (progress) {
1945+
q.notify(progress);
1946+
};
1947+
1948+
ft.download(uri, filePath, q.resolve, q.reject, trustAllHosts, options);
19391949
return q.promise;
19401950
},
19411951

1942-
uploadFile: function (server, filePath, options) {
1952+
upload: function (server, filePath, options, trustAllHosts) {
19431953
var q = $q.defer();
1944-
var fileTransfer = new FileTransfer();
1954+
var ft = new FileTransfer();
19451955
var uri = encodeURI(server);
19461956

1947-
if (options.timeout !== undefined && options.timeout !== null) {
1957+
if (options && options.timeout !== undefined && options.timeout !== null) {
19481958
$timeout(function () {
1949-
fileTransfer.abort();
1959+
ft.abort();
19501960
}, options.timeout);
19511961
options.timeout = null;
19521962
}
19531963

1954-
fileTransfer.onprogress = q.notify;
1955-
fileTransfer.upload(filePath, uri, q.resolve, q.reject, options);
1964+
ft.onprogress = function (progress) {
1965+
q.notify(progress);
1966+
};
1967+
1968+
q.promise.abort = function () {
1969+
ft.abort();
1970+
};
1971+
1972+
ft.upload(filePath, uri, q.resolve, q.reject, options, trustAllHosts);
19561973
return q.promise;
19571974
}
1958-
19591975
};
19601976

19611977
/*
@@ -2060,51 +2076,93 @@ angular.module('ngCordova.plugins.fileOpener2', [])
20602076
.factory('$cordovaFileOpener2', ['$q', function ($q) {
20612077

20622078
return {
2063-
2064-
open: function(file, type) {
2065-
2079+
open: function (file, type) {
20662080
var q = $q.defer();
20672081
cordova.plugins.fileOpener2.open(file, type, {
2068-
error: function(e) {
2082+
error: function (e) {
20692083
q.reject(e);
2070-
},
2071-
success: function() {
2084+
}, success: function () {
20722085
q.resolve();
20732086
}
20742087
});
20752088
return q.promise;
2076-
20772089
},
20782090

2079-
uninstall: function(pack) {
2080-
2091+
uninstall: function (pack) {
20812092
var q = $q.defer();
20822093
cordova.plugins.fileOpener2.uninstall(pack, {
2083-
error: function(e) {
2094+
error: function (e) {
20842095
q.reject(e);
2085-
},
2086-
success: function() {
2096+
}, success: function () {
20872097
q.resolve();
20882098
}
20892099
});
20902100
return q.promise;
2091-
20922101
},
20932102

2094-
appIsInstalled: function(pack) {
2095-
2103+
appIsInstalled: function (pack) {
20962104
var q = $q.defer();
20972105
cordova.plugins.fileOpener2.appIsInstalled(pack, {
2098-
success : function(res) {
2106+
success: function (res) {
20992107
q.resolve(res);
21002108
}
21012109
});
21022110
return q.promise;
2103-
21042111
}
2105-
21062112
};
2113+
}]);
2114+
2115+
// install : cordova plugin add org.apache.cordova.file-transfer
2116+
// link : https://github.com/apache/cordova-plugin-file-transfer/blob/master/doc/index.md
2117+
2118+
angular.module('ngCordova.plugins.fileTransfer', [])
2119+
2120+
.factory('$cordovaFileTransfer', ['$q', '$timeout', function ($q, $timeout) {
2121+
return {
2122+
download: function (source, filePath, options, trustAllHosts) {
2123+
var q = $q.defer();
2124+
var ft = new FileTransfer();
2125+
var uri = encodeURI(source);
2126+
2127+
if (options && options.timeout !== undefined && options.timeout !== null) {
2128+
$timeout(function () {
2129+
ft.abort();
2130+
}, options.timeout);
2131+
options.timeout = null;
2132+
}
21072133

2134+
ft.onprogress = function (progress) {
2135+
q.notify(progress);
2136+
};
2137+
2138+
ft.download(uri, filePath, q.resolve, q.reject, trustAllHosts, options);
2139+
return q.promise;
2140+
},
2141+
2142+
upload: function (server, filePath, options, trustAllHosts) {
2143+
var q = $q.defer();
2144+
var ft = new FileTransfer();
2145+
var uri = encodeURI(server);
2146+
2147+
if (options && options.timeout !== undefined && options.timeout !== null) {
2148+
$timeout(function () {
2149+
ft.abort();
2150+
}, options.timeout);
2151+
options.timeout = null;
2152+
}
2153+
2154+
ft.onprogress = function (progress) {
2155+
q.notify(progress);
2156+
};
2157+
2158+
q.promise.abort = function () {
2159+
ft.abort();
2160+
};
2161+
2162+
ft.upload(filePath, uri, q.resolve, q.reject, options, trustAllHosts);
2163+
return q.promise;
2164+
}
2165+
};
21082166
}]);
21092167

21102168
// install : cordova plugin add https://github.com/EddyVerbruggen/Flashlight-PhoneGap-Plugin.git
@@ -3627,6 +3685,8 @@ angular.module('ngCordova.plugins', [
36273685
'ngCordova.plugins.facebook',
36283686
'ngCordova.plugins.facebookAds',
36293687
'ngCordova.plugins.file',
3688+
'ngCordova.plugins.fileTransfer',
3689+
'ngCordova.plugins.fileOpener2',
36303690
'ngCordova.plugins.flashlight',
36313691
'ngCordova.plugins.flurryAds',
36323692
'ngCordova.plugins.ga',

demo/www/lib/ngCordova/dist/ng-cordova.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)