Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capacitor #83

Open
wants to merge 5 commits into
base: latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 49 additions & 35 deletions src/android/io/sqlc/SQLitePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,46 +241,60 @@ private void createFromResource(String myDBName, File dbfile)
// IMPLEMENTATION based on various sources:
InputStream in = null;
OutputStream out = null;

/*
** Different path variations for Cordova/Ionic, Capacitor
** Capacitor writes the source to the public/assets path, but
** some have an old code which copies the dataabse to the root: 'public'.
** So we check for all different versions
*/

String[] pathArray = {"www/", "www/assets/", "public/", "public/assets/"};

try {
in = this.cordova.getActivity().getAssets().open("www/" + myDBName);
String dbPath = dbfile.getAbsolutePath();
dbPath = dbPath.substring(0, dbPath.lastIndexOf("/") + 1);

File dbPathFile = new File(dbPath);
if (!dbPathFile.exists())
dbPathFile.mkdirs();

File newDbFile = new File(dbPath + myDBName);
out = new FileOutputStream(newDbFile);

// XXX TODO: this is very primitive, other alternatives at:
// http://www.journaldev.com/861/4-ways-to-copy-file-in-java
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
out.write(buf, 0, len);

Log.v("info", "Copied prepopulated DB content to: " + newDbFile.getAbsolutePath());
} catch (IOException e) {
Log.v("createFromResource", "No prepopulated DB found, Error=" + e.getMessage());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {
Log.v("info", "Error closing input DB file, ignored");
for (String resourcePath : pathArray) {

try {
in = this.cordova.getActivity().getAssets().open(resourcePath + myDBName);
String dbPath = dbfile.getAbsolutePath();
dbPath = dbPath.substring(0, dbPath.lastIndexOf("/") + 1);

File dbPathFile = new File(dbPath);
if (!dbPathFile.exists())
dbPathFile.mkdirs();

File newDbFile = new File(dbPath + myDBName);
out = new FileOutputStream(newDbFile);

// XXX TODO: this is very primitive, other alternatives at:
// http://www.journaldev.com/861/4-ways-to-copy-file-in-java
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
out.write(buf, 0, len);

Log.v("info", "Copied prepopulated DB content from " + resourcePath + " to: " + newDbFile.getAbsolutePath());
} catch (IOException e) {
Log.v("createFromResource", "No prepopulated DB found in path " + resourcePath + ", Error=" + e.getMessage());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {
Log.v("info", "Error closing input DB file, ignored");
}
}
}

if (out != null) {
try {
out.close();
} catch (IOException ignored) {
Log.v("info", "Error closing output DB file, ignored");

if (out != null) {
try {
out.close();
} catch (IOException ignored) {
Log.v("info", "Error closing output DB file, ignored");
}
}
}

}

}

/**
Expand Down
48 changes: 34 additions & 14 deletions src/ios/SQLitePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,40 @@ -(void)openNow: (CDVInvokedUrlCommand*)command
-(void) createFromResource: (NSString *)dbfile withDbname:(NSString *)dbname {
// IMPLEMENTATION based on various sources:
NSString * bundleRoot = [[NSBundle mainBundle] resourcePath];

NSString * www = [bundleRoot stringByAppendingPathComponent:@"www"];
NSString * prepopulatedDb = [www stringByAppendingPathComponent: dbfile];
// NSLog(@"Look for pre-populated DB at: %@", prepopulatedDb);

if ([[NSFileManager defaultManager] fileExistsAtPath:prepopulatedDb]) {
NSLog(@"Found prepopulated DB: %@", prepopulatedDb);
NSError * error;
BOOL success = [[NSFileManager defaultManager] copyItemAtPath:prepopulatedDb toPath:dbname error:&error];

if(success)
NSLog(@"Copied pre-populated DB content to: %@", dbname);
else
NSLog(@"Unable to copy pre-populated DB file: %@", [error localizedDescription]);

/*
** Different path variations for Cordova/Ionic, Capacitor
** Capacitor writes the source to the public/assets path, but
** some have an old code which copies the dataabse to the root: 'public'.
** So we check for all different versions
*/

NSArray * pathArray = @[
[bundleRoot stringByAppendingPathComponent:@"www"],
[bundleRoot stringByAppendingPathComponent:@"www/assets"],
[bundleRoot stringByAppendingPathComponent:@"public"],
[bundleRoot stringByAppendingPathComponent:@"public/assets"]
];

/*
** Loop the paths from the pathArray for all possible variations in db locations
*/
for (NSString *copyPath in pathArray) {


NSString *prepopulatedDb = [copyPath stringByAppendingPathComponent:dbfile];


if ([[NSFileManager defaultManager] fileExistsAtPath:prepopulatedDb]) {
NSLog(@"Found prepopulated DB: %@", prepopulatedDb);
NSError * error;
BOOL success = [[NSFileManager defaultManager] copyItemAtPath:prepopulatedDb toPath:dbname error:&error];

if(success)
NSLog(@"Copied pre-populated DB content to: %@", dbname);
else
NSLog(@"Unable to copy pre-populated DB file: %@", [error localizedDescription]);
}
}
}

Expand Down