Skip to content

Commit

Permalink
fix: opening some directory crashes xplorer
Browse files Browse the repository at this point in the history
  • Loading branch information
kimlimjustin committed Dec 23, 2021
1 parent 288bdff commit 349187f
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 132 deletions.
121 changes: 121 additions & 0 deletions src-tauri/src/extensions.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::files_api;
use crate::storage;
use crate::util::read_to_serde_json;
use crate::ARGS_STRUCT;
use notify::{raw_watcher, RawEvent, RecursiveMode, Watcher};
use std::env;
use std::path::{Path, PathBuf};
use std::sync::mpsc::channel;
use url::Url;
extern crate path_absolutize;
use path_absolutize::*;

Expand Down Expand Up @@ -416,3 +418,122 @@ pub fn uninstall_extensions(extension_identifier: String) {
}
storage::write_data("extensions".to_string(), serde_json::json!(new_extensions));
}

#[tokio::main]
pub async fn init_extension() {
// Extensions stuff
if ARGS_STRUCT.subcommand_matches("extensions").is_some() {
let extension_cmd = ARGS_STRUCT.subcommand_matches("extensions").unwrap();
match extension_cmd.subcommand_matches("theme") {
Some(theme_command) => {
match theme_command.subcommand_matches("build") {
Some(theme_build_info) => {
let configuration = theme_build_info.value_of("configuration");
if configuration.is_some() {
let configuration = configuration.unwrap();
if configuration == "." {
build_themes(
Path::new(&env::current_dir().unwrap().join("package.json")).to_path_buf(),
)
} else {
let configuration = Path::new(configuration);
if configuration.exists() && configuration.is_file() {
build_themes(configuration.to_path_buf())
} else {
build_themes(configuration.join("package.json").to_path_buf())
}
}
} else {
build_themes(
Path::new(&env::current_dir().unwrap().join("package.json")).to_path_buf(),
);
}
std::process::exit(0);
}
None => {}
};
match theme_command.subcommand_matches("install") {
Some(theme_install_info) => {
let theme = theme_install_info.value_of("theme");
if theme.is_some() {
let theme = theme.unwrap();
if theme == "." {
install_themes(read_to_serde_json(
Path::new(&env::current_dir().unwrap().join("dist/themes.xtension"))
.to_path_buf(),
))
} else {
let theme_path = Path::new(theme.clone());
if theme_path.exists() && theme_path.is_file() {
install_themes(read_to_serde_json(theme_path.to_path_buf()))
} else if Url::parse(theme).is_ok() {
let res = reqwest::get(theme).await.unwrap();
let body = res.text().await.unwrap();
let body = serde_json::from_str::<serde_json::Value>(body.as_str()).unwrap();
install_themes(body)
} else {
install_themes(read_to_serde_json(
theme_path.join("dist/themes.xtension").to_path_buf(),
))
}
}
} else {
install_themes(read_to_serde_json(
Path::new(&env::current_dir().unwrap().join("dist/themes.xtension")).to_path_buf(),
));
}
std::process::exit(0);
}
None => {}
}
}
None => {}
}
match extension_cmd.subcommand_matches("install") {
Some(extension_install_info) => {
let extension = extension_install_info.value_of("extension");
if extension.is_some() {
let extension = extension.unwrap();
let extension_path = Path::new(extension);
if extension_path.exists() && extension_path.is_file() {
install_extensions(read_to_serde_json(extension_path.to_path_buf()))
} else if Url::parse(extension).is_ok() {
let res = reqwest::get(extension).await.unwrap();
let body = res.text().await.unwrap();
let body = serde_json::from_str::<serde_json::Value>(body.as_str()).unwrap();
install_themes(body)
} else {
panic!("Extension file not found");
}
} else {
panic!("No extension specified");
}
std::process::exit(0);
}
None => {}
}
match extension_cmd.subcommand_matches("uninstall") {
Some(extension_uninstall_info) => {
let extension = extension_uninstall_info.value_of("extension");
if extension.is_some() {
let extension = extension.unwrap();
uninstall_extensions(extension.to_string());
} else {
panic!("No extension specified");
}
std::process::exit(0);
}
None => {}
}
}
let xtension_arg = ARGS_STRUCT.value_of("xtension");
if xtension_arg.is_some() {
let xtension_arg = xtension_arg.unwrap();
let xtension_arg = Path::new(xtension_arg);
if xtension_arg.exists() && xtension_arg.is_file() {
install_extensions(read_to_serde_json(xtension_arg.to_path_buf()));
} else {
panic!("Extension file not found");
}
};
}
135 changes: 3 additions & 132 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@ mod extensions;
mod file_lib;
mod files_api;
mod storage;
mod util;
use clap::{App, Arg, ArgMatches};
use std::path::Path;
mod tests;
use font_loader::system_fonts;
use lazy_static::lazy_static;
use std::env;
extern crate reqwest;
#[cfg(target_os = "windows")]
use std::os::windows::process::CommandExt;
use std::path::PathBuf;
use std::process::Command;
use tauri::Manager;
use tauri_plugin_vibrancy::Vibrancy;
use url::Url;

