Browse Source

fix(core): streaming of small files using `asset://`, closes #2854 (#3039)

Lucas Fernandes Nogueira 3 years ago
parent
commit
151e629ebf

+ 5 - 0
.changes/streaming-small-file-fix.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+Fix streaming of small files using the `asset` protocol.

+ 1 - 1
core/tauri/src/manager.rs

@@ -350,7 +350,7 @@ impl<R: Runtime> WindowManager<R> {
               if range.length > file_size / 3 {
                 // max size sent (400ko / request)
                 // as it's local file system we can afford to read more often
-                real_length = 1024 * 400;
+                real_length = std::cmp::min(file_size - range.start, 1024 * 400);
               }
 
               // last byte we are reading, the length of the range include the last byte

+ 2 - 1
examples/streaming/src-tauri/src/main.rs

@@ -9,6 +9,7 @@
 
 fn main() {
   use std::{
+    cmp::min,
     io::{Read, Seek, SeekFrom},
     path::PathBuf,
     process::{Command, Stdio},
@@ -74,7 +75,7 @@ fn main() {
           if range.length > file_size / 3 {
             // max size sent (400ko / request)
             // as it's local file system we can afford to read more often
-            real_length = 1024 * 400;
+            real_length = min(file_size - range.start, 1024 * 400);
           }
 
           // last byte we are reading, the length of the range include the last byte

+ 1 - 1
tooling/api/src/tauri.ts

@@ -96,7 +96,7 @@ async function invoke<T>(cmd: string, args: InvokeArgs = {}): Promise<T> {
  * Convert a device file path to an URL that can be loaded by the webview.
  * Note that `asset:` must be allowed on the `csp` value configured on `tauri.conf.json`.
  *
- * @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`.
+ * @param  filePath the file path.
  *
  * @return the URL that can be used as source on the webview
  */