アプリのデータに Phonegap のファイル API を使用しています。ファイルのダウンロードは問題ありませんが、ファイルを更新する方法や使用していないファイルを削除する方法がわかりません。これが私のコードです:
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);
}
現時点では、ファイルはマップ be.emm.mobile に何もない場合にのみダウンロードされます。アプリがアクティブでインターネット接続があるたびにファイルをダウンロードしたい。使用していないファイルも削除したい。Phonegap のドキュメントでは、この例を示していますが、実装方法がわかりません。
function success(entry) {
console.log("Removal succeeded");
}
function fail(error) {
alert('Error removing file: ' + error.code);
}
// remove the file
entry.remove(success, fail);
これを処理する方法についてのヒントやアドバイスがある人はいますか?