Created
March 5, 2011 19:34
-
-
Save codeboxed/856649 to your computer and use it in GitHub Desktop.
Copy files from a source to a target using Titanium as a platform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os.path | |
def check(mypath): | |
return os.path.isfile(mypath) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Class name: FileUtils.js | |
* Author: Codeboxed | |
* URL: http://www.codeboxed.com | |
* Date: March 5, 2011 | |
* Platform: Titanium | |
*/ | |
var FileUtils = function (){ | |
var appDataDir = Titanium.Filesystem.getApplicationDataDirectory(); | |
var appResDir = Titanium.Filesystem.getResourcesDirectory(); | |
var separator = Titanium.Filesystem.getSeparator(); | |
/** | |
* Private method used to get the directory based on the path | |
* @param {string} pathName The static path names that can be found in this function | |
* @returns Titanium.Filesystem | |
*/ | |
var getLocationForPathName = function(pathName) { | |
var location = null; | |
switch(pathName) { | |
case 'APP_DATA_DIR': | |
location = appDataDir; | |
break; | |
case 'APP_RESOURCE_DIR': | |
location = appResDir; | |
break; | |
default: | |
location = pathName; | |
//console.warn('Path name '+pathName+' is not guaranteed to work!'); | |
} | |
return location; | |
}; | |
/** | |
* Check to see if a given filename exists | |
* @param {string} fileName The query string that needs to be executed on the table | |
* @param {string} where The loction where to serach for the file (e.g. 'APP_DATA_DIR' ) | |
* @returns {boolean} true or false | |
*/ | |
var fileExists = function (fileName, where){ | |
var location = getLocationForPathName(where)+separator+fileName; | |
return check(location); //the check function comes from the python file | |
}; | |
/** | |
* Method used to copy a file form a location to another location | |
* @param {string} fileName The name of the file to be copied | |
* @param {string} from The location of the file | |
* @param {string} to The target where the file will be copied | |
* @param {boolean} check Check to see if the file exists | |
* @param {boolean} overwrite If true, it overwrites the file | |
*/ | |
var cloneFile = function (fileName, from, to, check, overwrite){ | |
var parent = getLocationForPathName(from); | |
var target = getLocationForPathName(to); | |
var checkExistance = false; | |
if (check == true) { | |
checkExistance = fileExists(fileName, to); | |
} | |
if (checkExistance) { | |
if (overwrite == true) { | |
Titanium.Filesystem.asyncCopy( | |
[Titanium.Filesystem.getFile(parent, fileName)], | |
target, | |
function(index, total){ //CALLBACK FUNCTION | |
//alert("Copied " + index + "out of " + total + "files so far."); | |
}); | |
console.log('file existed - overwritten'); | |
} | |
console.log('file exists - NOT overwritten'); | |
} else { | |
Titanium.Filesystem.asyncCopy( | |
[Titanium.Filesystem.getFile(parent, fileName)], | |
target, | |
function(index, total){ //CALLBACK FUNCTION | |
//alert("Copied " + index + "out of " + total + "files so far."); | |
}); | |
console.log('file did not existed - copied'); | |
} | |
}; | |
return { | |
checkFile: function(fileName, target) { | |
return fileExists(fileName, target); | |
}, | |
copyFile: function(fileName, from, to, check, overwrite) { | |
return cloneFile(fileName, from, to, check, overwrite); | |
} | |
}; | |
}; | |
//HOW TO USE | |
var fileUtils = new FileUtils(); | |
var filePresence = fileUtils.checkFile('test.db', 'APP_DATA_DIR'); | |
fileUtils.copyFile('test.db', 'APP_RESOURCE_DIR', 'APP_DATA_DIR', true, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment