-
-
Notifications
You must be signed in to change notification settings - Fork 45
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
Allow copying databases from custom assets path #94
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
package com.op.sqlite | ||
|
||
import android.util.Log | ||
import com.facebook.react.bridge.Promise | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.bridge.ReactMethod | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule | ||
import com.facebook.react.bridge.ReadableMap | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
import java.io.InputStream | ||
import java.io.OutputStream | ||
import com.facebook.react.util.RNLog; | ||
|
||
//@ReactModule(name = OPSQLiteModule.NAME) | ||
internal class OPSQLiteModule(context: ReactApplicationContext?) : ReactContextBaseJavaModule(context) { | ||
|
@@ -47,25 +50,33 @@ internal class OPSQLiteModule(context: ReactApplicationContext?) : ReactContextB | |
} | ||
} | ||
|
||
@ReactMethod(isBlockingSynchronousMethod = true) | ||
fun moveAssetsDatabase(name: String, extension: String): Boolean { | ||
@ReactMethod | ||
fun moveAssetsDatabase(args: ReadableMap, promise: Promise) { | ||
val filename = args.getString("filename")!! | ||
val path = args.getString("path") ?: "custom" | ||
val overwrite = if(args.hasKey("overwrite")) { args.getBoolean("overwrite") } else false | ||
val context = reactApplicationContext | ||
val assetsManager = context.assets | ||
|
||
try { | ||
// Open the input stream for the asset file | ||
val inputStream: InputStream = assetsManager.open("custom/$name.$extension") | ||
val inputStream: InputStream = assetsManager.open("$path/$filename") | ||
|
||
// Create the output file in the documents directory | ||
val databasesFolder = | ||
context.getDatabasePath("defaultDatabase") | ||
.absolutePath | ||
.replace("defaultDatabase", "") | ||
|
||
val outputFile = File(databasesFolder, "$name.$extension") | ||
val outputFile = File(databasesFolder, filename) | ||
|
||
if (outputFile.exists()) { | ||
return true | ||
if(overwrite) { | ||
outputFile.delete() | ||
} else { | ||
promise.resolve(true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't the input stream be closed here before resolving the promise and returning? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Solved by moving the instantiation down |
||
return | ||
} | ||
} | ||
|
||
// Open the output stream for the output file | ||
|
@@ -82,9 +93,10 @@ internal class OPSQLiteModule(context: ReactApplicationContext?) : ReactContextB | |
inputStream.close() | ||
outputStream.close() | ||
|
||
return true | ||
promise.resolve(true) | ||
} catch (exception: Exception) { | ||
return false | ||
RNLog.e(this.reactApplicationContext, "Exception: $exception") | ||
promise.resolve(false) | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -489,11 +489,12 @@ export const open = (options: { | |
}; | ||
}; | ||
|
||
export const moveAssetsDatabase = ( | ||
dbName: string, | ||
extension: string | ||
): boolean => { | ||
return NativeModules.OPSQLite.moveAssetsDatabase(dbName, extension); | ||
export const moveAssetsDatabase = (args: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method's native implementation is now async so the definition here needs to be updated to reflect that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah yeah, missed that. Thanks! |
||
filename: string; | ||
path?: string; | ||
overwrite?: boolean; | ||
}): boolean => { | ||
return NativeModules.OPSQLite.moveAssetsDatabase(args); | ||
}; | ||
|
||
export const isSQLCipher = (): boolean => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it not be more efficient to check if the output file exists before even opening the input stream, so that it is only opened if we will actually use it? This doesn't matter if we are overwriting the file, but in the case where we're not and the file already exists, it would make a difference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think opening a stream makes such a difference, but I will move it.