Skip to content
This repository has been archived by the owner on Jan 14, 2025. It is now read-only.

Commit

Permalink
Merge branch 'raisfeld-ori:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
arielldev authored Mar 20, 2024
2 parents c66eed6 + 6a0ab18 commit 87a7b7a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src-tauri/src/data/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ pub fn load_user(name: &str, password: &str) -> Result<(), String>{
"fs" => {
let file_content = read(&path).unwrap();
let original = aes_decrypt(name, password, &file_content);
unsafe{}

let original = serde_json::from_slice(&original).unwrap();
unsafe{FS = original};
}
_ => {}

Expand Down
47 changes: 39 additions & 8 deletions src-tauri/src/fs/commands.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::PathBuf;
use std::{fs, path::PathBuf};
use serde::{Deserialize, Serialize};
use std::fs::read;
use serde_json::Error;
use std::fs::{write,read};

pub static mut FS: Home = Home::new();

Expand All @@ -14,13 +15,15 @@ pub fn pwd() -> String{return unsafe {

#[tauri::command]
pub fn cd(new: String) {
for item in unsafe {FS.current_dir.files.iter()}{
unsafe{
for item in FS.current_dir.files.iter(){
let dir = item.get_directory();
if dir.is_none(){continue}
// if you use an else block then dir will need to be mutable
let dir = dir.unwrap();
if dir.name == new{FS.cd(dir);}
if dir.name == new{unsafe{FS.cd(dir);}}
}
}
}

#[tauri::command]
Expand All @@ -31,12 +34,21 @@ pub fn ls() -> Vec<String> {
DiretoryItems::File(file) => file.name.clone()
}).collect::<Vec<String>>()}
}
#[tauri::command]
pub fn create_file(file_name: String, location: PathBuf) {
unsafe {
let new_file = File::new(file_name, location);
FS.current_dir.files.push(DiretoryItems::File(new_file))

}
}


#[derive(Clone, Debug, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Home{
path: Vec<Directory>,
current_dir: Directory,

}

impl Home{
Expand All @@ -47,12 +59,17 @@ impl Home{
let bin_dir = Directory::new(String::from("bin"));
home_dir.files.push(DiretoryItems::Directory(bin_dir));
self.path.push(home_dir);
println!("path:{:?}", self.path);
}

pub fn cd_back(&mut self) {if self.path.len() > 1 {self.path.pop();self.current_dir = self.path.last().unwrap().clone();}}
pub fn cd(&mut self, dir: Directory) {self.current_dir = dir.clone();self.path.push(dir);}
pub fn to_bytes(&self) -> Result<Vec<u8>, Error>{return serde_json::to_vec(self);}
}

#[tauri::command]
pub fn mkdir(name: String) {unsafe{FS.current_dir.files.push(DiretoryItems::Directory(Directory::new(name)))};}


#[derive(Clone, Debug, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Directory{
files: Vec<DiretoryItems>,
Expand All @@ -68,6 +85,7 @@ pub enum DiretoryItems{File(File),Directory(Directory)}

impl DiretoryItems{
pub fn get_directory(&self) -> Option<Directory>{match self{Self::Directory(dir)=>{Some(dir.clone())} _=>{None}}}
pub fn get_file(&self) -> Option<File>{match self{Self::File(file)=>{Some(file.clone())} _=>{None}}}
}

#[derive(Clone, Debug, PartialEq, PartialOrd, Serialize, Deserialize)]
Expand All @@ -76,17 +94,30 @@ pub struct File{
location: PathBuf,
}
impl File{
pub fn new(name: String,location: PathBuf) -> Self {return File {name, location}}
pub fn add_data(&self, data: Vec<String>) -> Option<()>{
let content = data.join("\n");
let data = fs::write(self.location.as_path(), content);
if data.is_err(){return None;}
// Ori will work on it later
None
}
pub fn open(&self) -> Option<Vec<u8>> {
let data = read(self.location.as_path());
if data.is_err(){return None;}
// i'l keep on working on it later
None
}

}

#[test]
fn test_fs(){
fn test_fs() {
let mut home = Home::new();
home.init_fs();

let mut dir = Directory::new(String::from("man"));
home.cd(dir.clone());
let location = PathBuf::from("Home/man/");
let new_file = File::new(String::from("salves"), location.clone());
dir.files.push(DiretoryItems::File(new_file));
println!("dir files: {:?}", home.path);
}
4 changes: 2 additions & 2 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use base64::{decode_config, encode_config, URL_SAFE};
use crate::fs::encryption::aes_encrypt;
use crate::data::auth::{init_dir, save_user, authenticate_user, load_user, user_exists, create_user};
use crate::data::auth::Encodable;
use crate::fs::commands::{pwd, ls, FS, cd};
use crate::fs::commands::{pwd, ls, FS, cd, mkdir};

pub fn dir() -> PathBuf {data_dir().expect("failed to enter data directory").join("d_vault_data")}
pub fn get_user_dir(name: &str, password: &str) -> PathBuf{
Expand Down Expand Up @@ -80,6 +80,6 @@ pwd - shows your current path
fn main() {
tauri::Builder::default().invoke_handler(tauri::generate_handler![
first_init, list_commands, console, user_get, authenticate_user, save_user, user_exists, load_user, ls, pwd, cd, create_user,
create_value,
create_value, mkdir
]).run(tauri::generate_context!()).expect("failed to run the code");
}

0 comments on commit 87a7b7a

Please sign in to comment.