Refactor file path
This commit is contained in:
parent
80bdf7035c
commit
a3dd7fecb4
2 changed files with 26 additions and 35 deletions
|
|
@ -65,7 +65,8 @@ impl Server {
|
|||
options: &mut Vec<TransferOption>,
|
||||
to: &SocketAddr,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
match check_file_exists(&get_full_path(&filename, &self.directory), &self.directory) {
|
||||
let file_path = &self.directory.join(&filename);
|
||||
match check_file_exists(&file_path, &self.directory) {
|
||||
ErrorCode::FileNotFound => {
|
||||
return Message::send_error_to(
|
||||
&self.socket,
|
||||
|
|
@ -85,7 +86,7 @@ impl Server {
|
|||
ErrorCode::FileExists => Worker::send(
|
||||
self.socket.local_addr().unwrap(),
|
||||
*to,
|
||||
filename,
|
||||
file_path.to_path_buf(),
|
||||
options.to_vec(),
|
||||
),
|
||||
_ => {}
|
||||
|
|
@ -100,7 +101,8 @@ impl Server {
|
|||
options: &mut Vec<TransferOption>,
|
||||
to: &SocketAddr,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
match check_file_exists(&get_full_path(&filename, &self.directory), &self.directory) {
|
||||
let file_path = &self.directory.join(&filename);
|
||||
match check_file_exists(&file_path, &self.directory) {
|
||||
ErrorCode::FileExists => {
|
||||
return Message::send_error_to(
|
||||
&self.socket,
|
||||
|
|
@ -120,7 +122,7 @@ impl Server {
|
|||
ErrorCode::FileNotFound => Worker::receive(
|
||||
self.socket.local_addr().unwrap(),
|
||||
*to,
|
||||
filename,
|
||||
file_path.to_path_buf(),
|
||||
options.to_vec(),
|
||||
),
|
||||
_ => {}
|
||||
|
|
@ -146,29 +148,10 @@ fn validate_file_path(file: &PathBuf, directory: &PathBuf) -> bool {
|
|||
!file.to_str().unwrap().contains("..") && file.ancestors().any(|a| a == directory)
|
||||
}
|
||||
|
||||
fn get_full_path(filename: &str, directory: &PathBuf) -> PathBuf {
|
||||
let mut file = directory.clone();
|
||||
file.push(PathBuf::from(filename));
|
||||
file
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn gets_full_path() {
|
||||
assert_eq!(
|
||||
get_full_path("test.txt", &PathBuf::from("/dir/test")),
|
||||
PathBuf::from("/dir/test/test.txt")
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
get_full_path("some_dir/test.txt", &PathBuf::from("/dir/test")),
|
||||
PathBuf::from("/dir/test/some_dir/test.txt")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validates_file_path() {
|
||||
assert!(validate_file_path(
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use std::{
|
|||
fs::File,
|
||||
io::{Read, Write},
|
||||
net::{SocketAddr, UdpSocket},
|
||||
path::Path,
|
||||
path::{Path, PathBuf},
|
||||
thread,
|
||||
time::Duration,
|
||||
};
|
||||
|
|
@ -35,16 +35,16 @@ impl Worker {
|
|||
pub fn send(
|
||||
addr: SocketAddr,
|
||||
remote: SocketAddr,
|
||||
filename: String,
|
||||
file_path: PathBuf,
|
||||
mut options: Vec<TransferOption>,
|
||||
) {
|
||||
thread::spawn(move || {
|
||||
let mut handle_send = || -> Result<(), Box<dyn Error>> {
|
||||
let socket = setup_socket(&addr, &remote)?;
|
||||
let work_type = WorkType::Send(Path::new(&filename).metadata().unwrap().len());
|
||||
let work_type = WorkType::Send(Path::new(&file_path).metadata().unwrap().len());
|
||||
accept_request(&socket, &options, &work_type)?;
|
||||
check_response(&socket)?;
|
||||
send_file(&socket, &filename, &mut options)?;
|
||||
send_file(&socket, &file_path, &mut options)?;
|
||||
|
||||
Ok(())
|
||||
};
|
||||
|
|
@ -58,7 +58,7 @@ impl Worker {
|
|||
pub fn receive(
|
||||
addr: SocketAddr,
|
||||
remote: SocketAddr,
|
||||
filename: String,
|
||||
file_path: PathBuf,
|
||||
mut options: Vec<TransferOption>,
|
||||
) {
|
||||
thread::spawn(move || {
|
||||
|
|
@ -66,7 +66,7 @@ impl Worker {
|
|||
let socket = setup_socket(&addr, &remote)?;
|
||||
let work_type = WorkType::Receive;
|
||||
accept_request(&socket, &options, &work_type)?;
|
||||
receive_file(&socket, &filename, &mut options)?;
|
||||
receive_file(&socket, &file_path, &mut options)?;
|
||||
|
||||
Ok(())
|
||||
};
|
||||
|
|
@ -80,10 +80,10 @@ impl Worker {
|
|||
|
||||
fn send_file(
|
||||
socket: &UdpSocket,
|
||||
filename: &String,
|
||||
file_path: &PathBuf,
|
||||
options: &mut Vec<TransferOption>,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
let mut file = File::open(filename).unwrap();
|
||||
let mut file = File::open(file_path).unwrap();
|
||||
let worker_options = parse_options(options, &WorkType::Send(file.metadata().unwrap().len()))?;
|
||||
|
||||
let mut block_number = 1;
|
||||
|
|
@ -119,16 +119,20 @@ fn send_file(
|
|||
};
|
||||
}
|
||||
|
||||
println!("Sent {filename} to {}", socket.peer_addr().unwrap());
|
||||
println!(
|
||||
"Sent {} to {}",
|
||||
file_path.display(),
|
||||
socket.peer_addr().unwrap()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn receive_file(
|
||||
socket: &UdpSocket,
|
||||
filename: &String,
|
||||
file_path: &PathBuf,
|
||||
options: &mut Vec<TransferOption>,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
let mut file = File::create(filename).unwrap();
|
||||
let mut file = File::create(file_path).unwrap();
|
||||
let worker_options = parse_options(options, &WorkType::Receive)?;
|
||||
|
||||
let mut block_number: u16 = 0;
|
||||
|
|
@ -170,7 +174,11 @@ fn receive_file(
|
|||
};
|
||||
}
|
||||
|
||||
println!("Received {filename} from {}", socket.peer_addr().unwrap());
|
||||
println!(
|
||||
"Received {} from {}",
|
||||
file_path.display(),
|
||||
socket.peer_addr().unwrap()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue