[Fix] use working directory instead of executable directory
This commit is contained in:
parent
9b99d95307
commit
c04e70ef85
4 changed files with 5256 additions and 24 deletions
5236
communities/Cargo.lock
generated
Normal file
5236
communities/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -9,6 +9,7 @@ use crate::{
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use json::{JsonValue, array, object};
|
use json::{JsonValue, array, object};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::{any::Any, collections::HashMap};
|
use std::{any::Any, collections::HashMap};
|
||||||
use ttp_core::{CommunicationType, CommunicationValue, DataTypes};
|
use ttp_core::{CommunicationType, CommunicationValue, DataTypes};
|
||||||
|
|
@ -36,7 +37,9 @@ impl TextChat {
|
||||||
self.get_name()
|
self.get_name()
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Err(e) = fs::create_dir_all(user_dir) {
|
let working_dir = iota_util::file_util::get_directory();
|
||||||
|
let full_dir = Path::new(&working_dir).join(user_dir);
|
||||||
|
if let Err(e) = fs::create_dir_all(&full_dir) {
|
||||||
log!("Failed to create chat directory: {}", e);
|
log!("Failed to create chat directory: {}", e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,8 +57,10 @@ pub fn startup() {
|
||||||
LOGGER.set(tx).expect("Logger already initialized");
|
LOGGER.set(tx).expect("Logger already initialized");
|
||||||
|
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let log_dir = Path::new("logs");
|
let working_dir = iota_util::file_util::get_directory();
|
||||||
fs::create_dir_all(log_dir).expect("Failed to create log directory");
|
let base_dir = Path::new(&working_dir);
|
||||||
|
let log_dir = base_dir.join("logs");
|
||||||
|
fs::create_dir_all(&log_dir).expect("Failed to create log directory");
|
||||||
|
|
||||||
let start_ts = SystemTime::now()
|
let start_ts = SystemTime::now()
|
||||||
.duration_since(UNIX_EPOCH)
|
.duration_since(UNIX_EPOCH)
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,12 @@
|
||||||
use iota_util::file_util::save_file;
|
use iota_util::file_util::{load_file, save_file};
|
||||||
use json::{self, Array, JsonValue};
|
use json::{self, Array, JsonValue};
|
||||||
use std::fs;
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
pub struct UserCommunityUtil;
|
pub struct UserCommunityUtil;
|
||||||
|
|
||||||
impl UserCommunityUtil {
|
impl UserCommunityUtil {
|
||||||
pub fn add_community(storage_owner: i64, address: String, title: String, position: String) {
|
pub fn add_community(storage_owner: i64, address: String, title: String, position: String) {
|
||||||
let file_path = format!("users/{}/", storage_owner);
|
let file_path = format!("users/{}/", storage_owner);
|
||||||
let mut communities = Self::load_array(&file_path);
|
let mut communities = Self::load_array(&file_path, "communities.json");
|
||||||
|
|
||||||
let mut community = JsonValue::new_object();
|
let mut community = JsonValue::new_object();
|
||||||
community["title"] = JsonValue::String(title);
|
community["title"] = JsonValue::String(title);
|
||||||
|
|
@ -26,7 +24,7 @@ impl UserCommunityUtil {
|
||||||
|
|
||||||
pub fn remove_community(storage_owner: i64, community_address: String) {
|
pub fn remove_community(storage_owner: i64, community_address: String) {
|
||||||
let file_path = format!("users/{}/", storage_owner);
|
let file_path = format!("users/{}/", storage_owner);
|
||||||
let communities = Self::load_array(&file_path);
|
let communities = Self::load_array(&file_path, "communities.json");
|
||||||
|
|
||||||
let filtered: Array = communities
|
let filtered: Array = communities
|
||||||
.iter()
|
.iter()
|
||||||
|
|
@ -41,27 +39,20 @@ impl UserCommunityUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_communities(storage_owner: i64) -> Array {
|
pub fn get_communities(storage_owner: i64) -> Array {
|
||||||
let file_path = format!("users/{}/communities.json", storage_owner);
|
let file_path = format!("users/{}/", storage_owner);
|
||||||
Self::load_array(&file_path)
|
Self::load_array(&file_path, "communities.json")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_array(file_path: &str) -> Array {
|
fn load_array(dir: &str, name: &str) -> Array {
|
||||||
if !Path::new(file_path).exists() {
|
let content = load_file(dir, name);
|
||||||
|
if content.is_empty() {
|
||||||
return Array::new();
|
return Array::new();
|
||||||
}
|
}
|
||||||
|
|
||||||
match fs::read_to_string(file_path) {
|
let parsed = json::parse(&content);
|
||||||
Ok(content) => {
|
match parsed {
|
||||||
let parsed = json::parse(&content);
|
Ok(JsonValue::Array(arr)) => arr,
|
||||||
match parsed {
|
_ => Array::new(),
|
||||||
Ok(JsonValue::Array(arr)) => arr,
|
|
||||||
_ => Array::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
eprintln!("Failed to read file {}: {}", file_path, err);
|
|
||||||
Array::new()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue