unify adding files via rescans and ui

feat/vaults
Tomáš Mládek 2022-01-23 14:50:37 +01:00
parent ef59090859
commit 893a87a094
No known key found for this signature in database
GPG Key ID: ED21612889E75EC5
3 changed files with 35 additions and 36 deletions

View File

@ -249,6 +249,10 @@ impl UpEndConnection {
.execute(&self.conn)?)
}
pub fn normalize_path(&self, path: &Path) -> Result<PathBuf> {
Ok(path.strip_prefix(self.vault_path.as_path())?.to_path_buf())
}
pub fn retrieve_entry(&self, hash: Hash) -> Result<Option<Entry>> {
use crate::database::inner::schema::data::dsl::*;

View File

@ -153,7 +153,6 @@ fn rescan_vault_inner<D: Borrow<UpEndDatabase>>(
db.connection().unwrap(),
&resolve_cache,
path.clone(),
&absolute_dir_path,
&existing_files,
);
@ -240,11 +239,10 @@ fn rescan_vault_inner<D: Borrow<UpEndDatabase>>(
Ok(all_outcomes)
}
fn process_directory_entry<P: AsRef<Path>>(
fn process_directory_entry(
connection: UpEndConnection,
resolve_cache: &Arc<Mutex<ResolveCache>>,
path: PathBuf,
directory_path: &P,
existing_files: &Arc<RwLock<Vec<models::File>>>,
) -> UpdatePathResult {
debug!("Processing: {:?}", path);
@ -252,8 +250,10 @@ fn process_directory_entry<P: AsRef<Path>>(
// 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<Hash> = None;
@ -324,7 +324,7 @@ fn process_directory_entry<P: AsRef<Path>>(
insert_file_with_metadata(
&connection,
normalized_path,
&normalized_path,
file_hash.unwrap(),
size,
mtime,
@ -337,6 +337,29 @@ fn process_directory_entry<P: AsRef<Path>>(
})
}
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,

View File

@ -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 {