Update sqlDB.java

Added `checkDbOnStorage()` to check if a file exists
This commit is contained in:
rafaellop 2017-03-08 14:06:23 +01:00 committed by GitHub
parent 7a27d16b3e
commit e268fb79c6

View File

@ -14,6 +14,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.util.Log;
/**
* This class echoes a string called from JavaScript.
*/
@ -65,6 +67,7 @@ public class sqlDB extends CordovaPlugin {
}
return true;
} else if (action.equalsIgnoreCase("copyDbToStorage")) {
Log.d("DBSavePlugin", "copyDbToStorage");
String db = args.getString(0);
String dest = args.getString(2);
this.copyDbToStorage(db, dest, callbackContext);
@ -75,6 +78,12 @@ public class sqlDB extends CordovaPlugin {
boolean deletedb = args.getBoolean(3);
this.copyDbFromStorage(db, src, deletedb, callbackContext);
return true;
} else if (action.equalsIgnoreCase("checkDbOnStorage")) {
Log.d("DBSavePlugin", "checkDbOnStorage1");
String db = args.getString(0);
String src = args.getString(2);
this.checkDbOnStorage(db, src, callbackContext);
return true;
} else {
plresult = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(plresult);
@ -168,8 +177,27 @@ public class sqlDB extends CordovaPlugin {
sendPluginResponse(404, "Invalid DB Source Location", true, callbackContext);
}
}
private void checkDbOnStorage(String db, String src, final CallbackContext callbackContext) {
Log.d("DBSavePlugin", "checkDbOnStorage");
File source;
if (src.indexOf("file://") != -1) {
source = new File(src.replace("file://", ""));
} else {
source = new File(src);
}
if (source.exists()) {
Log.d("DBSavePlugin", "exists");
} else {
Log.d("DBSavePlugin", "doesnt exists");
sendPluginResponse(404, "Invalid DB Source Location", true, callbackContext);
}
}
private void copyDbToStorage(String dbname, String dest, final CallbackContext callbackContext) {
Log.d("DBSavePlugin", "copyDbToStorage");
File source = cordova.getActivity().getDatabasePath(dbname);
File destFolder;
File destination;
@ -198,4 +226,6 @@ public class sqlDB extends CordovaPlugin {
sendPluginResponse(404, "Invalid DB Location or DB Doesn't Exists", true, callbackContext);
}
}
}