diff --git a/src/filesystem.rs b/src/filesystem.rs index f4f2752..99eae3d 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -1,7 +1,6 @@ use crate::addressing::Address; use crate::database::{ - DbExecutor, Entry, EntryValue, InnerEntry, InsertEntry, QueryEntries, RetrieveByHash, - RetrieveObject, + DbExecutor, Entry, EntryValue, InnerEntry, InsertEntry, QueryEntries, RetrieveObject, }; use crate::hash::{ComputeHash, HasherWorker}; use crate::models; @@ -293,12 +292,15 @@ async fn _reimport_directory>( db_executor: &Addr, hasher_worker: &Addr, ) -> Result<()> { - for path in WalkDir::new(&directory) + let absolute_path = fs::canonicalize(&directory)?; + let path_entries: Result, std::io::Error> = WalkDir::new(&directory) .into_iter() .filter_map(|e| e.ok()) .filter(|e| e.path().is_file()) - .map(|e| e.into_path()) - { + .map(|e| fs::canonicalize(e.into_path())) + .collect(); + + for path in path_entries? { info!("Processing: {:?}", path); let metadata = fs::metadata(&path)?; @@ -308,31 +310,34 @@ async fn _reimport_directory>( } let digest = hasher_worker - .send(ComputeHash { - path: path.to_path_buf(), - }) + .send(ComputeHash { path: path.clone() }) .await??; - let existing_file: Option = db_executor - .send(RetrieveByHash { - hash: digest.clone(), - }) + // let existing_file: Option = db_executor + // .send(RetrieveByHash { + // hash: digest.clone(), + // }) + // .await??; + + let new_file = models::NewFile { + path: path + .strip_prefix(&absolute_path)? + .to_str() + .expect("path not valid unicode?!") + .to_string(), + hash: (digest.clone()).0, + created: NaiveDateTime::from_timestamp(Utc::now().timestamp(), 0), + size, + }; + + db_executor + .send(crate::database::InsertFile { file: new_file }) .await??; - if existing_file.is_none() { - let new_file = models::NewFile { - path: path.to_str().expect("path not valid unicode?!").to_string(), - hash: (digest.clone()).0, - created: NaiveDateTime::from_timestamp(Utc::now().timestamp(), 0), - size, - }; - - db_executor - .send(crate::database::InsertFile { file: new_file }) - .await??; - } - - let components = path.components().collect::>(); + let components = path + .strip_prefix(&absolute_path)? + .components() + .collect::>(); let (filename, dir_path) = components.split_last().unwrap(); let file_address = Address::UUID(Uuid::new_v4()); diff --git a/src/routes.rs b/src/routes.rs index 5a892f9..30a9cc8 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -33,7 +33,7 @@ pub async fn get_raw(state: web::Data, hash: web::Path) -> Result match response { Ok(result) => match result { - Some(path) => Ok(NamedFile::open(path)?), + Some(path) => Ok(NamedFile::open(state.directory.join(path))?), None => Err(error::ErrorNotFound("NOT FOUND")), }, Err(e) => Err(error::ErrorInternalServerError(e)),