diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 48d6598..37ed4a5 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -2849,7 +2849,7 @@ checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" [[package]] name = "s3_manager" -version = "0.9.1" +version = "0.9.2" dependencies = [ "aws-config", "aws-sdk-s3", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index da039dc..f4ea126 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "s3_manager" -version = "0.9.1" +version = "0.9.2" description = "S3 Manager: A tool to manage S3 buckets" authors = ["Lukasz Celitan, @SpaceoutPL"] license = "MIT" diff --git a/src-tauri/src/lib/s3/put/files.rs b/src-tauri/src/lib/s3/put/files.rs index 665b435..2f4dfa5 100644 --- a/src-tauri/src/lib/s3/put/files.rs +++ b/src-tauri/src/lib/s3/put/files.rs @@ -1,4 +1,5 @@ use crate::lib::s3::client::client::create_client; +use crate::lib::s3::utils::get_file_name::get_file_name; use aws_sdk_s3::{types::ByteStream, Client}; use std::{error::Error, fs, path::Path}; use tauri::Window; @@ -35,15 +36,7 @@ pub async fn put_files( if path.is_dir() { for fil in WalkDir::new(&file).into_iter().filter_map(|fil| fil.ok()) { if fil.metadata().unwrap().is_file() { - let filename = &fil - .path() - .display() - .to_string() - .split("/") - .last() - .unwrap_or_default() - .to_string(); - + let filename = get_file_name(&fil.path().to_str().unwrap_or_default()); let key = folder_name.clone() + "/" + filename; println!("{} {}", filename, !filename.starts_with(".")); @@ -62,11 +55,17 @@ pub async fn put_files( } } } else { - let filename = &file.split("/").last().unwrap_or_default().to_string(); + let filename = get_file_name(&file); + let key = folder_name.to_string() + "/" + filename; - put_file(&client, bucket_name.to_string(), file, key.to_string()) - .await - .unwrap(); + put_file( + &client, + bucket_name.to_string(), + file.clone(), + key.to_string(), + ) + .await + .unwrap(); window.emit("event-upload-file", &filename).unwrap() } diff --git a/src-tauri/src/lib/s3/utils/get_file_name.rs b/src-tauri/src/lib/s3/utils/get_file_name.rs new file mode 100644 index 0000000..591157d --- /dev/null +++ b/src-tauri/src/lib/s3/utils/get_file_name.rs @@ -0,0 +1,23 @@ +// Import the std::path::Path type +use std::path::Path; + +// Define a function to get a file name from a full path +pub fn get_file_name(full_path: &str) -> &str { + // Check if the full path is empty + if full_path.is_empty() { + // Return an empty string + return ""; + } + + // Parse the full path as a Path type + let path = Path::new(full_path); + + // Get the file name as an Option<&OsStr> + let file_name = path.file_name(); + + // Convert the file name to a string slice or return an empty string + match file_name { + Some(name) => name.to_str().unwrap_or(""), + None => "", + } +} diff --git a/src-tauri/src/lib/s3/utils/mod.rs b/src-tauri/src/lib/s3/utils/mod.rs index bb28608..579deb2 100644 --- a/src-tauri/src/lib/s3/utils/mod.rs +++ b/src-tauri/src/lib/s3/utils/mod.rs @@ -1,2 +1,3 @@ +pub mod get_file_name; pub mod presigned_url; pub mod response_error; diff --git a/src/components/dropFiles/dropFiles.svelte b/src/components/dropFiles/dropFiles.svelte index 2fd2b8b..b536d32 100644 --- a/src/components/dropFiles/dropFiles.svelte +++ b/src/components/dropFiles/dropFiles.svelte @@ -13,6 +13,7 @@ import Check from "../icons/check.svelte"; import CircularProgress from "../circularProgress/circularProgress.svelte"; import { showModal } from "src/store/modal"; + import { getFileName } from "src/lib/getFileName"; let visible = false; let loading = false; @@ -70,7 +71,7 @@ files = [...new Set([...files, ...paths])]; // eslint-disable-next-line @typescript-eslint/no-misused-promises [...new Set([...files, ...paths])].forEach(async (file: string) => { - const getName = file.split("/")[file.split("/").length - 1]; + const getName = getFileName(file); if (!getName.includes(".")) { const files: FileEntry[] = await readDir(file, { recursive: true }); const filesArray = getAllFiles(files); diff --git a/src/lib/getFileName.ts b/src/lib/getFileName.ts new file mode 100644 index 0000000..362dd31 --- /dev/null +++ b/src/lib/getFileName.ts @@ -0,0 +1,16 @@ +// Define a function to get a file name from a full path +export function getFileName(fullPath: string): string { + + if (!fullPath) { + return ""; + } + + // Split the full path by slash (/) or backslash (\) depending on OS + let splitPath = fullPath.split("/"); + if (splitPath.length === 1) { + // Assume Windows OS and use backslash (\) + splitPath = fullPath.split("\\"); + } + + return splitPath[splitPath.length - 1]; + } \ No newline at end of file