Saturday, 7 September 2013

Update files in Phonegap file api and remove unused files

Update files in Phonegap file api and remove unused files

I'm using Phonegap's file api for the data of my app. Downloading files is
no problem, however I don't know how to update the files or remove unused
ones. Here's my code:
var DATADIR;
var knownfiles = [];
function onFSSuccess(fileSystem) {
fileSystem.root.getDirectory("Android/data/be.emm.mobile",
{create:true},gotDir,onError);
}
function gotDir(d){
console.log("got dir");
DATADIR = d;
var reader = DATADIR.createReader();
reader.readEntries(function(d){
gotFiles(d);
appReady();
},onError);
}
function gotFiles(entries) {
console.log("The dir has "+entries.length+" entries.");
for (var i=0; i<entries.length; i++) {
console.log(entries[i].name+' dir? '+entries[i].isDirectory);
knownfiles.push(entries[i].name);
}
}
function onError(e){
console.log("ERROR");
console.log(JSON.stringify(e));
}
function onDeviceReady() {
$("#status").html("Checking your local cache....");
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSSuccess, null);
}
function appReady(){
$("#status").html("Ready to check remote files...");
$.get("http://mllsdemode.be/EMM-test/download.json", {}, function(res) {
if (res.length > 0) {
$("#status").html("Going to sync some images...");
for (var i = 0; i < res.length; i++) {
if (knownfiles.indexOf(res[i]) == -1) {
console.log("need to download " + res[i]);
var ft = new FileTransfer();
var dlPath = DATADIR.fullPath + "/" + res[i];
console.log("downloading crap to " + dlPath);
ft.download("http://mllsdemode.be/EMM-test/" + escape(res[i]), dlPath,
function(e){
console.log("Successful download of "+e.fullPath);
}, onError);
}
}
}
$("#status").html("");
}, "json");
}
function init() {
document.addEventListener("deviceready", onDeviceReady, true);
}
At this moment the files only get downloaded when there's nothing in the
map be.emm.mobile. I want to download files everytime the app is active
and there's internet connection. I also want to delete the files that
aren't being used. In the Phonegap documentation they give this example,
but I don't know how to implement it.
function success(entry) {
console.log("Removal succeeded");
}
function fail(error) {
alert('Error removing file: ' + error.code);
}
// remove the file
entry.remove(success, fail);
Anyone who has any tips or advice for me how to handle this?

No comments:

Post a Comment