1
1
use std:: collections:: HashMap ;
2
-
2
+ use std:: sync:: Arc ;
3
+ use warp:: fs:: { file_reply, ArcPath , Conditionals , File } ;
3
4
use warp:: { path:: FullPath , Filter , Rejection } ;
4
5
5
6
/// A filter for static aliases that determines which file to serve.
@@ -23,9 +24,67 @@ pub fn static_aliases_filter(
23
24
if file_to_serve. is_empty ( ) {
24
25
Err ( warp:: reject:: not_found ( ) )
25
26
} else {
26
- // TODO Return the actual file (see Warp internals for this)
27
27
Ok ( file_to_serve)
28
28
}
29
29
} ,
30
30
)
31
31
}
32
+
33
+ /// Serves the file provided through the filter.
34
+ pub async fn serve_file ( path : String ) -> Result < File , Rejection > {
35
+ let arc_path = ArcPath ( Arc :: new ( path. into ( ) ) ) ;
36
+ let conds = Conditionals :: default ( ) ;
37
+ file_reply ( arc_path, conds) . await
38
+ }
39
+
40
+ // /// Serves the file provided through the filter. This returns an error because we assume that the file is supposed to exist at this point (this is used for static
41
+ // /// aliases).
42
+ // pub async fn serve_file(path: String) -> Result<Response, Rejection> {
43
+ // match TkFile::open(path).await {
44
+ // Ok(file) => {
45
+ // let metadata = file.metadata().await.map_err(|e| warp::reject::not_found())?;
46
+ // let stream = file_stream(file, metadata);
47
+ // let res = Response::new(Body::wrap_stream(stream));
48
+
49
+ // Ok(res)
50
+ // },
51
+ // // If a static alias can't be found, we'll act as if it doesn't exist and proceed to the next handler
52
+ // Err(_) => Err(warp::reject::not_found())
53
+ // }
54
+ // }
55
+
56
+ // // The default chunk size for streaming a file (taken from Warp's internals)
57
+ // const DFLT_BUF_SIZE: usize = 8_192;
58
+ // #[cfg(unix)]
59
+ // fn get_buf_size(metadata: Metadata) -> usize {
60
+ // use std::os::unix::prelude::MetadataExt;
61
+
62
+ // std::cmp::max(metadata.blksize() as usize, DFLT_BUF_SIZE)
63
+ // }
64
+ // #[cfg(not(unix))]
65
+ // fn get_buf_size(_metadata: Metadata) -> usize {
66
+ // DFLT_BUF_SIZE // On Windows, we don't have a blocksize function based on the metadata
67
+ // }
68
+
69
+ // /// Reserves more space in a buffer if needed
70
+ // fn reserve_if_needed(buf: &mut BytesMut, cap: usize) {
71
+ // if buf.capacity() - buf.len() < cap {
72
+ // buf.reserve(cap);
73
+ // }
74
+ // }
75
+
76
+ // fn file_stream(mut file: TkFile, metadata: Metadata) -> impl Stream<Item = Result<Bytes, std::io::Error>> + Send {
77
+ // let buf_size = get_buf_size(metadata);
78
+ // let stream = file.seek(SeekFrom::Start(0));
79
+
80
+ // let mut buf = BytesMut::new();
81
+ // reserve_if_needed(&mut buf, buf_size);
82
+
83
+ // try_stream! {
84
+ // for i in 0u8..3 {
85
+ // reserve_if_needed(&mut buf, buf_size);
86
+ // let n = file.read(&mut buf).await?;
87
+ // yield Bytes::from(buf[..n]);
88
+ // }
89
+ // }
90
+ // }
0 commit comments