extern crate dirs;
use std::io::Read;
use std::io::Write;
#[derive(Debug)]
pub struct FileSystem {
base_dir: String
}
impl FileSystem {
pub fn init() -> FileSystem {
let home_dir = dirs::home_dir();
let home_dir_string: String = home_dir.unwrap().to_str().unwrap().into();
FileSystem{base_dir: home_dir_string}
}
pub fn create_application(&self, _bytecode_wasm: &str) -> String {
let uuid = uuid::Uuid::new_v4().to_string();
let mut path = std::path::PathBuf::from(&self.base_dir);
path.push(&uuid);
std::fs::create_dir_all(path.as_path()).unwrap();
path.push("bytecode.wasm");
let mut file = std::fs::File::create(path.as_path()).unwrap();
file.write_all(_bytecode_wasm.as_bytes());
uuid
}
pub fn read_application(&self, _application_uuid: &str) -> String {
let mut reading_path = std::path::PathBuf::from(&self.base_dir);
reading_path.push(&_application_uuid);
reading_path.push("bytecode.wasm");
let mut file_to_read = std::fs::File::open(reading_path.as_path()).unwrap();
let mut buffer = Vec::new();
let bytecode_wasm_string = file_to_read.read_to_end(&mut buffer).unwrap();
bytecode_wasm_string.to_string()
}
pub fn update_application(&self, _application_uuid: &str, _bytecode_wasm: &str) -> String {
let mut update_path = std::path::PathBuf::from(&self.base_dir);
update_path.push(&_application_uuid);
update_path.push("bytecode.wasm");
let _file_to_update = std::fs::remove_file(update_path.as_path()).unwrap();
let mut file_to_write = std::fs::File::create(update_path.as_path()).unwrap();
file_to_write.write_all(_bytecode_wasm.as_bytes());
_application_uuid.to_string()
}
pub fn delete_application(&self, _application_uuid: &str) -> String {
let mut path = std::path::PathBuf::from(&self.base_dir);
path.push(&_application_uuid);
std::fs::remove_dir_all(path.as_path()).unwrap();
_application_uuid.to_string()
}
}