Skip to content

Commit

Permalink
Merge branch 'dev-2.24'
Browse files Browse the repository at this point in the history
  • Loading branch information
birdofpreyru committed Jan 31, 2024
2 parents ef32521 + c893b7b commit d3086d6
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 20 deletions.
28 changes: 20 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ and [old][Old Architecture] [RN][React Native] architectures.
- [copyAssetsVideoIOS()] — Copies a video from the assets-library
to the specified destination.
- [copyFile()] — Copies a file to a new destination.
- [copyFileAssets()] — (Android only) Copies an asset file to
the given destination.
- [copyFileAssets()] — (Android only) Copies Android app's asset(s)
to the specified destination.
- [copyFileRes()] — (Android only) Copies specified resource to
the given destination.
- [copyFolder()] — (Windows only) Copies a folder to a new location
Expand Down Expand Up @@ -625,13 +625,25 @@ function copyFileAssets(from: string, into: string): Promise<void>
```
**VERIFIED:** Android. **NOT SUPPORTED:** iOS, macOS, Windows.

Copies a file from the given path in the Android app's assets folder to
the specified destination path, overwriting the file at destination, if
it exists.
Copies Android app's asset(s) to the specified destination.

If `from` points to a file, this function assumes `into` is a file path as well,
and it copies the asset to that destination, overwriting the existing file at
that destination, if any.

If `from` points to a folder, this function assumes `into` is a folder path
as well, and it recursively copies the content of `from` into that destination,
preserving the folder structure of copied assets, and overwriting existing files
in the destination in case of conflicts. It does not clean the destination prior
to copying into it, and it cannot overwrite files by folders and vice-versa.
If `into` does not exist, it will be created, assuming its parent folder
does exist (_i.e._ it does not attempt to create entire path as [mkdir()] does).

- `from` &mdash; **string** &mdash; Source path, relative to the root assets
folder. Can be empty to refer the root assets folder itself.

- `into` &mdash; **string** &mdash; Destination path.

- `from` &mdash; **string** &mdash; Source asset path (relative to the asset
folder's root).
- `to` &mdash; **string** &mdash; Destination path.
- Resolves once completed.

### copyFileRes()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,39 @@ class ReactNativeFsModule internal constructor(context: ReactApplicationContext)
}

@ReactMethod
override fun copyFileAssets(assetPath: String, destination: String, promise: Promise) {
override fun copyFileAssets(from: String, into: String, promise: Promise) {
try {
val assetManager: AssetManager = getReactApplicationContext().getAssets()
try {
val `in` = assetManager.open(assetPath)
copyInputStream(`in`, assetPath, destination, promise)
} catch (e: IOException) {
// Default error message is just asset name, so make a more helpful error here.
reject(promise, assetPath, Exception(String.format("Asset '%s' could not be opened", assetPath)))
val queue = ArrayList<Pair<String,String>>()
queue.add(Pair(from, into))

while (!queue.isEmpty()) {
val next = queue.removeLast()
val list = assetManager.list(next.first)

// Next asset to copy is a folder.
if (list?.isEmpty() == false) {
File(next.second).mkdir()
for (i in 0.. list.size - 1) {
val name = list[i]
queue.add(Pair(
if (next.first.isEmpty()) name else next.first + "/" + name,
next.second + "/" + name
))
}
}

// Next asset to copy is a file.
else {
val stream = assetManager.open(next.first)
copyInputStream(stream, next.second)
}
}

promise.resolve(null)
} catch (e: Exception) {
Errors.OPERATION_FAILED.reject(promise, e.toString())
}
}

@ReactMethod
Expand Down Expand Up @@ -847,6 +871,30 @@ class ReactNativeFsModule internal constructor(context: ReactApplicationContext)
}
}

/**
* Copies given InputStream to the specified destination.
*
* It is a less opinionated version of the copyInputStream() method defined above.
*
* Note that since API 33 InputStream has .transferTo() method that can do the job,
* but for now we stick with this lower-level, but more universal implementation.
*/
private fun copyInputStream(stream: InputStream, destination: String) {
var out: OutputStream? = null
try {
// TODO: Since API 33 there is a handy method .transferTo() of InputStream to do this job.
out = getOutputStream(destination, false)
val buffer = ByteArray(1024 * 10) // 10k buffer
var read: Int
while (stream.read(buffer).also { read = it } != -1) {
out!!.write(buffer, 0, read)
}
} finally {
closeIgnoringException(stream)
closeIgnoringException(out)
}
}

private fun DeleteRecursive(fileOrDirectory: File) {
if (fileOrDirectory.isDirectory) {
for (child in fileOrDirectory.listFiles()) {
Expand Down
29 changes: 29 additions & 0 deletions example/src/TestBaseMethods.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,35 @@ const tests: { [name: string]: StatusOrEvaluator } = {
return 'fail';
}
},
'copyFileAssets() - invalid path': async () => {
const path = `${TemporaryDirectoryPath}/good-utf8.txt`;
try {
await unlink(path);
} catch {}
try {
if (await exists(path)) return 'fail';
await copyFileAssets('invalid-path', path);
return 'fail';
} catch {
return 'pass';
}
},
// NOTE: This is a new test, for the updated function behavior.
'copyFileAssets() - new': async () => {
const dest = `${TemporaryDirectoryPath}/copy-file-assets-2`;
try {
await unlink(dest);
} catch {}
// await mkdir(dest);
try {
await copyFileAssets('test', dest);
const res = await readFile(`${dest}/good-utf8.txt`);
if (res !== 'GÖÖÐ\n') return 'fail';
return 'pass';
} catch {
return 'fail';
}
},
'copyFileRes()': async () => {
const path = `${TemporaryDirectoryPath}/res_good_utf8.txt`;
try {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dr.pogodin/react-native-fs",
"version": "2.23.0",
"version": "2.24.0",
"description": "Native filesystem access for react-native",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down Expand Up @@ -71,8 +71,8 @@
},
"peerDependencies": {
"react": "*",
"react-native": "^0.73",
"react-native-windows": "^0.73"
"react-native": "^0.73.*",
"react-native-windows": "^0.73.*"
},
"workspaces": [
"example"
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1832,8 +1832,8 @@ __metadata:
typescript: ^5.3.3
peerDependencies:
react: "*"
react-native: ^0.73
react-native-windows: ^0.73
react-native: ^0.73.*
react-native-windows: ^0.73.*
languageName: unknown
linkType: soft

Expand Down

0 comments on commit d3086d6

Please sign in to comment.