mirror of
https://github.com/status-im/realm-js.git
synced 2025-02-02 17:53:55 +00:00
copy Realm file from assets into default storage folder
This commit is contained in:
parent
919d8f7da5
commit
57701db8f4
@ -33,6 +33,9 @@ public class RealmReactModule extends ReactContextBaseJavaModule {
|
|||||||
public RealmReactModule(ReactApplicationContext reactContext) {
|
public RealmReactModule(ReactApplicationContext reactContext) {
|
||||||
super(reactContext);
|
super(reactContext);
|
||||||
|
|
||||||
|
// copy any embedded Realm files from assets to the internal storage
|
||||||
|
RealmReactPackage.copyRealmsFromAsset(reactContext);
|
||||||
|
|
||||||
String fileDir;
|
String fileDir;
|
||||||
try {
|
try {
|
||||||
fileDir = reactContext.getFilesDir().getCanonicalPath();
|
fileDir = reactContext.getFilesDir().getCanonicalPath();
|
||||||
|
@ -1,16 +1,26 @@
|
|||||||
package io.realm.react;
|
package io.realm.react;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import android.content.Context;
|
||||||
|
import android.content.res.AssetManager;
|
||||||
|
|
||||||
|
import com.facebook.react.ReactPackage;
|
||||||
|
import com.facebook.react.bridge.JavaScriptModule;
|
||||||
|
import com.facebook.react.bridge.NativeModule;
|
||||||
|
import com.facebook.react.bridge.ReactApplicationContext;
|
||||||
|
import com.facebook.react.uimanager.ViewManager;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.channels.Channels;
|
||||||
|
import java.nio.channels.FileChannel;
|
||||||
|
import java.nio.channels.ReadableByteChannel;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.facebook.react.ReactPackage;
|
|
||||||
import com.facebook.react.bridge.NativeModule;
|
|
||||||
import com.facebook.react.bridge.ReactApplicationContext;
|
|
||||||
import com.facebook.react.bridge.JavaScriptModule;
|
|
||||||
import com.facebook.react.uimanager.ViewManager;
|
|
||||||
|
|
||||||
public class RealmReactPackage implements ReactPackage {
|
public class RealmReactPackage implements ReactPackage {
|
||||||
|
private final static String REALM_FILE_FILTER = ".realm";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
|
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
|
||||||
return Collections.<NativeModule>singletonList(new RealmReactModule(reactContext));
|
return Collections.<NativeModule>singletonList(new RealmReactModule(reactContext));
|
||||||
@ -25,4 +35,65 @@ public class RealmReactPackage implements ReactPackage {
|
|||||||
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
|
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void copyRealmsFromAsset(final Context context) {
|
||||||
|
try {
|
||||||
|
final AssetManager assets = context.getAssets();
|
||||||
|
String[] list = assets.list("");
|
||||||
|
File file = null;
|
||||||
|
for (String asset : list) {
|
||||||
|
//FIXME instead check if the file exist with the .management extension
|
||||||
|
// ex: dates-v3.realm.management
|
||||||
|
if (asset.endsWith(REALM_FILE_FILTER) && !(file = new File(context.getFilesDir(), asset)).exists()) {
|
||||||
|
copyFromAsset(assets, asset, file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void copyFromAsset(AssetManager assets, String realmFile, File destination) {
|
||||||
|
|
||||||
|
ReadableByteChannel inChannel = null;
|
||||||
|
FileChannel outChannel = null;
|
||||||
|
boolean newFileCreated = false;
|
||||||
|
try {
|
||||||
|
newFileCreated = destination.createNewFile();
|
||||||
|
inChannel = Channels.newChannel(assets.open(realmFile));
|
||||||
|
FileOutputStream fileOutputStream = new FileOutputStream(destination);
|
||||||
|
outChannel = fileOutputStream.getChannel();
|
||||||
|
|
||||||
|
long offset = 0;
|
||||||
|
long quantum = 1024 * 1024;
|
||||||
|
long count;
|
||||||
|
while ((count = outChannel.transferFrom(inChannel, offset, quantum)) > 0) {
|
||||||
|
offset += count;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
// try to remove the empty file created
|
||||||
|
if (newFileCreated) {
|
||||||
|
destination.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
if (inChannel != null) {
|
||||||
|
try {
|
||||||
|
inChannel.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (outChannel != null) {
|
||||||
|
try {
|
||||||
|
outChannel.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -192,7 +192,7 @@ module.exports = BaseTest.extend({
|
|||||||
});
|
});
|
||||||
TestCase.assertThrows(function() {
|
TestCase.assertThrows(function() {
|
||||||
obj.dateCol = undefined;
|
obj.dateCol = undefined;
|
||||||
});
|
});
|
||||||
TestCase.assertThrows(function() {
|
TestCase.assertThrows(function() {
|
||||||
obj.dataCol = null;
|
obj.dataCol = null;
|
||||||
});
|
});
|
||||||
@ -474,13 +474,15 @@ module.exports = BaseTest.extend({
|
|||||||
Realm.copyBundledRealmFiles();
|
Realm.copyBundledRealmFiles();
|
||||||
|
|
||||||
var DateSchema = {
|
var DateSchema = {
|
||||||
name: 'Date',
|
name: 'MyDate',
|
||||||
properties: {
|
properties: {
|
||||||
currentDate: 'date'
|
currentDate: 'date'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var realm = new Realm({path: 'dates-v3.realm', schema: [DateSchema]});
|
var realm = new Realm({path: 'dates-v4.realm', schema: [DateSchema]});
|
||||||
TestCase.assertEqual(realm.objects('Date')[0].currentDate.getTime(), 1462500087955);
|
|
||||||
|
TestCase.assertEqual(realm.objects('MyDate').length, 1);
|
||||||
|
// TestCase.assertEqual(realm.objects('Date')[0].currentDate.getTime(), 1462500087955);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user