lazy_static! {
pub static ref ARGS_STRUCT: ArgMatches = {
Expand Down Expand Up @@ -149,135 +147,8 @@ fn change_transparent_effect(effect: String, window: tauri::Window) {
}
}

fn read_to_serde_json(path: PathBuf) -> serde_json::Value {
let file: Result<serde_json::Value, serde_json::Error> =
serde_json::from_str(std::fs::read_to_string(path).unwrap().as_str());
let file = match file {
Ok(file) => file,
Err(_) => {
panic!("Error parsing file");
}
};
file
}

#[tokio::main]
async fn main() {
// Extensions stuff
if ARGS_STRUCT.subcommand_matches("extensions").is_some() {
let extension_cmd = ARGS_STRUCT.subcommand_matches("extensions").unwrap();
match extension_cmd.subcommand_matches("theme") {
Some(theme_command) => {
match theme_command.subcommand_matches("build") {
Some(theme_build_info) => {
let configuration = theme_build_info.value_of("configuration");
if configuration.is_some() {
let configuration = configuration.unwrap();
if configuration == "." {
extensions::build_themes(
Path::new(&env::current_dir().unwrap().join("package.json")).to_path_buf(),
)
} else {
let configuration = Path::new(configuration);
if configuration.exists() && configuration.is_file() {
extensions::build_themes(configuration.to_path_buf())
} else {
extensions::build_themes(configuration.join("package.json").to_path_buf())
}
}
} else {
extensions::build_themes(
Path::new(&env::current_dir().unwrap().join("package.json")).to_path_buf(),
);
}
std::process::exit(0);
}
None => {}
};
match theme_command.subcommand_matches("install") {
Some(theme_install_info) => {
let theme = theme_install_info.value_of("theme");
if theme.is_some() {
let theme = theme.unwrap();
if theme == "." {
extensions::install_themes(read_to_serde_json(
Path::new(&env::current_dir().unwrap().join("dist/themes.xtension"))
.to_path_buf(),
))
} else {
let theme_path = Path::new(theme.clone());
if theme_path.exists() && theme_path.is_file() {
extensions::install_themes(read_to_serde_json(theme_path.to_path_buf()))
} else if Url::parse(theme).is_ok() {
let res = reqwest::get(theme).await.unwrap();
let body = res.text().await.unwrap();
let body = serde_json::from_str::<serde_json::Value>(body.as_str()).unwrap();
extensions::install_themes(body)
} else {
extensions::install_themes(read_to_serde_json(
theme_path.join("dist/themes.xtension").to_path_buf(),
))
}
}
} else {
extensions::install_themes(read_to_serde_json(
Path::new(&env::current_dir().unwrap().join("dist/themes.xtension")).to_path_buf(),
));
}
std::process::exit(0);
}
None => {}
}
}
None => {}
}
match extension_cmd.subcommand_matches("install") {
Some(extension_install_info) => {
let extension = extension_install_info.value_of("extension");
if extension.is_some() {
let extension = extension.unwrap();
let extension_path = Path::new(extension);
if extension_path.exists() && extension_path.is_file() {
extensions::install_extensions(read_to_serde_json(extension_path.to_path_buf()))
} else if Url::parse(extension).is_ok() {
let res = reqwest::get(extension).await.unwrap();
let body = res.text().await.unwrap();
let body = serde_json::from_str::<serde_json::Value>(body.as_str()).unwrap();
extensions::install_themes(body)
} else {
panic!("Extension file not found");
}
} else {
panic!("No extension specified");
}
std::process::exit(0);
}
None => {}
}
match extension_cmd.subcommand_matches("uninstall") {
Some(extension_uninstall_info) => {
let extension = extension_uninstall_info.value_of("extension");
if extension.is_some() {
let extension = extension.unwrap();
extensions::uninstall_extensions(extension.to_string());
} else {
panic!("No extension specified");
}
std::process::exit(0);
}
None => {}
}
}
let xtension_arg = ARGS_STRUCT.value_of("xtension");
if xtension_arg.is_some() {
let xtension_arg = xtension_arg.unwrap();
let xtension_arg = Path::new(xtension_arg);
if xtension_arg.exists() && xtension_arg.is_file() {
extensions::install_extensions(read_to_serde_json(xtension_arg.to_path_buf()));
} else {
panic!("Extension file not found");
}
};
fn main() {
extensions::init_extension();
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![
files_api::read_directory,
Expand Down
12 changes: 12 additions & 0 deletions src-tauri/src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use std::path::PathBuf;
pub fn read_to_serde_json(path: PathBuf) -> serde_json::Value {
let file: Result<serde_json::Value, serde_json::Error> =
serde_json::from_str(std::fs::read_to_string(path).unwrap().as_str());
let file = match file {
Ok(file) => file,
Err(_) => {
panic!("Error parsing file");
}
};
file
}

2 comments on commit 349187f

@vercel
Copy link

@vercel vercel bot commented on 349187f Dec 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 349187f Dec 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

updater – ./api/updater

updater.xplorer.space
updater-kimlimjustin.vercel.app
updater-git-master-kimlimjustin.vercel.app

Please sign in to comment.