Skip to content

Commit

Permalink
Merge remote-tracking branch 'Parent/v2' into Current
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaRHristov committed Sep 18, 2024
2 parents 5ca4743 + 7efa980 commit 779ee5a
Show file tree
Hide file tree
Showing 16 changed files with 2,604 additions and 25 deletions.
28 changes: 28 additions & 0 deletions android/src/androidTest/java/ExampleInstrumentedTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

package com.plugin.fs

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.plugin.fs", appContext.packageName)
}
}
3 changes: 3 additions & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
</manifest>
93 changes: 93 additions & 0 deletions android/src/main/java/FsPlugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

package com.plugin.fs

import android.annotation.SuppressLint
import android.app.Activity
import android.content.res.AssetManager.ACCESS_BUFFER
import android.net.Uri
import android.os.ParcelFileDescriptor
import app.tauri.annotation.Command
import app.tauri.annotation.InvokeArg
import app.tauri.annotation.TauriPlugin
import app.tauri.plugin.Invoke
import app.tauri.plugin.JSObject
import app.tauri.plugin.Plugin
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream

@InvokeArg
class WriteTextFileArgs {
val uri: String = ""
val content: String = ""
}

@InvokeArg
class GetFileDescriptorArgs {
lateinit var uri: String
lateinit var mode: String
}

@TauriPlugin
class FsPlugin(private val activity: Activity): Plugin(activity) {
@SuppressLint("Recycle")
@Command
fun getFileDescriptor(invoke: Invoke) {
val args = invoke.parseArgs(GetFileDescriptorArgs::class.java)

val res = JSObject()

if (args.uri.startsWith(app.tauri.TAURI_ASSETS_DIRECTORY_URI)) {
val path = args.uri.substring(app.tauri.TAURI_ASSETS_DIRECTORY_URI.length)
try {
val fd = activity.assets.openFd(path).parcelFileDescriptor?.detachFd()
res.put("fd", fd)
} catch (e: IOException) {
// if the asset is compressed, we cannot open a file descriptor directly
// so we copy it to the cache and get a fd from there
// this is a lot faster than serializing the file and sending it as invoke response
// because on the Rust side we can leverage the custom protocol IPC and read the file directly
val cacheFile = File(activity.cacheDir, "_assets/$path")
cacheFile.parentFile?.mkdirs()
copyAsset(path, cacheFile)

val fd = ParcelFileDescriptor.open(cacheFile, ParcelFileDescriptor.parseMode(args.mode)).detachFd()
res.put("fd", fd)
}
} else {
val fd = activity.contentResolver.openAssetFileDescriptor(
Uri.parse(args.uri),
args.mode
)?.parcelFileDescriptor?.detachFd()
res.put("fd", fd)
}

invoke.resolve(res)
}

@Throws(IOException::class)
private fun copy(input: InputStream, output: OutputStream) {
val buf = ByteArray(1024)
var len: Int
while ((input.read(buf).also { len = it }) > 0) {
output.write(buf, 0, len)
}
}

@Throws(IOException::class)
private fun copyAsset(assetPath: String, cacheFile: File) {
val input = activity.assets.open(assetPath, ACCESS_BUFFER)
input.use { i ->
val output = FileOutputStream(cacheFile, false)
output.use { o ->
copy(i, o)
}
}
}
}

21 changes: 21 additions & 0 deletions android/src/test/java/ExampleUnitTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

package com.plugin.fs

import org.junit.Test

import org.junit.Assert.*

/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
2 changes: 1 addition & 1 deletion node_modules/@tauri-apps/api

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 28 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
{
"authors": [
"Tauri Programme within The Commons Conservancy"
],
"dependencies": {
"@tauri-apps/api": "^2.0.0-rc.4"
},
"description": "Access the file system.",
"exports": {
"import": "./dist-js/index.js",
"require": "./dist-js/index.cjs",
"types": "./dist-js/index.d.ts"
},
"files": [
"dist-js",
"README.md",
"LICENSE"
],
"main": "./dist-js/index.cjs",
"module": "./dist-js/index.js",
"name": "@tauri-apps/plugin-fs",
"scripts": {
"build": "rollup -c"
},
"types": "./dist-js/index.d.ts"
"name": "@tauri-apps/plugin-fs",
"version": "2.0.0-rc.2",
"description": "Access the file system.",
"license": "MIT or APACHE-2.0",
"authors": [
"Tauri Programme within The Commons Conservancy"
],
"repository": "https://github.com/tauri-apps/plugins-workspace",
"type": "module",
"types": "./dist-js/index.d.ts",
"main": "./dist-js/index.cjs",
"module": "./dist-js/index.js",
"exports": {
"types": "./dist-js/index.d.ts",
"import": "./dist-js/index.js",
"require": "./dist-js/index.cjs"
},
"scripts": {
"build": "rollup -c"
},
"files": [
"dist-js",
"README.md",
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "^2.0.0-rc.4"
}
}
Loading

0 comments on commit 779ee5a

Please sign in to comment.