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>,
|
options: &mut Vec<TransferOption>,
|
||||||
to: &SocketAddr,
|
to: &SocketAddr,
|
||||||
) -> Result<(), Box<dyn Error>> {
|
) -> 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 => {
|
ErrorCode::FileNotFound => {
|
||||||
return Message::send_error_to(
|
return Message::send_error_to(
|
||||||
&self.socket,
|
&self.socket,
|
||||||
|
|
@ -85,7 +86,7 @@ impl Server {
|
||||||
ErrorCode::FileExists => Worker::send(
|
ErrorCode::FileExists => Worker::send(
|
||||||
self.socket.local_addr().unwrap(),
|
self.socket.local_addr().unwrap(),
|
||||||
*to,
|
*to,
|
||||||
filename,
|
file_path.to_path_buf(),
|
||||||
options.to_vec(),
|
options.to_vec(),
|
||||||
),
|
),
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|
@ -100,7 +101,8 @@ impl Server {
|
||||||
options: &mut Vec<TransferOption>,
|
options: &mut Vec<TransferOption>,
|
||||||
to: &SocketAddr,
|
to: &SocketAddr,
|
||||||
) -> Result<(), Box<dyn Error>> {
|
) -> 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 => {
|
ErrorCode::FileExists => {
|
||||||
return Message::send_error_to(
|
return Message::send_error_to(
|
||||||
&self.socket,
|
&self.socket,
|
||||||
|
|
@ -120,7 +122,7 @@ impl Server {
|
||||||
ErrorCode::FileNotFound => Worker::receive(
|
ErrorCode::FileNotFound => Worker::receive(
|
||||||
self.socket.local_addr().unwrap(),
|
self.socket.local_addr().unwrap(),
|
||||||
*to,
|
*to,
|
||||||
filename,
|
file_path.to_path_buf(),
|
||||||
options.to_vec(),
|
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)
|
!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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
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]
|
#[test]
|
||||||
fn validates_file_path() {
|
fn validates_file_path() {
|
||||||
assert!(validate_file_path(
|
assert!(validate_file_path(
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ use std::{
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{Read, Write},
|
io::{Read, Write},
|
||||||
net::{SocketAddr, UdpSocket},
|
net::{SocketAddr, UdpSocket},
|
||||||
path::Path,
|
path::{Path, PathBuf},
|
||||||
thread,
|
thread,
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
@ -35,16 +35,16 @@ impl Worker {
|
||||||
pub fn send(
|
pub fn send(
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
remote: SocketAddr,
|
remote: SocketAddr,
|
||||||
filename: String,
|
file_path: PathBuf,
|
||||||
mut options: Vec<TransferOption>,
|
mut options: Vec<TransferOption>,
|
||||||
) {
|
) {
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let mut handle_send = || -> Result<(), Box<dyn Error>> {
|
let mut handle_send = || -> Result<(), Box<dyn Error>> {
|
||||||
let socket = setup_socket(&addr, &remote)?;
|
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)?;
|
accept_request(&socket, &options, &work_type)?;
|
||||||
check_response(&socket)?;
|
check_response(&socket)?;
|
||||||
send_file(&socket, &filename, &mut options)?;
|
send_file(&socket, &file_path, &mut options)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
};
|
};
|
||||||
|
|
@ -58,7 +58,7 @@ impl Worker {
|
||||||
pub fn receive(
|
pub fn receive(
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
remote: SocketAddr,
|
remote: SocketAddr,
|
||||||
filename: String,
|
file_path: PathBuf,
|
||||||
mut options: Vec<TransferOption>,
|
mut options: Vec<TransferOption>,
|
||||||
) {
|
) {
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
|
|
@ -66,7 +66,7 @@ impl Worker {
|
||||||
let socket = setup_socket(&addr, &remote)?;
|
let socket = setup_socket(&addr, &remote)?;
|
||||||
let work_type = WorkType::Receive;
|
let work_type = WorkType::Receive;
|
||||||
accept_request(&socket, &options, &work_type)?;
|
accept_request(&socket, &options, &work_type)?;
|
||||||
receive_file(&socket, &filename, &mut options)?;
|
receive_file(&socket, &file_path, &mut options)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
};
|
};
|
||||||
|
|
@ -80,10 +80,10 @@ impl Worker {
|
||||||
|
|
||||||
fn send_file(
|
fn send_file(
|
||||||
socket: &UdpSocket,
|
socket: &UdpSocket,
|
||||||
filename: &String,
|
file_path: &PathBuf,
|
||||||
options: &mut Vec<TransferOption>,
|
options: &mut Vec<TransferOption>,
|
||||||
) -> Result<(), Box<dyn Error>> {
|
) -> 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 worker_options = parse_options(options, &WorkType::Send(file.metadata().unwrap().len()))?;
|
||||||
|
|
||||||
let mut block_number = 1;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn receive_file(
|
fn receive_file(
|
||||||
socket: &UdpSocket,
|
socket: &UdpSocket,
|
||||||
filename: &String,
|
file_path: &PathBuf,
|
||||||
options: &mut Vec<TransferOption>,
|
options: &mut Vec<TransferOption>,
|
||||||
) -> Result<(), Box<dyn Error>> {
|
) -> 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 worker_options = parse_options(options, &WorkType::Receive)?;
|
||||||
|
|
||||||
let mut block_number: u16 = 0;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue