diff --git a/src/database/mod.rs b/src/database/mod.rs index 7b40292..7b0c125 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -249,6 +249,10 @@ impl UpEndConnection { .execute(&self.conn)?) } + pub fn normalize_path(&self, path: &Path) -> Result { + Ok(path.strip_prefix(self.vault_path.as_path())?.to_path_buf()) + } + pub fn retrieve_entry(&self, hash: Hash) -> Result> { use crate::database::inner::schema::data::dsl::*; diff --git a/src/filesystem.rs b/src/filesystem.rs index fcc27a3..316f60f 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -153,7 +153,6 @@ fn rescan_vault_inner>( db.connection().unwrap(), &resolve_cache, path.clone(), - &absolute_dir_path, &existing_files, ); @@ -240,11 +239,10 @@ fn rescan_vault_inner>( Ok(all_outcomes) } -fn process_directory_entry>( +fn process_directory_entry( connection: UpEndConnection, resolve_cache: &Arc>, path: PathBuf, - directory_path: &P, existing_files: &Arc>>, ) -> UpdatePathResult { debug!("Processing: {:?}", path); @@ -252,8 +250,10 @@ fn process_directory_entry>( // Prepare the data let existing_files = Arc::clone(existing_files); - let normalized_path = path.strip_prefix(&directory_path)?; - let normalized_path_str = normalized_path.to_str().expect("path not valid unicode?!"); + let normalized_path = connection.normalize_path(&path)?; + let normalized_path_str = normalized_path + .to_str() + .ok_or(anyhow!("Path not valid unicode!"))?; let mut file_hash: Option = None; @@ -324,7 +324,7 @@ fn process_directory_entry>( insert_file_with_metadata( &connection, - normalized_path, + &normalized_path, file_hash.unwrap(), size, mtime, @@ -337,6 +337,29 @@ fn process_directory_entry>( }) } +pub fn add_path(connection: &UpEndConnection, path: &Path, hash: Hash) -> Result<()> { + let normalized_path = connection.normalize_path(path)?; + let metadata = fs::metadata(&path)?; + let size = metadata.len() as i64; + let mtime = metadata + .modified() + .map(|t| { + NaiveDateTime::from_timestamp(t.duration_since(UNIX_EPOCH).unwrap().as_secs() as i64, 0) + }) + .ok(); + let mime_type = tree_magic_mini::from_filepath(path).map(|s| s.to_string()); + + insert_file_with_metadata( + connection, + &normalized_path, + hash, + size, + mtime, + mime_type, + None, + ) +} + fn insert_file_with_metadata( connection: &UpEndConnection, normalized_path: &Path, diff --git a/src/routes.rs b/src/routes.rs index 4212460..3d5812e 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,9 +1,9 @@ use crate::addressing::{Address, Addressable}; use crate::database::entry::{Entry, InEntry}; use crate::database::hierarchies::{list_roots, resolve_path, UHierPath}; -use crate::database::inner::models; use crate::database::lang::Query; use crate::database::UpEndDatabase; +use crate::filesystem::add_path; use crate::previews::PreviewStore; use crate::util::hash::{decode, encode, Hashable}; use crate::util::jobs::JobContainer; @@ -13,7 +13,6 @@ use actix_web::error::{ErrorBadRequest, ErrorInternalServerError, ErrorNotFound} use actix_web::http; use actix_web::{delete, error, get, post, put, web, Either, Error, HttpResponse}; use anyhow::{anyhow, Result}; -use chrono::{NaiveDateTime, Utc}; use futures_util::TryStreamExt; use log::{debug, info, trace}; use serde::Deserialize; @@ -23,7 +22,6 @@ use std::fs; use std::io::Write; use std::path::PathBuf; use std::sync::{Arc, RwLock}; -use std::time::UNIX_EPOCH; use std::{collections::HashMap, io}; use tempfile::NamedTempFile; @@ -236,34 +234,8 @@ pub async fn put_object( }) .await?; - let metadata = fs::metadata(&final_path)?; - let size = metadata.len() as i64; - let mtime = metadata - .modified() - .map(|t| { - NaiveDateTime::from_timestamp( - t.duration_since(UNIX_EPOCH).unwrap().as_secs() as i64, - 0, - ) - }) - .ok(); - let connection = state.upend.connection().map_err(ErrorInternalServerError)?; - - connection - .insert_file(models::NewFile { - path: final_name, - hash: hash.0, - added: NaiveDateTime::from_timestamp(Utc::now().timestamp(), 0), - size, - mtime, - }) - .map_err(ErrorInternalServerError)?; - - if let Some(filename) = filename { - upend_insert_val!(&connection, address, "LBL", filename) - .map_err(ErrorInternalServerError)?; - } + add_path(&connection, &final_path, hash).map_err(ErrorInternalServerError)?; Ok((address, None)) } else {