Snippets
Created by
Tomer Cagan
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | /**
* Created by Tomer on 2016-12-06.
*/
(function() {
angular.module('myApp.services.saveToGallery', [])
.factory("SaveToGalleryService", SaveToLibraryService);
SaveToLibraryService.$inject = ["$q"];
function SaveToLibraryService($q) {
return {
saveToGallery: downloadFile
};
//First step check parameters mismatch and checking network connection if available call download function
function downloadFile(url, folderName, fileName, successCallback, errorCallback) {
//Parameters mismatch check
if (!url || !folderName || !fileName) {
if (errorCallback) {
errorCallback("invalid arguments");
}
}
else {
//checking Internet connection availablity
var networkState = navigator.connection.type;
if (networkState == Connection.NONE) {
if (errorCallback) {
errorCallback("not internet connection");
}
} else {
download(url, folderName, fileName)
.then(function onDlSuccess(fileEntry) {
refreshMedia.refresh(fileEntry.nativeURL);
if (successCallback) {
successCallback(true, fileEntry);
}
}, function onDlError(err) {
if (errorCallback) {
errorCallback(err);
}
});
}
}
}
function download(url, folderName, fileName) {
var defer = $q.defer();
var downloadLink = encodeURI(url),
ext = downloadLink.substr(downloadLink.lastIndexOf('.') + 1); //Get extension of URL
//step to request a file system
window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, fileSystemSuccess, fileSystemFail);
function fileSystemSuccess(dir) {
console.log("fileSystemSuccess", dir);
dir.getDirectory(folderName, { create: true, exclusive: false }, onDirectorySuccess, onDirectoryFail);
}
function fileSystemFail(evt) {
//Unable to access file system
console.log("fileSystemFail", evt.target.error.code);
defer.reject(evt.target.error);
}
function onDirectorySuccess(dir) {
console.log("onDirectorySuccess", dir);
var fp = dir.toURL(); // Returns fill path of local directory
fp = fp + "/" + fileName + "." + ext; // fullpath and name of the file which we want to give
// download function call
filetransfer(downloadLink, fp);
}
function onDirectoryFail(error) {
//Error while creating directory
console.log("onDirectoryFail", error);
defer.reject(error);
}
function filetransfer(downloadLink, fp) {
console.log("starting file transfer", downloadLink, "-->", fp);
var fileTransfer = new FileTransfer();
// File download function with URL and local path
fileTransfer.download(downloadLink, fp,
function (entry) {
console.log("Download completed", entry);
defer.resolve(entry);
},
function (error) {
//Download abort errors or download failed errors
console.log("download error source ", error);
defer.reject(error);
}
);
}
return defer.promise;
}
}
})();
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.