55use crate :: { path:: SafePathBuf , scope, webview:: UriSchemeProtocolHandler } ;
66use http:: { header:: * , status:: StatusCode , Request , Response } ;
77use http_range:: HttpRange ;
8+ use std:: fs:: File ;
9+ use std:: io:: { Read , Seek , Write } ;
810use std:: { borrow:: Cow , io:: SeekFrom } ;
911use tauri_utils:: mime_type:: MimeType ;
10- use tokio:: fs:: File ;
11- use tokio:: io:: { AsyncReadExt , AsyncSeekExt , AsyncWriteExt } ;
1212
1313pub fn get ( scope : scope:: fs:: Scope , window_origin : String ) -> UriSchemeProtocolHandler {
1414 Box :: new (
@@ -49,7 +49,7 @@ fn get_response(
4949 }
5050
5151 // Separate block for easier error handling
52- let mut file = match crate :: async_runtime :: safe_block_on ( File :: open ( path. clone ( ) ) ) {
52+ let mut file = match File :: open ( path. clone ( ) ) {
5353 Ok ( file) => file,
5454 Err ( e) => {
5555 #[ cfg( target_os = "android" ) ]
@@ -70,32 +70,20 @@ fn get_response(
7070 }
7171 } ;
7272
73- let ( mut file, len, mime_type, read_bytes) = crate :: async_runtime:: safe_block_on ( async move {
74- // get file length
75- let len = {
76- let old_pos = file. stream_position ( ) . await ?;
77- let len = file. seek ( SeekFrom :: End ( 0 ) ) . await ?;
78- file. seek ( SeekFrom :: Start ( old_pos) ) . await ?;
79- len
80- } ;
81-
73+ let len = file. metadata ( ) ?. len ( ) ;
74+ let ( mime_type, read_bytes) = {
8275 // get file mime type
83- let ( mime_type, read_bytes) = {
84- let nbytes = len. min ( 8192 ) ;
85- let mut magic_buf = Vec :: with_capacity ( nbytes as usize ) ;
86- let old_pos = file. stream_position ( ) . await ?;
87- ( & mut file) . take ( nbytes) . read_to_end ( & mut magic_buf) . await ?;
88- file. seek ( SeekFrom :: Start ( old_pos) ) . await ?;
89- (
90- MimeType :: parse ( & magic_buf, & path) ,
91- // return the `magic_bytes` if we read the whole file
92- // to avoid reading it again later if this is not a range request
93- if len < 8192 { Some ( magic_buf) } else { None } ,
94- )
95- } ;
96-
97- Ok :: < ( File , u64 , String , Option < Vec < u8 > > ) , anyhow:: Error > ( ( file, len, mime_type, read_bytes) )
98- } ) ?;
76+ let nbytes = len. min ( 8192 ) ;
77+ let mut magic_buf = Vec :: with_capacity ( nbytes as usize ) ;
78+ ( & mut file) . take ( nbytes) . read_to_end ( & mut magic_buf) ?;
79+ file. rewind ( ) ?;
80+ (
81+ MimeType :: parse ( & magic_buf, & path) ,
82+ // return the `magic_bytes` if we read the whole file
83+ // to avoid reading it again later if this is not a range request
84+ if len < 8192 { Some ( magic_buf) } else { None } ,
85+ )
86+ } ;
9987
10088 resp = resp. header ( CONTENT_TYPE , & mime_type) ;
10189
@@ -148,12 +136,12 @@ fn get_response(
148136 // calculate number of bytes needed to be read
149137 let nbytes = end + 1 - start;
150138
151- let buf = crate :: async_runtime :: safe_block_on ( async move {
139+ let buf = {
152140 let mut buf = Vec :: with_capacity ( nbytes as usize ) ;
153- file. seek ( SeekFrom :: Start ( start) ) . await ?;
154- file. take ( nbytes) . read_to_end ( & mut buf) . await ?;
155- Ok :: < Vec < u8 > , anyhow :: Error > ( buf)
156- } ) ? ;
141+ file. seek ( SeekFrom :: Start ( start) ) ?;
142+ file. take ( nbytes) . read_to_end ( & mut buf) ?;
143+ buf
144+ } ;
157145
158146 resp = resp. header ( CONTENT_RANGE , format ! ( "bytes {start}-{end}/{len}" ) ) ;
159147 resp = resp. header ( CONTENT_LENGTH , end + 1 - start) ;
@@ -186,38 +174,34 @@ fn get_response(
186174 format ! ( "multipart/byteranges; boundary={boundary}" ) ,
187175 ) ;
188176
189- let buf = crate :: async_runtime :: safe_block_on ( async move {
177+ let buf = {
190178 // multi-part range header
191179 let mut buf = Vec :: new ( ) ;
192180
193181 for ( start, end) in ranges {
194182 // a new range is being written, write the range boundary
195- buf. write_all ( boundary_sep. as_bytes ( ) ) . await ?;
183+ buf. write_all ( boundary_sep. as_bytes ( ) ) ?;
196184
197185 // write the needed headers `Content-Type` and `Content-Range`
198- buf
199- . write_all ( format ! ( "{CONTENT_TYPE}: {mime_type}\r \n " ) . as_bytes ( ) )
200- . await ?;
201- buf
202- . write_all ( format ! ( "{CONTENT_RANGE}: bytes {start}-{end}/{len}\r \n " ) . as_bytes ( ) )
203- . await ?;
186+ buf. write_all ( format ! ( "{CONTENT_TYPE}: {mime_type}\r \n " ) . as_bytes ( ) ) ?;
187+ buf. write_all ( format ! ( "{CONTENT_RANGE}: bytes {start}-{end}/{len}\r \n " ) . as_bytes ( ) ) ?;
204188
205189 // write the separator to indicate the start of the range body
206- buf. write_all ( "\r \n " . as_bytes ( ) ) . await ?;
190+ buf. write_all ( "\r \n " . as_bytes ( ) ) ?;
207191
208192 // calculate number of bytes needed to be read
209193 let nbytes = end + 1 - start;
210194
211195 let mut local_buf = Vec :: with_capacity ( nbytes as usize ) ;
212- file. seek ( SeekFrom :: Start ( start) ) . await ?;
213- ( & mut file) . take ( nbytes) . read_to_end ( & mut local_buf) . await ?;
196+ file. seek ( SeekFrom :: Start ( start) ) ?;
197+ ( & mut file) . take ( nbytes) . read_to_end ( & mut local_buf) ?;
214198 buf. extend_from_slice ( & local_buf) ;
215199 }
216200 // all ranges have been written, write the closing boundary
217- buf. write_all ( boundary_closer. as_bytes ( ) ) . await ?;
201+ buf. write_all ( boundary_closer. as_bytes ( ) ) ?;
218202
219- Ok :: < Vec < u8 > , anyhow :: Error > ( buf)
220- } ) ? ;
203+ buf
204+ } ;
221205 resp. body ( buf. into ( ) )
222206 }
223207 } else if request. method ( ) == http:: Method :: HEAD {
@@ -230,11 +214,9 @@ fn get_response(
230214 let buf = if let Some ( b) = read_bytes {
231215 b
232216 } else {
233- crate :: async_runtime:: safe_block_on ( async move {
234- let mut local_buf = Vec :: with_capacity ( len as usize ) ;
235- file. read_to_end ( & mut local_buf) . await ?;
236- Ok :: < Vec < u8 > , anyhow:: Error > ( local_buf)
237- } ) ?
217+ let mut local_buf = Vec :: with_capacity ( len as usize ) ;
218+ file. read_to_end ( & mut local_buf) ?;
219+ local_buf
238220 } ;
239221 resp = resp. header ( CONTENT_LENGTH , len) ;
240222 resp. body ( buf. into ( ) )
0 commit comments