Skip to content

Commit

Permalink
feat(fs): resolve content URIs on Android (#1658)
Browse files Browse the repository at this point in the history
* Implemented writeTextFile on Android.

* Added license headers.

* fix fmt checks.

* implement more file APIs

* change file

* cleanup

* refactor dialog plugin to leverage new FS APIs

* implement metadata functions

* fix build

* expose FS rust API

* resolve resources on android

* update pnpm

* update docs

---------

Co-authored-by: Lucas Nogueira <[email protected]>
  • Loading branch information
mikoto2000 and lucasfernog authored Aug 23, 2024
1 parent 3c52f30 commit 448846b
Show file tree
Hide file tree
Showing 38 changed files with 1,950 additions and 819 deletions.
7 changes: 7 additions & 0 deletions .changes/dialog-return-path.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"dialog": patch
"dialog-js": patch
---

The `open` function now returns a string representing either the file path or URI instead of an object.
To read the file data, use the `fs` APIs.
5 changes: 5 additions & 0 deletions .changes/resolve-content-uris.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fs": patch:feat
---

Resolve `content://` path URIs on Android.
33 changes: 17 additions & 16 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ resolver = "2"
[workspace.dependencies]
serde = { version = "1", features = ["derive"] }
log = "0.4"
tauri = { version = "2.0.0-rc.3", default-features = false }
tauri-build = "2.0.0-rc.3"
tauri-plugin = "2.0.0-rc.3"
tauri-utils = "2.0.0-rc.3"
tauri = { version = "2.0.0-rc.5", default-features = false }
tauri-build = "2.0.0-rc.5"
tauri-plugin = "2.0.0-rc.5"
tauri-utils = "2.0.0-rc.5"
serde_json = "1"
thiserror = "1"
url = "2"
Expand Down
2 changes: 1 addition & 1 deletion examples/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@iconify-json/codicon": "^1.1.37",
"@iconify-json/ph": "^1.1.8",
"@sveltejs/vite-plugin-svelte": "^3.0.1",
"@tauri-apps/cli": "2.0.0-rc.4",
"@tauri-apps/cli": "2.0.0-rc.6",
"@unocss/extractor-svelte": "^0.62.0",
"svelte": "^4.2.8",
"unocss": "^0.62.0",
Expand Down
3 changes: 3 additions & 0 deletions examples/api/src-tauri/capabilities/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
"clipboard-manager:allow-write-text",
"clipboard-manager:allow-read-image",
"clipboard-manager:allow-write-image",
"fs:allow-open",
"fs:allow-write",
"fs:allow-read",
"fs:allow-rename",
"fs:allow-mkdir",
"fs:allow-remove",
Expand Down
1 change: 1 addition & 0 deletions examples/api/src-tauri/gen/android/.idea/gradle.xml

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

15 changes: 7 additions & 8 deletions examples/api/src/views/Dialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@
async function prompt() {
confirm("Do you want to do something?")
.then((res) =>
onMessage(
res ? "Yes" : "No"
)
)
.then((res) => onMessage(res ? "Yes" : "No"))
.catch(onMessage);
}
Expand Down Expand Up @@ -67,14 +63,15 @@
if (Array.isArray(res)) {
onMessage(res);
} else {
var pathToRead = typeof res === "string" ? res : res.path;
var pathToRead = res;
var isFile = pathToRead.match(/\S+\.\S+$/g);
readFile(pathToRead)
.then(function (response) {
if (isFile) {
if (
pathToRead.includes(".png") ||
pathToRead.includes(".jpg")
pathToRead.includes(".jpg") ||
pathToRead.includes(".jpeg")
) {
arrayBufferToBase64(
new Uint8Array(response),
Expand Down Expand Up @@ -144,5 +141,7 @@
>Open save dialog</button
>
<button class="btn" id="prompt-dialog" on:click={prompt}>Prompt</button>
<button class="btn" id="custom-prompt-dialog" on:click={promptCustom}>Prompt (custom)</button>
<button class="btn" id="custom-prompt-dialog" on:click={promptCustom}
>Prompt (custom)</button
>
<button class="btn" id="message-dialog" on:click={msg}>Message</button>
2 changes: 1 addition & 1 deletion plugins/deep-link/examples/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@tauri-apps/plugin-deep-link": "2.0.0-rc.0"
},
"devDependencies": {
"@tauri-apps/cli": "2.0.0-rc.4",
"@tauri-apps/cli": "2.0.0-rc.6",
"typescript": "^5.2.2",
"vite": "^5.0.13"
}
Expand Down
4 changes: 4 additions & 0 deletions plugins/dialog/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ targets = ["x86_64-unknown-linux-gnu", "x86_64-linux-android"]
[build-dependencies]
tauri-plugin = { workspace = true, features = [ "build" ] }

[dev-dependencies]
tauri = { workspace = true, features = [ "wry" ] }

[dependencies]
serde = { workspace = true }
serde_json = { workspace = true }
tauri = { workspace = true }
log = { workspace = true }
thiserror = { workspace = true }
dunce = { workspace = true }
url = { workspace = true }
tauri-plugin-fs = { path = "../fs", version = "2.0.0-rc.0" }

[target.'cfg(target_os = "ios")'.dependencies]
Expand Down
41 changes: 7 additions & 34 deletions plugins/dialog/android/src/main/java/DialogPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class Filter {
class FilePickerOptions {
lateinit var filters: Array<Filter>
var multiple: Boolean? = null
var readData: Boolean? = null
}

@InvokeArg
Expand Down Expand Up @@ -95,7 +94,7 @@ class DialogPlugin(private val activity: Activity): Plugin(activity) {
try {
when (result.resultCode) {
Activity.RESULT_OK -> {
val callResult = createPickFilesResult(result.data, filePickerOptions?.readData ?: false)
val callResult = createPickFilesResult(result.data)
invoke.resolve(callResult)
}
Activity.RESULT_CANCELED -> invoke.reject("File picker cancelled")
Expand All @@ -108,49 +107,23 @@ class DialogPlugin(private val activity: Activity): Plugin(activity) {
}
}

private fun createPickFilesResult(data: Intent?, readData: Boolean): JSObject {
private fun createPickFilesResult(data: Intent?): JSObject {
val callResult = JSObject()
val filesResultList: MutableList<JSObject> = ArrayList()
if (data == null) {
callResult.put("files", JSArray.from(filesResultList))
callResult.put("files", null)
return callResult
}
val uris: MutableList<Uri?> = ArrayList()
val uris: MutableList<String?> = ArrayList()
if (data.clipData == null) {
val uri: Uri? = data.data
uris.add(uri)
uris.add(uri?.toString())
} else {
for (i in 0 until data.clipData!!.itemCount) {
val uri: Uri = data.clipData!!.getItemAt(i).uri
uris.add(uri)
uris.add(uri.toString())
}
}
for (i in uris.indices) {
val uri = uris[i] ?: continue
val fileResult = JSObject()
if (readData) {
fileResult.put("base64Data", FilePickerUtils.getDataFromUri(activity, uri))
}
val duration = FilePickerUtils.getDurationFromUri(activity, uri)
if (duration != null) {
fileResult.put("duration", duration)
}
val resolution = FilePickerUtils.getHeightAndWidthFromUri(activity, uri)
if (resolution != null) {
fileResult.put("height", resolution.height)
fileResult.put("width", resolution.width)
}
fileResult.put("mimeType", FilePickerUtils.getMimeTypeFromUri(activity, uri))
val modifiedAt = FilePickerUtils.getModifiedAtFromUri(activity, uri)
if (modifiedAt != null) {
fileResult.put("modifiedAt", modifiedAt)
}
fileResult.put("name", FilePickerUtils.getNameFromUri(activity, uri))
fileResult.put("path", FilePickerUtils.getPathFromUri(activity, uri))
fileResult.put("size", FilePickerUtils.getSizeFromUri(activity, uri))
filesResultList.add(fileResult)
}
callResult.put("files", JSArray.from(filesResultList.toTypedArray()))
callResult.put("files", JSArray.from(uris.toTypedArray()))
return callResult
}

Expand Down
17 changes: 2 additions & 15 deletions plugins/dialog/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@

import { invoke } from "@tauri-apps/api/core";

interface FileResponse {
base64Data?: string;
duration?: number;
height?: number;
width?: number;
mimeType?: string;
modifiedAt?: number;
name?: string;
path: string;
size: number;
}

/**
* Extension filters for the file dialog.
*
Expand Down Expand Up @@ -117,8 +105,8 @@ type OpenDialogReturn<T extends OpenDialogOptions> = T["directory"] extends true
? string[] | null
: string | null
: T["multiple"] extends true
? FileResponse[] | null
: FileResponse | null;
? string[] | null
: string | null;

/**
* Open a file/directory selection dialog.
Expand Down Expand Up @@ -306,7 +294,6 @@ async function confirm(

export type {
DialogFilter,
FileResponse,
OpenDialogOptions,
OpenDialogReturn,
SaveDialogOptions,
Expand Down
Loading

0 comments on commit 448846b

Please sign in to comment.