Skip to content

Commit 51a5cfe

Browse files
authored
feat(api): add convertFileSrc helper (#2138)
1 parent 06abe65 commit 51a5cfe

7 files changed

Lines changed: 35 additions & 7 deletions

File tree

.changes/api-convert-file-url.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"api": patch
3+
---
4+
5+
Adds `convertFileSrc` helper to the `tauri` module, simplifying the process of using file paths as webview source (`img`, `video`, etc).

core/tauri/scripts/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/api/public/build/bundle.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/api/public/build/bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/api/src-tauri/tauri.conf.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@
7979
}
8080
],
8181
"security": {
82-
"csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'"
82+
"csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: asset: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'"
8383
},
8484
"systemTray": {
8585
"iconPath": "../../.icons/icon.png"
8686
}
8787
}
88-
}
88+
}

examples/api/src/components/FileSystem.svelte

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<script>
22
import { readBinaryFile, readDir, Dir } from "@tauri-apps/api/fs";
3+
import { convertFileSrc } from "@tauri-apps/api/tauri";
34
45
export let onMessage;
56
67
let pathToRead = "";
8+
let img;
79
810
function getDir() {
911
const dirSelect = document.getElementById("dir");
@@ -71,6 +73,10 @@
7173
})
7274
.catch(onMessage);
7375
}
76+
77+
function setSrc() {
78+
img.src = convertFileSrc(pathToRead)
79+
}
7480
</script>
7581

7682
<form on:submit|preventDefault={read}>
@@ -86,4 +92,7 @@
8692
bind:value={pathToRead}
8793
/>
8894
<button class="button" id="read">Read</button>
95+
<button class="button" type="button" on:click={setSrc}>Use as img src</button>
96+
97+
<img alt="file" bind:this={img}>
8998
</form>

tooling/api/src/tauri.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ async function invoke<T>(cmd: string, args: InvokeArgs = {}): Promise<T> {
8888
})
8989
}
9090

91+
/**
92+
* Convert a device file path to an URL that can be loaded by the webview.
93+
* Note that `asset:` must be allowed on the `csp` value configured on `tauri.conf.json`.
94+
*
95+
* @param filePath the file path. On Windows, the drive name must be omitted, i.e. using `/Users/user/file.png` instead of `C:/Users/user/file.png`.
96+
*
97+
* @return the URL that can be used as source on the webview
98+
*/
99+
function convertFileSrc(filePath: string): string {
100+
return navigator.userAgent.includes('Windows')
101+
? `https://custom.protocol.asset_${filePath}`
102+
: `asset://${filePath}`
103+
}
104+
91105
export type { InvokeArgs }
92106

93-
export { transformCallback, invoke }
107+
export { transformCallback, invoke, convertFileSrc }

0 commit comments

Comments
 (0)