Skip to content

Commit

Permalink
v.0.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
helmac committed Jul 3, 2019
1 parent 44ca051 commit 470217e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-ionic-migrate-storage",
"version": "0.1.1",
"version": "0.1.3",
"cordova": {
"id": "cordova-plugin-ionic-migrate-storage",
"platforms": [
Expand Down
55 changes: 55 additions & 0 deletions src/android/MigrateStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,61 @@ private void migrateLocalStorage() throws Exception {
if (fileLocalStorage.exists()) {
fileLocalStorage.renameTo(ionicLocalStorage);
hasMigratedData = true;
} else {
this.logDebug("migrateLocalStorage: Migrating localStorage from leveldb..");

String levelDbPath = this.getLocalStorageRootPath() + "/leveldb";
this.logDebug("migrateLocalStorage: levelDbPath: " + levelDbPath);

File levelDbDir = new File(levelDbPath);
if(!levelDbDir.isDirectory() || !levelDbDir.exists()) {
this.logDebug("migrateLocalStorage: '" + levelDbPath + "' is not a directory or was not found; Exiting");
return;
}

LevelDB db = new LevelDB(levelDbPath);

String localHostProtocol = this.getLocalHostProtocol();

if(db.exists(Utils.stringToBytes("META:" + localHostProtocol))) {
this.logDebug("migrateLocalStorage: Found 'META:" + localHostProtocol + "' key; Skipping migration");
db.close();
return;
}

// Yes, there is a typo here; `newInterator` 😔
LevelIterator iterator = db.newInterator();

// To update in bulk!
WriteBatch batch = new WriteBatch();


// 🔃 Loop through the keys and replace `file://` with `http://localhost:{portNumber}`
logDebug("migrateLocalStorage: Starting replacements;");
for(iterator.seekToFirst(); iterator.isValid(); iterator.next()) {
String key = Utils.bytesToString(iterator.key());
byte[] value = iterator.value();

if (key.contains(FILE_PROTOCOL)) {
String newKey = key.replace(FILE_PROTOCOL, localHostProtocol);

logDebug("migrateLocalStorage: Changing key:" + key + " to '" + newKey + "'");

// Add new key to db
batch.putBytes(Utils.stringToBytes(newKey), value);
hasMigratedData = true;
} else {
logDebug("migrateLocalStorage: Skipping key:" + key);
}
}

// Commit batch to DB
db.write(batch);

iterator.close();
db.close();

this.logDebug("migrateLocalStorage: Successfully migrated localStorage..");
}

if (fileLocalStorageJournal.exists()) {
Expand Down

0 comments on commit 470217e

Please sign in to comment.