January 2019
Intermediate to advanced
520 pages
14h 32m
English
Let's implement a branch to download images. The handler can download files using the /download/filename path. To extract the name of the file, we use a regular expression:
lazy_static! { static ref DOWNLOAD_FILE: Regex = Regex::new("^/download/(?P<filename>\\w{20})?$").unwrap();}
We will use startwith to detect the /download part of the path. Take a look at the implementation:
(&Method::GET, path) if path.starts_with("/download") => { if let Some(cap) = DOWNLOAD_FILE.captures(path) { let filename = cap.name("filename").unwrap().as_str(); let mut filepath = files.to_path_buf(); filepath.push(filename); let open_file = File::open(filepath); let body = open_file.map(|file| { let chunks = FileChunkStream::new(file); Response::new(Body::wrap_stream(chunks)) ...Read now
Unlock full access