strip and relativize paths on fs import

feat/vaults
Tomáš Mládek 2020-09-13 20:43:45 +02:00
parent ff457faca3
commit c9c005eff0
2 changed files with 32 additions and 27 deletions

View File

@ -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<T: AsRef<Path>>(
db_executor: &Addr<crate::database::DbExecutor>,
hasher_worker: &Addr<HasherWorker>,
) -> Result<()> {
for path in WalkDir::new(&directory)
let absolute_path = fs::canonicalize(&directory)?;
let path_entries: Result<Vec<PathBuf>, 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<T: AsRef<Path>>(
}
let digest = hasher_worker
.send(ComputeHash {
path: path.to_path_buf(),
})
.send(ComputeHash { path: path.clone() })
.await??;
let existing_file: Option<String> = db_executor
.send(RetrieveByHash {
hash: digest.clone(),
})
// let existing_file: Option<String> = 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::<Vec<Component>>();
let components = path
.strip_prefix(&absolute_path)?
.components()
.collect::<Vec<Component>>();
let (filename, dir_path) = components.split_last().unwrap();
let file_address = Address::UUID(Uuid::new_v4());

View File

@ -33,7 +33,7 @@ pub async fn get_raw(state: web::Data<State>, hash: web::Path<String>) -> 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)),