fix conflict

This commit is contained in:
Ffauzan
2022-07-19 11:27:28 +07:00

View File

@@ -1,164 +1,156 @@
use file_diff::diff; use file_diff::diff;
use std::fs; use std::fs;
use std::io::{Read, Write}; use std::io::{Read, Write};
#[tauri::command] #[tauri::command]
pub fn rename(path: String, new_name: String) { pub fn rename(path: String, new_name: String) {
let mut new_path = path.clone(); let mut new_path = path.clone();
// Check if file/folder to replace exists // Check if file/folder to replace exists
if fs::metadata(&path).is_err() { if fs::metadata(&path).is_err() {
return; return;
} }
// Check if path uses forward or back slashes // Check if path uses forward or back slashes
if new_path.contains('\\') { if new_path.contains('\\') {
new_path = path.replace('\\', "/"); new_path = path.replace('\\', "/");
} }
let path_replaced = &path.replace(&new_path.split('/').last().unwrap(), &new_name); let path_replaced = &path.replace(&new_path.split('/').last().unwrap(), &new_name);
fs::rename(path, &path_replaced).unwrap(); fs::rename(path, &path_replaced).unwrap();
} }
#[tauri::command] #[tauri::command]
pub fn dir_create(path: String) { pub fn dir_create(path: String) {
fs::create_dir_all(path).unwrap(); fs::create_dir_all(path).unwrap();
} }
#[tauri::command] #[tauri::command]
pub fn dir_exists(path: &str) -> bool { pub fn dir_exists(path: &str) -> bool {
let path_buf = std::path::PathBuf::from(path); let path_buf = std::path::PathBuf::from(path);
fs::metadata(path_buf).is_ok() fs::metadata(path_buf).is_ok()
} }
#[tauri::command] #[tauri::command]
pub fn dir_is_empty(path: &str) -> bool { pub fn dir_is_empty(path: &str) -> bool {
let path_buf = std::path::PathBuf::from(path); let path_buf = std::path::PathBuf::from(path);
fs::read_dir(path_buf).unwrap().count() == 0 fs::read_dir(path_buf).unwrap().count() == 0
} }
#[tauri::command] #[tauri::command]
pub fn dir_delete(path: &str) { pub fn dir_delete(path: &str) {
let path_buf = std::path::PathBuf::from(path); let path_buf = std::path::PathBuf::from(path);
fs::remove_dir_all(path_buf).unwrap(); fs::remove_dir_all(path_buf).unwrap();
} }
#[tauri::command] #[tauri::command]
pub fn are_files_identical(path1: &str, path2: &str) -> bool { pub fn are_files_identical(path1: &str, path2: &str) -> bool {
diff(path1, path2) diff(path1, path2)
} }
#[tauri::command] #[tauri::command]
pub fn copy_file(path: String, new_path: String) -> bool { pub fn copy_file(path: String, new_path: String) -> bool {
let filename = &path.split('/').last().unwrap(); let filename = &path.split('/').last().unwrap();
let mut new_path_buf = std::path::PathBuf::from(&new_path); let mut new_path_buf = std::path::PathBuf::from(&new_path);
let path_buf = std::path::PathBuf::from(&path); let path_buf = std::path::PathBuf::from(&path);
// If the new path doesn't exist, create it. // If the new path doesn't exist, create it.
if !dir_exists(new_path_buf.pop().to_string().as_str()) { if !dir_exists(new_path_buf.pop().to_string().as_str()) {
std::fs::create_dir_all(&new_path).unwrap(); std::fs::create_dir_all(&new_path).unwrap();
} }
// Copy old to new // Copy old to new
match std::fs::copy(&path_buf, format!("{}/{}", new_path, filename)) { match std::fs::copy(&path_buf, format!("{}/{}", new_path, filename)) {
Ok(_) => true, Ok(_) => true,
Err(e) => { Err(e) => {
println!("Failed to copy file: {}", e); println!("Failed to copy file: {}", e);
println!("Path: {}", path); println!("Path: {}", path);
println!("New Path: {}", new_path); println!("New Path: {}", new_path);
false false
} }
} }
} }
#[tauri::command] #[tauri::command]
pub fn copy_file_with_new_name(path: String, new_path: String, new_name: String) -> bool { pub fn copy_file_with_new_name(path: String, new_path: String, new_name: String) -> bool {
let mut new_path_buf = std::path::PathBuf::from(&new_path); let mut new_path_buf = std::path::PathBuf::from(&new_path);
let path_buf = std::path::PathBuf::from(&path); let path_buf = std::path::PathBuf::from(&path);
// If the new path doesn't exist, create it. // If the new path doesn't exist, create it.
if !dir_exists(new_path_buf.pop().to_string().as_str()) { if !dir_exists(new_path_buf.pop().to_string().as_str()) {
match std::fs::create_dir_all(&new_path) { match std::fs::create_dir_all(&new_path) {
Ok(_) => {} Ok(_) => {}
Err(e) => { Err(e) => {
println!("Failed to create directory: {}", e); println!("Failed to create directory: {}", e);
return false; return false;
} }
}; };
} }
// Copy old to new // Copy old to new
match std::fs::copy(&path_buf, format!("{}/{}", new_path, new_name)) { match std::fs::copy(&path_buf, format!("{}/{}", new_path, new_name)) {
Ok(_) => true, Ok(_) => true,
Err(e) => { Err(e) => {
println!("Failed to copy file: {}", e); println!("Failed to copy file: {}", e);
println!("Path: {}", path); println!("Path: {}", path);
println!("New Path: {}", new_path); println!("New Path: {}", new_path);
false false
} }
} }
} }
#[tauri::command] #[tauri::command]
pub fn delete_file(path: String) -> bool { pub fn delete_file(path: String) -> bool {
let path_buf = std::path::PathBuf::from(&path); let path_buf = std::path::PathBuf::from(&path);
match std::fs::remove_file(path_buf) { match std::fs::remove_file(path_buf) {
Ok(_) => true, Ok(_) => true,
Err(e) => { Err(e) => {
println!("Failed to delete file: {}", e); println!("Failed to delete file: {}", e);
false false
} }
}; };
false false
} }
#[tauri::command] #[tauri::command]
pub fn read_file(path: String) -> String { pub fn read_file(path: String) -> String {
let path_buf = std::path::PathBuf::from(&path); let path_buf = std::path::PathBuf::from(&path);
let mut file = match fs::File::open(path_buf) { let mut file = match fs::File::open(path_buf) {
Ok(file) => file, Ok(file) => file,
Err(e) => { Err(e) => {
println!("Failed to open file: {}", e); println!("Failed to open file: {}", e);
return String::new(); return String::new();
} }
}; };
let mut contents = String::new(); let mut contents = String::new();
file.read_to_string(&mut contents).unwrap(); file.read_to_string(&mut contents).unwrap();
contents contents
} }
#[tauri::command] #[tauri::command]
pub fn write_file(path: String, contents: String) { pub fn write_file(path: String, contents: String) {
let path_buf = std::path::PathBuf::from(&path); let path_buf = std::path::PathBuf::from(&path);
// Create file if it exists, otherwise just open and rewrite // Create file if it exists, otherwise just open and rewrite
let mut file = match fs::File::options().write(true).truncate(true).open(&path_buf) { let mut file = match fs::File::create(&path_buf) {
Ok(file) => file, Ok(file) => file,
Err(e) => { Err(e) => {
println!("Failed to open file: {}", e); println!("Failed to open file: {}", e);
return;
// attempt to create file. otherwise return }
match fs::File::create(&path_buf) { };
Ok(file) => file,
Err(e) => { // Write contents to file
println!("Failed to create file: {}", e); match file.write_all(contents.as_bytes()) {
return; Ok(_) => (),
} Err(e) => {
} println!("Failed to write to file: {}", e);
} }
}; }
}
// Write contents to file
match file.write_all(contents.as_bytes()) {
Ok(_) => (),
Err(e) => {
println!("Failed to write to file: {}", e);
}
}
}