[Fix] use working directory instead of executable directory

This commit is contained in:
Alois 2026-05-18 21:25:21 +02:00
commit c04e70ef85
4 changed files with 5256 additions and 24 deletions

5236
communities/Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -9,6 +9,7 @@ use crate::{
use async_trait::async_trait;
use json::{JsonValue, array, object};
use std::fs;
use std::path::Path;
use std::sync::Arc;
use std::{any::Any, collections::HashMap};
use ttp_core::{CommunicationType, CommunicationValue, DataTypes};
@ -36,7 +37,9 @@ impl TextChat {
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);
return;
}

View file

@ -57,8 +57,10 @@ pub fn startup() {
LOGGER.set(tx).expect("Logger already initialized");
thread::spawn(move || {
let log_dir = Path::new("logs");
fs::create_dir_all(log_dir).expect("Failed to create log directory");
let working_dir = iota_util::file_util::get_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()
.duration_since(UNIX_EPOCH)

View file

@ -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 std::fs;
use std::path::Path;
pub struct UserCommunityUtil;
impl UserCommunityUtil {
pub fn add_community(storage_owner: i64, address: String, title: String, position: String) {
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();
community["title"] = JsonValue::String(title);
@ -26,7 +24,7 @@ impl UserCommunityUtil {
pub fn remove_community(storage_owner: i64, community_address: String) {
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
.iter()
@ -41,27 +39,20 @@ impl UserCommunityUtil {
}
pub fn get_communities(storage_owner: i64) -> Array {
let file_path = format!("users/{}/communities.json", storage_owner);
Self::load_array(&file_path)
let file_path = format!("users/{}/", storage_owner);
Self::load_array(&file_path, "communities.json")
}
fn load_array(file_path: &str) -> Array {
if !Path::new(file_path).exists() {
fn load_array(dir: &str, name: &str) -> Array {
let content = load_file(dir, name);
if content.is_empty() {
return Array::new();
}
match fs::read_to_string(file_path) {
Ok(content) => {
let parsed = json::parse(&content);
match parsed {
Ok(JsonValue::Array(arr)) => arr,
_ => Array::new(),
}
}
Err(err) => {
eprintln!("Failed to read file {}: {}", file_path, err);
Array::new()
}
}
}
}