diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index 0d4116c..ab829e0 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -7,6 +7,7 @@ use crate::types::files::{ use crate::types::identifier::FileIdentifier; use serde::{Deserialize, Serialize}; use std::path::PathBuf; +use std::time::SystemTime; use tokio::fs; use tokio::fs::DirEntry; @@ -33,12 +34,11 @@ pub async fn add_local_file( let api = api_state.api().await?; let path = PathBuf::from(&metadata.path); let mut tags = Vec::new(); + let txt_path = PathBuf::from(format!("{}.txt", path.to_string_lossy())); if options.read_tags_from_txt { - let txt_path = PathBuf::from(format!("{}.txt", path.to_string_lossy())); - if txt_path.exists() { - let content = fs::read_to_string(txt_path).await?; + let content = fs::read_to_string(&txt_path).await?; tags.append( &mut content .split('\n') @@ -48,8 +48,15 @@ pub async fn add_local_file( } } - let file_content = fs::read(path).await?; + let file_content = fs::read(&path).await?; let file = api.file.add_file(metadata, tags, file_content).await?; + if options.delete_after_import { + fs::remove_file(path).await?; + + if options.read_tags_from_txt { + fs::remove_file(txt_path).await?; + } + } Ok(file) } @@ -116,7 +123,7 @@ async fn resolve_path_to_files(path: PathBuf) -> PluginResult PluginResult> { async fn retrieve_file_information(path: PathBuf) -> PluginResult { let mime = mime_guess::from_path(&path).first(); let metadata = fs::metadata(&path).await?; - let creation_time = metadata.created()?; - let change_time = metadata.modified()?; + let creation_time = metadata.created().unwrap_or(SystemTime::now()); + let change_time = metadata.modified().unwrap_or(SystemTime::now()); let name = path .file_name() .ok_or_else(|| "Could not retrieve file name")?;